UNPKG

6.72 kBTypeScriptView Raw
1export interface IStringable {
2 toString: () => string;
3}
4export interface IBaseExpression<T> {
5 /**
6 * Inserts a tag name in the expression.
7 */
8 tag: (name: string) => T;
9 /**
10 * Inserts a field name in the expression.
11 */
12 field: (name: string) => T;
13 /**
14 * Chains on a value to the expression. An error will be thrown if the
15 * value is a type we can't represent in InfluxQL, primarily `null` or
16 * `undefined.`
17 */
18 value: (value: any) => T;
19}
20export interface IExpressionHead extends IBaseExpression<IBinaryOp> {
21}
22export interface IExpressionTail extends IBaseExpression<IExpressionHead> {
23}
24export interface IBinaryOp {
25 /**
26 * Adds an 'AND' operator
27 */
28 and: IExpressionTail;
29 /**
30 * Adds an 'OR' operator
31 */
32 or: IExpressionTail;
33 /**
34 * Adds a '+' addition symbol
35 */
36 plus: IExpressionTail;
37 /**
38 * Adds a '*' multiplication symbol
39 */
40 times: IExpressionTail;
41 /**
42 * Adds a '-' subtraction symbol
43 */
44 minus: IExpressionTail;
45 /**
46 * Adds a '/' division symbol
47 */
48 div: IExpressionTail;
49 /**
50 * Adds a '=' symbol
51 */
52 equals: IExpressionTail;
53 /**
54 * Adds a '=~' comparator to select entries matching a regex.
55 */
56 matches: IExpressionTail;
57 /**
58 * Adds a '!~' comparator to select entries not matching a regex.
59 */
60 doesntMatch: IExpressionTail;
61 /**
62 * Adds a '!=' comparator to select entries not equaling a certain value.
63 */
64 notEqual: IExpressionTail;
65 /**
66 * Adds a '>' symbol
67 */
68 gt: IExpressionTail;
69 /**
70 * Adds a '>=' symbol
71 */
72 gte: IExpressionTail;
73 /**
74 * Adds a '<' symbol
75 */
76 lt: IExpressionTail;
77 /**
78 * Adds a '<=' symbol
79 */
80 lte: IExpressionTail;
81}
82/**
83 * Expression is used to build filtering expressions, like those used in WHERE
84 * clauses. It can be used for fluent and safe building of queries using
85 * untrusted input.
86 *
87 * @example
88 * e => e
89 * .field('host').equals.value('ares.peet.io')
90 * .or
91 * .field('host').matches(/example\.com$/)
92 * .or
93 * .expr(e => e
94 * .field('country').equals.value('US')
95 * .and
96 * .field('state').equals.value('WA'));
97 *
98 * // Generates:
99 * // "host" = 'ares.peet.io' OR "host" ~= /example\.com$/ OR \
100 * // ("county" = 'US' AND "state" = 'WA')
101 */
102export declare class Expression implements IExpressionHead, IExpressionTail, IBinaryOp {
103 private readonly _query;
104 /**
105 * Inserts a tag reference into the expression; the name will be
106 * automatically escaped.
107 * @param name
108 * @return
109 */
110 tag(name: string): this;
111 /**
112 * Inserts a field reference into the expression; the name will be
113 * automatically escaped.
114 * @param name
115 * @return
116 */
117 field(name: string): this;
118 /**
119 * Inserts a subexpression; invokes the function with a new expression
120 * that can be chained on.
121 * @param fn
122 * @return
123 * @example
124 * e.field('a').equals.value('b')
125 * .or.expr(e =>
126 * e.field('b').equals.value('b')
127 * .and.field('a').equals.value('c'))
128 * .toString()
129 * // "a" = 'b' OR ("b" = 'b' AND "a" = 'c')
130 */
131 exp(fn: (e: Expression) => Expression): this;
132 /**
133 * Value chains on a value to the expression.
134 *
135 * - Numbers will be inserted verbatim
136 * - Strings will be escaped and inserted
137 * - Booleans will be inserted correctly
138 * - Dates will be formatted and inserted correctly, including INanoDates.
139 * - Regular expressions will be inserted correctly, however an error will
140 * be thrown if they contain flags, as regex flags do not work in Influx
141 * - Otherwise we'll try to call `.toString()` on the value, throwing
142 * if we cannot do so.
143 *
144 * @param value
145 * @return
146 */
147 value(value: any): this;
148 /**
149 * Chains on an AND clause to the expression.
150 */
151 get and(): this;
152 /**
153 * Chains on an OR clause to the expression.
154 */
155 get or(): this;
156 /**
157 * Chains on a `+` operator to the expression.
158 */
159 get plus(): this;
160 /**
161 * Chains on a `*` operator to the expression.
162 */
163 get times(): this;
164 /**
165 * Chains on a `-` operator to the expression.
166 */
167 get minus(): this;
168 /**
169 * Chains on a `/` operator to the expression.
170 */
171 get div(): this;
172 /**
173 * Chains on a `=` conditional to the expression.
174 */
175 get equals(): this;
176 /**
177 * Chains on a `=~` conditional to the expression to match regexes.
178 */
179 get matches(): this;
180 /**
181 * Chains on a `!`` conditional to the expression to match regexes.
182 */
183 get doesntMatch(): this;
184 /**
185 * Chains on a `!=` conditional to the expression.
186 */
187 get notEqual(): this;
188 /**
189 * Chains on a `>` conditional to the expression.
190 */
191 get gt(): this;
192 /**
193 * Chains on a `>=` conditional to the expression.
194 */
195 get gte(): this;
196 /**
197 * Chains on a `<` conditional to the expression.
198 */
199 get lt(): this;
200 /**
201 * Chains on a `<=` conditional to the expression.
202 */
203 get lte(): this;
204 /**
205 * Converts the expression into its InfluxQL representation.
206 * @return
207 */
208 toString(): string;
209}
210/**
211 * Measurement creates a reference to a particular measurement. You can
212 * reference it solely by its name, but you can also specify the retention
213 * policy and database it lives under.
214 *
215 * @example
216 * m.name('my_measurement') // "my_measurement"
217 * m.name('my_measurement').policy('one_day') // "one_day"."my_measurement"
218 * m.name('my_measurement').policy('one_day').db('mydb') // "mydb"."one_day"."my_measurement"
219 */
220export declare class Measurement {
221 private _parts;
222 /**
223 * Sets the measurement name.
224 * @param name
225 * @return
226 */
227 name(name: string): this;
228 /**
229 * Sets the retention policy name.
230 * @param retentionPolicy
231 * @return
232 */
233 policy(retentionPolicy: string): this;
234 /**
235 * Sets the database name.
236 * @param db
237 * @return
238 */
239 db(db: string): this;
240 /**
241 * Converts the measurement into its InfluxQL representation.
242 * @return
243 * @throws {Error} if a measurement name is not provided
244 */
245 toString(): string;
246}
247export declare type measurement = {
248 measurement: string | ((m: Measurement) => IStringable);
249};
250export declare type where = {
251 where: string | ((e: IExpressionHead) => IStringable);
252};
253export declare function parseMeasurement(q: measurement): string;
254export declare function parseWhere(q: where): string;
255
\No newline at end of file