-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathquery.js
3324 lines (3080 loc) · 90.9 KB
/
query.js
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict'
var annotate = require('fn-annotate')
var deprecate = require('util-deprecate')
var Expr = require('./Expr')
var errors = require('./errors')
var values = require('./values')
var objectAssign = require('object-assign')
var util = require('./_util')
/**
* This module contains functions used to construct FaunaDB Queries.
*
* See the [FaunaDB Query API Documentation](https://app.fauna.com/documentation/reference/queryapi)
* for per-function documentation.
*
* @module query
*/
/**
* @typedef {(Expr|string|number|boolean|Object)} module:query~ExprTerm
*/
/**
* @typedef {(module:query~ExprTerm|Array<module:query~ExprTerm>)} module:query~ExprArg
*/
// Type helpers
/**
* If one parameter is provided, constructs a literal Ref value.
* The string `collections/widget/123` will be equivalent to `new values.Ref('123', new values.Ref('widget', values.Native.COLLECTIONS))`
*
* If two are provided, constructs a Ref() function that, when evaluated, returns a Ref value.
*
* @param {string|module:query~ExprArg} ref|cls
* Alone, the ref in path form. Combined with `id`, must be a collection ref.
* @param {module:query~ExprArg} [id]
* A numeric id of the given collection.
* @return {Expr}
*/
function Ref() {
arity.between(1, 2, arguments, Ref.name)
switch (arguments.length) {
case 1:
return new Expr({ '@ref': wrap(arguments[0]) })
case 2:
return new Expr({ ref: wrap(arguments[0]), id: wrap(arguments[1]) })
}
}
/**
* @param {Uint8Array|ArrayBuffer|module:query~ExprArg} bytes
* A base64 encoded string or a byte array
* @return {Expr}
*/
function Bytes(bytes) {
arity.exact(1, arguments, Bytes.name)
return new values.Bytes(bytes)
}
// Basic forms
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* @param {module:query~ExprArg} msg
* The message to send back to the client.
* @return {Expr}
* */
function Abort(msg) {
arity.exact(1, arguments, Abort.name)
return new Expr({ abort: wrap(msg) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* @param {module:query~ExprArg} timestamp
* An Expr that will evaluate to a Time.
* @param {module:query~ExprArg} expr
* The Expr to run at the given snapshot time.
* @return {Expr}
* */
function At(timestamp, expr) {
arity.exact(2, arguments, At.name)
return new Expr({ at: wrap(timestamp), expr: wrap(expr) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* @param {module:query~ExprArg} bindings
* A set of bindings to use within the given expression.
* @param {module:query~ExprArg} in
* The expression to run with the given bindings.
* @return {Expr}
* */
function Let(vars, expr) {
arity.exact(2, arguments, Let.name)
var bindings = []
if (Array.isArray(vars)) {
bindings = vars.map(function(item) {
return wrapValues(item)
})
} else {
bindings = Object.keys(vars)
.filter(function(k) {
return vars[k] !== undefined
})
.map(function(k) {
var b = {}
b[k] = wrap(vars[k])
return b
})
}
if (typeof expr === 'function') {
if (Array.isArray(vars)) {
var expr_vars = []
vars.forEach(function(item) {
Object.keys(item).forEach(function(name) {
expr_vars.push(Var(name))
})
})
expr = expr.apply(null, expr_vars)
} else {
expr = expr.apply(
null,
Object.keys(vars).map(function(name) {
return Var(name)
})
)
}
}
return new Expr({ let: bindings, in: wrap(expr) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* @param {module:query~ExprArg} varName
* The name of the bound var.
* @return {Expr}
* */
function Var(varName) {
arity.exact(1, arguments, Var.name)
return new Expr({ var: wrap(varName) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* @param {module:query~ExprArg} condition
* An expression that returns a boolean.
* @param {module:query~ExprArg} then
* The expression to run if condition is true.
* @param {module:query~ExprArg} else
* The expression to run if the condition is false.
* @return {Expr}
* */
function If(condition, then, _else) {
arity.exact(3, arguments, If.name)
return new Expr({ if: wrap(condition), then: wrap(then), else: wrap(_else) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* @param {...module:query~ExprArg} args
* A series of expressions to run.
* @return {Expr}
* */
function Do() {
arity.min(1, arguments, Do.name)
var args = argsToArray(arguments)
return new Expr({ do: wrap(args) })
}
/** See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* @param {...module:query~ExprArg} fields
* The object to be escaped.
* @return {Expr}
* */
var objectFunction = function(fields) {
arity.exact(1, arguments, objectFunction.name)
return new Expr({ object: wrapValues(fields) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* Directly produces a FaunaDB Lambda expression as described in the FaunaDB reference
* documentation.
*
* @param {module:query~ExprArg} var
* The names of the variables to be bound in this lambda expression.
* @param {module:query~ExprArg} expr
* The lambda expression.
* @return {Expr}
*/
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* Takes a Javascript function, and will transform it
* into the appropriate FaunaDB query. For example:
*
* ```
* Lambda(function(a) { return Add(a, a); });
* // Returns { lambda: 'a', expr: { add: [{ var: a }, { var: a }] } }
* ```
* Note that the driver will handle wrapping all usages of the lambda's bound
* variables with the {@link modules:query~Var} function.
*
* @param {function} func
* Takes the provided function and produces the appropriate FaunaDB query expression.
* @return {Expr}
*
*/ function Lambda() {
arity.between(1, 2, arguments, Lambda.name)
switch (arguments.length) {
case 1:
var value = arguments[0]
if (typeof value === 'function') {
return _lambdaFunc(value)
} else if (
value instanceof Expr ||
util.checkInstanceHasProperty(value, '_isFaunaExpr')
) {
return value
} else {
throw new errors.InvalidValue(
'Lambda function takes either a Function or an Expr.'
)
}
case 2:
var var_name = arguments[0]
var expr = arguments[1]
return _lambdaExpr(var_name, expr)
}
}
/**
* @private
*/
function _lambdaFunc(func) {
var vars = annotate(func)
switch (vars.length) {
case 0:
throw new errors.InvalidValue(
'Provided Function must take at least 1 argument.'
)
case 1:
return _lambdaExpr(vars[0], func(Var(vars[0])))
default:
return _lambdaExpr(
vars,
func.apply(
null,
vars.map(function(name) {
return Var(name)
})
)
)
}
}
/**
* @private
*/
function _lambdaExpr(var_name, expr) {
return new Expr({ lambda: wrap(var_name), expr: wrap(expr) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* Invokes a given function passing in the provided arguments
*
* ```
* Call(Ref("functions/a_function"), 1, 2)
* ```
*
* @param {module:query~ExprArg} ref
* The ref of the UserDefinedFunction to call
* @param {...module:query~ExprArg} args
* A series of values to pass as arguments to the UDF.
* @return {Expr}
* */
function Call(ref) {
arity.min(1, arguments, Call.name)
var args = argsToArray(arguments)
args.shift()
return new Expr({ call: wrap(ref), arguments: wrap(varargs(args)) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#basic-forms).
*
* Constructs a `@query` type using the Lambda() or a function.
*
* ```
* Query(Lambda(['a', 'b'], Add(Var('a'), Var('b'))))
* Query(function (a, b) { return Add(a, b) })
* ```
*
* @param {module:query~ExprArg|function} lambda
* A function to escape as a query.
* @return {Expr}
* */
function Query(lambda) {
arity.exact(1, arguments, Query.name)
return new Expr({ query: wrap(lambda) })
}
// Collection functions
/** See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} collection
* An expression resulting in a collection to be mapped over.
* @param {module:query~ExprArg|function} lambda
* A function to be called for each element of the collection.
* @return {Expr}
* */
function Map(collection, lambda_expr) {
arity.exact(2, arguments, Map.name)
return new Expr({ map: wrap(lambda_expr), collection: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} collection
* An expression resulting in a collection to be iterated over.
* @param {module:query~ExprArg|function} lambda
* A function to be called for each element of the collection.
* @return {Expr}
* */
function Foreach(collection, lambda_expr) {
arity.exact(2, arguments, Foreach.name)
return new Expr({ foreach: wrap(lambda_expr), collection: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} collection
* An expression resulting in a collection to be filtered.
* @param {module:query~ExprArg|function} lambda
* A function that returns a boolean used to filter unwanted values.
* @return {Expr}
* */
function Filter(collection, lambda_expr) {
arity.exact(2, arguments, Filter.name)
return new Expr({ filter: wrap(lambda_expr), collection: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} number
* An expression resulting in the number of elements to take from the collection.
* @param {module:query~ExprArg} collection
* An expression resulting in a collection.
* @return {Expr}
* */
function Take(number, collection) {
arity.exact(2, arguments, Take.name)
return new Expr({ take: wrap(number), collection: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} number
* An expression resulting in the number of elements to drop from the collection.
* @param {module:query~ExprArg} collection
* An expression resulting in a collection.
* @return {Expr}
* */
function Drop(number, collection) {
arity.exact(2, arguments, Drop.name)
return new Expr({ drop: wrap(number), collection: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} elements
* An expression resulting in a collection of elements to prepend to the given collection.
* @param {module:query~ExprArg} collection
* An expression resulting in a collection.
* @return {Expr}
*/
function Prepend(elements, collection) {
arity.exact(2, arguments, Prepend.name)
return new Expr({ prepend: wrap(elements), collection: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} elements
* An expression resulting in a collection of elements to append to the given collection.
* @param {module:query~ExprArg} collection
* An expression resulting in a collection.
* @return {Expr}
*/
function Append(elements, collection) {
arity.exact(2, arguments, Append.name)
return new Expr({ append: wrap(elements), collection: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} collection
* An expression resulting in a collection.
* @return {Expr}
*/
function IsEmpty(collection) {
arity.exact(1, arguments, IsEmpty.name)
return new Expr({ is_empty: wrap(collection) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#collections).
*
* @param {module:query~ExprArg} collection
* An expression resulting in a collection.
* @return {Expr}
*/
function IsNonEmpty(collection) {
arity.exact(1, arguments, IsNonEmpty.name)
return new Expr({ is_nonempty: wrap(collection) })
}
// Type check functions
/**
* Check if the expression is a number.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isnumber">IsNumber</a>
*/
function IsNumber(expr) {
arity.exact(1, arguments, IsNumber.name)
return new Expr({ is_number: wrap(expr) })
}
/**
* Check if the expression is a double.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isdouble">IsDouble</a>
*/
function IsDouble(expr) {
arity.exact(1, arguments, IsDouble.name)
return new Expr({ is_double: wrap(expr) })
}
/**
* Check if the expression is an integer.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isinteger">IsInteger</a>
*/
function IsInteger(expr) {
arity.exact(1, arguments, IsInteger.name)
return new Expr({ is_integer: wrap(expr) })
}
/**
* Check if the expression is a boolean.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isboolean">IsBoolean</a>
*/
function IsBoolean(expr) {
arity.exact(1, arguments, IsBoolean.name)
return new Expr({ is_boolean: wrap(expr) })
}
/**
* Check if the expression is null.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isnull">IsNull</a>
*/
function IsNull(expr) {
arity.exact(1, arguments, IsNull.name)
return new Expr({ is_null: wrap(expr) })
}
/**
* Check if the expression is a byte array.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isbytes">IsBytes</a>
*/
function IsBytes(expr) {
arity.exact(1, arguments, IsBytes.name)
return new Expr({ is_bytes: wrap(expr) })
}
/**
* Check if the expression is a timestamp.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/istimestamp">IsTimestamp</a>
*/
function IsTimestamp(expr) {
arity.exact(1, arguments, IsTimestamp.name)
return new Expr({ is_timestamp: wrap(expr) })
}
/**
* Check if the expression is a date.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isdate">IsDate</a>
*/
function IsDate(expr) {
arity.exact(1, arguments, IsDate.name)
return new Expr({ is_date: wrap(expr) })
}
/**
* Check if the expression is a string.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isstring">IsString</a>
*/
function IsString(expr) {
arity.exact(1, arguments, IsString.name)
return new Expr({ is_string: wrap(expr) })
}
/**
* Check if the expression is an array.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isarray">IsArray</a>
*/
function IsArray(expr) {
arity.exact(1, arguments, IsArray.name)
return new Expr({ is_array: wrap(expr) })
}
/**
* Check if the expression is an object.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isobject">IsObject</a>
*/
function IsObject(expr) {
arity.exact(1, arguments, IsObject.name)
return new Expr({ is_object: wrap(expr) })
}
/**
* Check if the expression is a reference.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isref">IsRef</a>
*/
function IsRef(expr) {
arity.exact(1, arguments, IsRef.name)
return new Expr({ is_ref: wrap(expr) })
}
/**
* Check if the expression is a set.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isset">IsSet</a>
*/
function IsSet(expr) {
arity.exact(1, arguments, IsSet.name)
return new Expr({ is_set: wrap(expr) })
}
/**
* Check if the expression is a document (either a reference or an instance).
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isdoc">IsDoc</a>
*/
function IsDoc(expr) {
arity.exact(1, arguments, IsDoc.name)
return new Expr({ is_doc: wrap(expr) })
}
/**
* Check if the expression is a lambda.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/islambda">IsLambda</a>
*/
function IsLambda(expr) {
arity.exact(1, arguments, IsLambda.name)
return new Expr({ is_lambda: wrap(expr) })
}
/**
* Check if the expression is a collection.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/iscollection">IsCollection</a>
*/
function IsCollection(expr) {
arity.exact(1, arguments, IsCollection.name)
return new Expr({ is_collection: wrap(expr) })
}
/**
* Check if the expression is a database.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isdatabase">IsDatabase</a>
*/
function IsDatabase(expr) {
arity.exact(1, arguments, IsDatabase.name)
return new Expr({ is_database: wrap(expr) })
}
/**
* Check if the expression is an index.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isindex">IsIndex</a>
*/
function IsIndex(expr) {
arity.exact(1, arguments, IsIndex.name)
return new Expr({ is_index: wrap(expr) })
}
/**
* Check if the expression is a function.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isfunction">IsFunction</a>
*/
function IsFunction(expr) {
arity.exact(1, arguments, IsFunction.name)
return new Expr({ is_function: wrap(expr) })
}
/**
* Check if the expression is a key.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/iskey">IsKey</a>
*/
function IsKey(expr) {
arity.exact(1, arguments, IsKey.name)
return new Expr({ is_key: wrap(expr) })
}
/**
* Check if the expression is a token.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/istoken">IsToken</a>
*/
function IsToken(expr) {
arity.exact(1, arguments, IsToken.name)
return new Expr({ is_token: wrap(expr) })
}
/**
* Check if the expression is credentials.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/iscredentials">IsCredentials</a>
*/
function IsCredentials(expr) {
arity.exact(1, arguments, IsCredentials.name)
return new Expr({ is_credentials: wrap(expr) })
}
/**
* Check if the expression is a role.
*
* @param {module:query~ExprArg} expr
* The expression to check
* @return {Expr}
* @see <a href="https://docs.fauna.com/fauna/current/api/fql/functions/isrole">IsRole</a>
*/
function IsRole(expr) {
arity.exact(1, arguments, IsRole.name)
return new Expr({ is_role: wrap(expr) })
}
// Read functions
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#read-functions).
*
* @param {module:query~ExprArg} ref
* An expression resulting in either a Ref or SetRef.
* @param {?module:query~ExprArg} ts
* The snapshot time at which to get the document.
* @return {Expr}
*/
function Get(ref, ts) {
arity.between(1, 2, arguments, Get.name)
ts = util.defaults(ts, null)
return new Expr(params({ get: wrap(ref) }, { ts: wrap(ts) }))
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#read-functions).
*
* @param {module:query~ExprArg} secret
* The key or token secret to lookup.
* @return {Expr}
*/
function KeyFromSecret(secret) {
arity.exact(1, arguments, KeyFromSecret.name)
return new Expr({ key_from_secret: wrap(secret) })
}
/**
* See the [docs](https://docs.fauna.com/fauna/current/api/fql/functions/reduce).
*
* @param {module:query~ExprArg} lambda
* The accumulator function
* @param {module:query~ExprArg} initial
* The initial value
* @param {module:query~ExprArg} collection
* The colleciton to be reduced
* @return {Expr}
*/
function Reduce(lambda, initial, collection) {
arity.exact(3, arguments, Reduce.name)
return new Expr({
reduce: wrap(lambda),
initial: wrap(initial),
collection: wrap(collection),
})
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#read-functions).
* You may want to utilize {@link Client#paginate} to obtain a {@link PageHelper},
* rather than using this query function directly.
*
* @param {module:query~ExprArg} set
* An expression resulting in a SetRef to page over.
* @param {?Object} opts
* An object representing options for pagination.
* - size: Maximum number of results to return.
* - after: Return the next page of results after this cursor (inclusive).
* - before: Return the previous page of results before this cursor (exclusive).
* - sources: If true, include the source sets along with each element.
* @return {Expr}
*/
function Paginate(set, opts) {
arity.between(1, 2, arguments, Paginate.name)
opts = util.defaults(opts, {})
return new Expr(objectAssign({ paginate: wrap(set) }, wrapValues(opts)))
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#read-functions).
*
* @param {module:query~ExprArg} ref
* An expression resulting in a Ref.
* @param {?module:query~ExprArg} ts
* The snapshot time at which to check for the document's existence.
* @return {Expr}
*/
function Exists(ref, ts) {
arity.between(1, 2, arguments, Exists.name)
ts = util.defaults(ts, null)
return new Expr(params({ exists: wrap(ref) }, { ts: wrap(ts) }))
}
// Write functions
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} ref
* The Ref (usually a CollectionRef) to create.
* @param {?module:query~ExprArg} params
* An object representing the parameters of the document.
* @return {Expr}
*/
function Create(collection_ref, params) {
arity.between(1, 2, arguments, Create.name)
return new Expr({ create: wrap(collection_ref), params: wrap(params) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} ref
* The Ref to update.
* @param {module:query~ExprArg} params
* An object representing the parameters of the document.
* @return {Expr}
*/
function Update(ref, params) {
arity.exact(2, arguments, Update.name)
return new Expr({ update: wrap(ref), params: wrap(params) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} ref
* The Ref to replace.
* @param {module:query~ExprArg} params
* An object representing the parameters of the document.
* @return {Expr}
*/
function Replace(ref, params) {
arity.exact(2, arguments, Replace.name)
return new Expr({ replace: wrap(ref), params: wrap(params) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} ref
* The Ref to delete.
* @return {Expr}
*/
function Delete(ref) {
arity.exact(1, arguments, Delete.name)
return new Expr({ delete: wrap(ref) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} ref
* The Ref to insert against
* @param {module:query~ExprArg} ts
* The valid time of the inserted event
* @param {module:query~ExprArg} action
* Whether the event should be a Create, Update, or Delete.
* @param {module:query~ExprArg} params
* If this is a Create or Update, the parameters of the document.
* @return {Expr}
*/
function Insert(ref, ts, action, params) {
arity.exact(4, arguments, Insert.name)
return new Expr({
insert: wrap(ref),
ts: wrap(ts),
action: wrap(action),
params: wrap(params),
})
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} ref
* The Ref of the document whose event should be removed.
* @param {module:query~ExprArg} ts
* The valid time of the event.
* @param {module:query~ExprArg} action
* The event action (Create, Update, or Delete) that should be removed.
* @return {Expr}
*/
function Remove(ref, ts, action) {
arity.exact(3, arguments, Remove.name)
return new Expr({ remove: wrap(ref), ts: wrap(ts), action: wrap(action) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} params
* An object of parameters used to create a class.
* - name (required): the name of the class to create
* @return {Expr}
*
* @deprecated use CreateCollection instead
*/
function CreateClass(params) {
arity.exact(1, arguments, CreateClass.name)
return new Expr({ create_class: wrap(params) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} params
* An object of parameters used to create a collection.
* - name (required): the name of the collection to create
* @return {Expr}
*/
function CreateCollection(params) {
arity.exact(1, arguments, CreateCollection.name)
return new Expr({ create_collection: wrap(params) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} params
* An object of parameters used to create a database.
* - name (required): the name of the database to create
* @return {Expr}
*/
function CreateDatabase(params) {
arity.exact(1, arguments, CreateDatabase.name)
return new Expr({ create_database: wrap(params) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} params
* An object of parameters used to create an index.
* - name (required): the name of the index to create
* - source: One or more source objects describing source collections and (optional) field bindings.
* - terms: An array of term objects describing the fields to be indexed. Optional
* - values: An array of value objects describing the fields to be covered. Optional
* - unique: If true, maintains a uniqueness constraint on combined terms and values. Optional
* - partitions: The number of sub-partitions within each term. Optional
* @return {Expr}
*/
function CreateIndex(params) {
arity.exact(1, arguments, CreateIndex.name)
return new Expr({ create_index: wrap(params) })
}
/**
* See the [docs](https://app.fauna.com/documentation/reference/queryapi#write-functions).
*
* @param {module:query~ExprArg} params
* An object of parameters used to create a new key
* - database: Ref of the database the key will be scoped to. Optional.
* - role: The role of the new key
* @return {Expr}
*/
function CreateKey(params) {
arity.exact(1, arguments, CreateKey.name)
return new Expr({ create_key: wrap(params) })
}