-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathquery.ts
More file actions
576 lines (519 loc) · 16.9 KB
/
query.ts
File metadata and controls
576 lines (519 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import JSONbigModule from 'json-bigint';
const JSONbig = JSONbigModule({ useNativeBigInt: true });
type QueryTypesSingle = string | number | bigint | boolean;
export type QueryTypesList = string[] | number[] | bigint[] | boolean[] | Query[] | any[];
export type QueryTypes = QueryTypesSingle | QueryTypesList;
type AttributesTypes = string | string[];
/**
* Helper class to generate query strings.
*/
export class Query {
method: string;
attribute: AttributesTypes | undefined;
values: QueryTypesList | undefined;
/**
* Constructor for Query class.
*
* @param {string} method
* @param {AttributesTypes} attribute
* @param {QueryTypes} values
*/
constructor(
method: string,
attribute?: AttributesTypes,
values?: QueryTypes
) {
this.method = method;
this.attribute = attribute;
if (values !== undefined) {
if (Array.isArray(values)) {
this.values = values;
} else {
this.values = [values] as QueryTypesList;
}
}
}
/**
* Convert the query object to a JSON string.
*
* @returns {string}
*/
toString(): string {
return JSONbig.stringify({
method: this.method,
attribute: this.attribute,
values: this.values,
});
}
/**
* Filter resources where attribute is equal to value.
*
* @param {string} attribute
* @param {QueryTypes} value
* @returns {string}
*/
static equal = (attribute: string, value: QueryTypes): string =>
new Query("equal", attribute, value).toString();
/**
* Filter resources where attribute is not equal to value.
*
* @param {string} attribute
* @param {QueryTypes} value
* @returns {string}
*/
static notEqual = (attribute: string, value: QueryTypes): string =>
new Query("notEqual", attribute, value).toString();
/**
* Filter resources where attribute matches a regular expression pattern.
*
* @param {string} attribute The attribute to filter on.
* @param {string} pattern The regular expression pattern to match.
* @returns {string}
*/
static regex = (attribute: string, pattern: string): string =>
new Query("regex", attribute, pattern).toString();
/**
* Filter resources where attribute is less than value.
*
* @param {string} attribute
* @param {QueryTypes} value
* @returns {string}
*/
static lessThan = (attribute: string, value: QueryTypes): string =>
new Query("lessThan", attribute, value).toString();
/**
* Filter resources where attribute is less than or equal to value.
*
* @param {string} attribute
* @param {QueryTypes} value
* @returns {string}
*/
static lessThanEqual = (attribute: string, value: QueryTypes): string =>
new Query("lessThanEqual", attribute, value).toString();
/**
* Filter resources where attribute is greater than value.
*
* @param {string} attribute
* @param {QueryTypes} value
* @returns {string}
*/
static greaterThan = (attribute: string, value: QueryTypes): string =>
new Query("greaterThan", attribute, value).toString();
/**
* Filter resources where attribute is greater than or equal to value.
*
* @param {string} attribute
* @param {QueryTypes} value
* @returns {string}
*/
static greaterThanEqual = (attribute: string, value: QueryTypes): string =>
new Query("greaterThanEqual", attribute, value).toString();
/**
* Filter resources where attribute is null.
*
* @param {string} attribute
* @returns {string}
*/
static isNull = (attribute: string): string =>
new Query("isNull", attribute).toString();
/**
* Filter resources where attribute is not null.
*
* @param {string} attribute
* @returns {string}
*/
static isNotNull = (attribute: string): string =>
new Query("isNotNull", attribute).toString();
/**
* Filter resources where the specified attributes exist.
*
* @param {string[]} attributes The list of attributes that must exist.
* @returns {string}
*/
static exists = (attributes: string[]): string =>
new Query("exists", undefined, attributes).toString();
/**
* Filter resources where the specified attributes do not exist.
*
* @param {string[]} attributes The list of attributes that must not exist.
* @returns {string}
*/
static notExists = (attributes: string[]): string =>
new Query("notExists", undefined, attributes).toString();
/**
* Filter resources where attribute is between start and end (inclusive).
*
* @param {string} attribute
* @param {string | number | bigint} start
* @param {string | number | bigint} end
* @returns {string}
*/
static between = (attribute: string, start: string | number | bigint, end: string | number | bigint): string =>
new Query("between", attribute, [start, end] as QueryTypesList).toString();
/**
* Filter resources where attribute starts with value.
*
* @param {string} attribute
* @param {string} value
* @returns {string}
*/
static startsWith = (attribute: string, value: string): string =>
new Query("startsWith", attribute, value).toString();
/**
* Filter resources where attribute ends with value.
*
* @param {string} attribute
* @param {string} value
* @returns {string}
*/
static endsWith = (attribute: string, value: string): string =>
new Query("endsWith", attribute, value).toString();
/**
* Specify which attributes should be returned by the API call.
*
* @param {string[]} attributes
* @returns {string}
*/
static select = (attributes: string[]): string =>
new Query("select", undefined, attributes).toString();
/**
* Filter resources by searching attribute for value.
* A fulltext index on attribute is required for this query to work.
*
* @param {string} attribute
* @param {string} value
* @returns {string}
*/
static search = (attribute: string, value: string): string =>
new Query("search", attribute, value).toString();
/**
* Sort results by attribute descending.
*
* @param {string} attribute
* @returns {string}
*/
static orderDesc = (attribute: string): string =>
new Query("orderDesc", attribute).toString();
/**
* Sort results by attribute ascending.
*
* @param {string} attribute
* @returns {string}
*/
static orderAsc = (attribute: string): string =>
new Query("orderAsc", attribute).toString();
/**
* Sort results randomly.
*
* @returns {string}
*/
static orderRandom = (): string =>
new Query("orderRandom").toString();
/**
* Return results after documentId.
*
* @param {string} documentId
* @returns {string}
*/
static cursorAfter = (documentId: string): string =>
new Query("cursorAfter", undefined, documentId).toString();
/**
* Return results before documentId.
*
* @param {string} documentId
* @returns {string}
*/
static cursorBefore = (documentId: string): string =>
new Query("cursorBefore", undefined, documentId).toString();
/**
* Return only limit results.
*
* @param {number} limit
* @returns {string}
*/
static limit = (limit: number): string =>
new Query("limit", undefined, limit).toString();
/**
* Filter resources by skipping the first offset results.
*
* @param {number} offset
* @returns {string}
*/
static offset = (offset: number): string =>
new Query("offset", undefined, offset).toString();
/**
* Filter resources where attribute contains the specified value.
* For string attributes, checks if the string contains the substring.
*
* Note: For array attributes, use {@link containsAny} or {@link containsAll} instead.
* @param {string} attribute
* @param {string | string[]} value
* @returns {string}
*/
static contains = (attribute: string, value: string | any[]): string =>
new Query("contains", attribute, value).toString();
/**
* Filter resources where attribute contains ANY of the specified values.
* For array and relationship attributes, matches documents where the attribute
* contains at least one of the given values.
*
* @param {string} attribute
* @param {any[]} value
* @returns {string}
*/
static containsAny = (attribute: string, value: any[]): string =>
new Query("containsAny", attribute, value).toString();
/**
* Filter resources where attribute contains ALL of the specified values.
* For array and relationship attributes, matches documents where the attribute
* contains every one of the given values.
*
* @param {string} attribute
* @param {any[]} value
* @returns {string}
*/
static containsAll = (attribute: string, value: any[]): string =>
new Query("containsAll", attribute, value).toString();
/**
* Filter resources where attribute does not contain the specified value.
*
* @param {string} attribute
* @param {string | any[]} value
* @returns {string}
*/
static notContains = (attribute: string, value: string | any[]): string =>
new Query("notContains", attribute, value).toString();
/**
* Filter resources by searching attribute for value (inverse of search).
* A fulltext index on attribute is required for this query to work.
*
* @param {string} attribute
* @param {string} value
* @returns {string}
*/
static notSearch = (attribute: string, value: string): string =>
new Query("notSearch", attribute, value).toString();
/**
* Filter resources where attribute is not between start and end (exclusive).
*
* @param {string} attribute
* @param {string | number | bigint} start
* @param {string | number | bigint} end
* @returns {string}
*/
static notBetween = (attribute: string, start: string | number | bigint, end: string | number | bigint): string =>
new Query("notBetween", attribute, [start, end] as QueryTypesList).toString();
/**
* Filter resources where attribute does not start with value.
*
* @param {string} attribute
* @param {string} value
* @returns {string}
*/
static notStartsWith = (attribute: string, value: string): string =>
new Query("notStartsWith", attribute, value).toString();
/**
* Filter resources where attribute does not end with value.
*
* @param {string} attribute
* @param {string} value
* @returns {string}
*/
static notEndsWith = (attribute: string, value: string): string =>
new Query("notEndsWith", attribute, value).toString();
/**
* Filter resources where document was created before date.
*
* @param {string} value
* @returns {string}
*/
static createdBefore = (value: string): string =>
Query.lessThan("$createdAt", value);
/**
* Filter resources where document was created after date.
*
* @param {string} value
* @returns {string}
*/
static createdAfter = (value: string): string =>
Query.greaterThan("$createdAt", value);
/**
* Filter resources where document was created between dates.
*
* @param {string} start
* @param {string} end
* @returns {string}
*/
static createdBetween = (start: string, end: string): string =>
Query.between("$createdAt", start, end);
/**
* Filter resources where document was updated before date.
*
* @param {string} value
* @returns {string}
*/
static updatedBefore = (value: string): string =>
Query.lessThan("$updatedAt", value);
/**
* Filter resources where document was updated after date.
*
* @param {string} value
* @returns {string}
*/
static updatedAfter = (value: string): string =>
Query.greaterThan("$updatedAt", value);
/**
* Filter resources where document was updated between dates.
*
* @param {string} start
* @param {string} end
* @returns {string}
*/
static updatedBetween = (start: string, end: string): string =>
Query.between("$updatedAt", start, end);
/**
* Combine multiple queries using logical OR operator.
*
* @param {string[]} queries
* @returns {string}
*/
static or = (queries: string[]) =>
new Query("or", undefined, queries.map((query) => JSONbig.parse(query))).toString();
/**
* Combine multiple queries using logical AND operator.
*
* @param {string[]} queries
* @returns {string}
*/
static and = (queries: string[]) =>
new Query("and", undefined, queries.map((query) => JSONbig.parse(query))).toString();
/**
* Filter array elements where at least one element matches all the specified queries.
*
* @param {string} attribute The attribute containing the array to filter on.
* @param {string[]} queries The list of query strings to match against array elements.
* @returns {string}
*/
static elemMatch = (attribute: string, queries: string[]): string =>
new Query(
"elemMatch",
attribute,
queries.map((query) => JSONbig.parse(query))
).toString();
/**
* Filter resources where attribute is at a specific distance from the given coordinates.
*
* @param {string} attribute
* @param {any[]} values
* @param {number} distance
* @param {boolean} meters
* @returns {string}
*/
static distanceEqual = (attribute: string, values: any[], distance: number, meters: boolean = true): string =>
new Query("distanceEqual", attribute, [[values, distance, meters]] as QueryTypesList).toString();
/**
* Filter resources where attribute is not at a specific distance from the given coordinates.
*
* @param {string} attribute
* @param {any[]} values
* @param {number} distance
* @param {boolean} meters
* @returns {string}
*/
static distanceNotEqual = (attribute: string, values: any[], distance: number, meters: boolean = true): string =>
new Query("distanceNotEqual", attribute, [[values, distance, meters]] as QueryTypesList).toString();
/**
* Filter resources where attribute is at a distance greater than the specified value from the given coordinates.
*
* @param {string} attribute
* @param {any[]} values
* @param {number} distance
* @param {boolean} meters
* @returns {string}
*/
static distanceGreaterThan = (attribute: string, values: any[], distance: number, meters: boolean = true): string =>
new Query("distanceGreaterThan", attribute, [[values, distance, meters]] as QueryTypesList).toString();
/**
* Filter resources where attribute is at a distance less than the specified value from the given coordinates.
*
* @param {string} attribute
* @param {any[]} values
* @param {number} distance
* @param {boolean} meters
* @returns {string}
*/
static distanceLessThan = (attribute: string, values: any[], distance: number, meters: boolean = true): string =>
new Query("distanceLessThan", attribute, [[values, distance, meters]] as QueryTypesList).toString();
/**
* Filter resources where attribute intersects with the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static intersects = (attribute: string, values: any[]): string =>
new Query("intersects", attribute, [values]).toString();
/**
* Filter resources where attribute does not intersect with the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static notIntersects = (attribute: string, values: any[]): string =>
new Query("notIntersects", attribute, [values]).toString();
/**
* Filter resources where attribute crosses the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static crosses = (attribute: string, values: any[]): string =>
new Query("crosses", attribute, [values]).toString();
/**
* Filter resources where attribute does not cross the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static notCrosses = (attribute: string, values: any[]): string =>
new Query("notCrosses", attribute, [values]).toString();
/**
* Filter resources where attribute overlaps with the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static overlaps = (attribute: string, values: any[]): string =>
new Query("overlaps", attribute, [values]).toString();
/**
* Filter resources where attribute does not overlap with the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static notOverlaps = (attribute: string, values: any[]): string =>
new Query("notOverlaps", attribute, [values]).toString();
/**
* Filter resources where attribute touches the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static touches = (attribute: string, values: any[]): string =>
new Query("touches", attribute, [values]).toString();
/**
* Filter resources where attribute does not touch the given geometry.
*
* @param {string} attribute
* @param {any[]} values
* @returns {string}
*/
static notTouches = (attribute: string, values: any[]): string =>
new Query("notTouches", attribute, [values]).toString();
}