-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.ts
executable file
·1850 lines (1657 loc) · 54.6 KB
/
stack.ts
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
/*!
* Contentstack DataSync Filesystem SDK.
* Enables querying on contents saved via @contentstack/datasync-content-store-filesystem
* Copyright (c) Contentstack LLC
* MIT Licensed
*/
import mask from 'json-mask'
import {
merge,
reverse,
sortBy,
} from 'lodash'
import sift from 'sift'
import {
existsSync,
readFile,
} from './fs'
import {
difference,
// doNothingClause,
getAssetsPath,
getContentTypesPath,
getEntriesPath,
segregateQueries,
} from './utils'
interface IShelf {
path: string,
position: string,
uid: string,
}
interface IQuery {
$or: Array < {
_content_type_uid: string,
_version?: {
$exists: boolean,
},
uid: string,
// Since collection name deterines the locale
locale?: string,
} >
}
const extend = {
compare(type) {
return function(key, value) {
if (typeof key === 'string' && typeof value !== 'undefined') {
this.q.query = this.q.query || {}
this.q.query[key] = this.q.query[key] || {}
this.q.query[key][type] = value
return this
}
throw new Error(`Kindly provide valid parameters for ${type}`)
}
},
contained(bool) {
const type = (bool) ? '$in' : '$nin'
return function(key, value) {
if (typeof key === 'string' && typeof value === 'object' && Array.isArray(value)) {
this.q.query = this.q.query || {}
this.q.query[key] = this.q.query[key] || {}
this.q.query[key][type] = this.q.query[key][type] || []
this.q.query[key][type] = this.q.query[key][type].concat(value)
return this
}
throw new Error(`Kindly provide valid parameters for ${bool}`)
}
},
exists(bool) {
return function(key) {
if (key && typeof key === 'string') {
this.q.query = this.q.query || {}
this.q.query[key] = this.q.query[key] || {}
this.q.query[key].$exists = bool
return this
}
throw new Error(`Kindly provide valid parameters for ${bool}`)
}
},
// TODO
logical(type) {
return function(query) {
this.q.query = this.q.query || {}
this.q.query[type] = query
return this
}
},
sort(type) {
return function(key) {
if (key && typeof key === 'string') {
this.q[type] = key
return this
}
throw new Error(`Kindly provide valid parameters for sort-${type}`)
}
},
pagination(type) {
return function(value) {
if (typeof value === 'number') {
this.q[type] = value
return this
}
throw new Error('Argument should be a number.')
}
},
}
/**
* @summary
* Expose SDK query methods on Stack
* @returns {this} - Returns `stack's` instance
*/
export class Stack {
// TODO
public config: any
public readonly contentStore: any
public readonly types: any
public readonly projections: string[]
public readonly q: any
public readonly lessThan: (key: string, value: any) => Stack
public readonly lessThanOrEqualTo: (key: string, value: any) => Stack
public readonly greaterThan: (key: string, value: any) => Stack
public readonly greaterThanOrEqualTo: (key: string, value: any) => Stack
public readonly notEqualTo: (key: string, value: any) => Stack
public readonly containedIn: (key: string, value: any) => Stack
public readonly notContainedIn: (key: string, value: any) => Stack
public readonly exists: (key: string) => Stack
public readonly notExists: (key: string) => Stack
public readonly ascending: (key: string) => Stack
public readonly descending: (key: string) => Stack
public readonly skip: (value: any) => Stack
public readonly limit: (value: any) => Stack
public readonly or: (query: any) => Stack
public readonly nor: (query: any) => Stack
public readonly not: (query: any) => Stack
public readonly and: (query: any) => Stack
constructor(config) {
// app config
this.config = config
this.contentStore = config.contentStore
this.projections = Object.keys(this.contentStore.projections)
this.types = config.contentStore.internal.types
this.q = this.q || {}
this.q.query = this.q.query || {}
/**
* @public
* @method lessThan
* @description Retrieves entries in which the value of a field is lesser than the provided value
* @param {String} key - uid of the field
* @param {*} value - Value used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.lessThan('created_at','2015-06-22').find()
* data.then((result) => {
* // result content the data who's 'created_at date'
* // is less than '2015-06-22'
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.lessThan = extend.compare('$lt')
/**
* @public
* @method lessThanOrEqualTo
* @description Retrieves entries in which the value of a field is lesser than or equal to the provided value.
* @param {String} key - uid of the field
* @param {*} value - Value used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.lessThanOrEqualTo('created_at','2015-06-22').find()
* data.then((result) => {
* // result contain the data of entries where the
* //'created_at' date will be less than or equalto '2015-06-22'.
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.lessThanOrEqualTo = extend.compare('$lte')
/**
* @public
* @method greaterThan
* @description Retrieves entries in which the value for a field is greater than the provided value.
* @param {String} key - uid of the field
* @param {*} value - value used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.greaterThan('created_at','2015-03-12').find()
* data.then((result) => {
* // result contains the data of entries where the
* //'created_at' date will be greaterthan '2015-06-22'
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.greaterThan = extend.compare('$gt')
/**
* @public
* @method greaterThanOrEqualTo
* @description Retrieves entries in which the value for a field is greater than or equal to the provided value.
* @param {String} key - uid of the field
* @param {*} value - Value used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.greaterThanOrEqualTo('created_at','2015-03-12').find()
* data.then((result) => {
* // result contains the data of entries where the
* //'created_at' date will be greaterThan or equalto '2015-06-22'
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.greaterThanOrEqualTo = extend.compare('$gte')
/**
* @public
* @method notEqualTo
* @description Retrieves entries in which the value for a field does not match the provided value.
* @param {String} key - uid of the field
* @param {*} value - Value used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.notEqualTo('title','Demo').find()
* data.then((result) => {
* // ‘result’ contains the list of entries where value
* // of the ‘title’ field will not be 'Demo'.
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.notEqualTo = extend.compare('$ne')
/**
* @public
* @method containedIn
* @description Retrieve entries in which the value of a field matches with any of the provided array of values
* @param {String} key - uid of the field
* @param {*} value - Array of values that are to be used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries().query();
* let data = blogQuery.containedIn('title', ['Demo', 'Welcome']).find()
* data.then((result) => {
* // ‘result’ contains the list of entries where value of the
* // ‘title’ field will contain either 'Demo' or ‘Welcome’.
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.containedIn = extend.contained(true)
/**
* @public
* @method notContainedIn
* @description Retrieve entries in which the value of a field does not match
* with any of the provided array of values.
* @param {String} key - uid of the field
* @param {Array} value - Array of values that are to be used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.notContainedIn('title', ['Demo', 'Welcome']).find()
* data.then((result) => {
* // 'result' contains the list of entries where value of the
* //title field should not be either "Demo" or ‘Welcome’
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.notContainedIn = extend.contained(false)
/**
* @public
* @method exists
* @description Retrieve entries if value of the field, mentioned in the condition, exists.
* @param {String} key - uid of the field
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.exists('featured').find()
* data.then((result) => {
* // ‘result’ contains the list of entries in which "featured" exists.
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.exists = extend.exists(true)
/**
* @public
* @method notExists
* @description Retrieve entries if value of the field, mentioned in the condition, does not exists.
* @param {String} key - uid of the field
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.notExists('featured').find()
* data.then((result) => {
* // result is the list of non-existing’featured’" data.
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.notExists = extend.exists(false)
/**
* @public
* @method ascending
* @description Sort fetched entries in the ascending order with respect to a specific field.
* @param {String} key - field uid based on which the ordering will be done
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.ascending('created_at').find()
* data.then((result) => {
* // ‘result’ contains the list of entries which is sorted in
* //ascending order on the basis of ‘created_at’.
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.ascending = extend.sort('asc')
/**
* @public
* @method descending
* @description Sort fetched entries in the descending order with respect to a specific field
* @param {String} key - field uid based on which the ordering will be done.
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.descending('created_at').find()
* data.then((result) => {
* // ‘result’ contains the list of entries which is sorted in
* //descending order on the basis of ‘created_at’.
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.descending = extend.sort('desc')
/**
* @public
* @method skip
* @description Skips at specific number of entries.
* @param {Number} skip - number of entries to be skipped
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.skip(5).find()
* data.then((result) => {
* //result
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.skip = extend.pagination('skip')
/**
* @public
* @method limit
* @description Returns a specific number of entries based on the set limit
* @param {Number} limit - maximum number of entries to be returned
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.limit(10).find()
* data.then((result) => {
* // result contains the limited number of entries
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
this.limit = extend.pagination('limit')
/**
* @public
* @method or
* @description Retrieves entries that satisfy at least one of the given conditions
* @param {object} queries - array of Query objects or raw queries
* @example
* let Query1 = Stack.contentType('example').entries().equalTo('title', 'Demo').find()
* let Query2 = Stack.contentType('example').entries().lessThan('comments', 10).find()
* blogQuery.or(Query1, Query2).find()
* @returns {this} - Returns `stack's` instance
*/
this.or = extend.logical('$or')
this.nor = extend.logical('$nor')
this.not = extend.logical('$not')
/**
* @public
* @method and
* @description Retrieve entries that satisfy all the provided conditions.
* @param {object} queries - array of query objects or raw queries.
* @example
* let Query1 = Stack.contentType('example').entries().equalTo('title', 'Demo')
* let Query2 = Stack.contentType('example').entries().lessThan('comments', 10)
* blogQuery.and(Query1, Query2).find()
* @returns {this} - Returns `stack's` instance
*/
this.and = extend.logical('$and')
}
/**
* TODO
* @public
* @method connect
* @summary
* Establish connection to filesytem
* @param {Object} overrides - Config overrides/flesystem specific config
* @example
* Stack.connect({overrides})
* .then((result) => {
* // db instance
* })
* .catch((error) => {
* // handle query errors
* })
*
* @returns {string} baseDir
*/
public connect(overrides: any = {}) {
this.config = merge(this.config, overrides)
return Promise.resolve(this.config)
}
/**
* @public
* @method contentType
* @summary
* Content type to query on
* @param {String} uid - Content type uid
* @returns {this} - Returns `stack's` instance
* @example
* Stack.contentType('example').entries().find()
* .then((result) => {
* // returns entries filtered based on 'example' content type
* })
* .catch((error) => {
* // handle query errors
* })
*
* @returns {Stack} instance
*/
public contentType(uid) {
if (typeof uid !== 'string' || uid.length === 0) {
throw new Error('Kindly provide a uid for .contentType()')
}
const stack = new Stack(this.config)
stack.q.content_type_uid = uid
return stack
}
/**
* @public
* @method entries
* @summary
* To get entries from contentType
* @example
* Stack.contentType('example')
* .entries()
* .find()
* @returns {this} - Returns `stack's` instance
*/
public entries() {
if (typeof this.q.content_type_uid === 'undefined') {
throw new Error('Please call .contentType() before calling .entries()!')
}
return this
}
/**
* @public
* @method entry
* @summary
* To get entry from contentType
* @example
* Stack.contentType('example').entry('bltabcd12345').find()
* //or
* Stack.contentType('example').entry().find()
* @param {string} uid- Optional. uid of entry
* @returns {this} - Returns `stack's` instance
*/
public entry(uid ? ) {
this.q.isSingle = true
if (typeof this.q.content_type_uid === 'undefined') {
throw new Error('Please call .contentType() before calling .entries()!')
}
if (uid && typeof uid === 'string') {
this.q.query.uid = uid
}
return this
}
/**
* @public
* @method asset
* @summary
* To get single asset
* @example
* Stack.asset('bltabced12435').find()
* //or
* Stack.asset().find()
* @param {string} uid- Optional. uid of asset
* @returns {this} - Returns `stack's` instance
*/
public asset(uid ? ) {
const stack = new Stack(this.config)
stack.q.isSingle = true
stack.q.content_type_uid = stack.types.assets
if (uid && typeof uid === 'string') {
stack.q.query.uid = uid
}
return stack
}
/**
* @public
* @method assets
* @summary Get assets details
* @example
* Stack.assets().find()
*
* @returns {this} - Returns `stack's` instance
*/
public assets() {
const stack = new Stack(this.config)
stack.q.content_type_uid = stack.types.assets
return stack
}
/**
* @public
* @method schemas
* @summary Get content type schemas
* @example
* Stack.schemas().find()
*
* @returns {this} - Returns `stack's` instance
*/
public schemas() {
const stack = new Stack(this.config)
stack.q.content_type_uid = stack.types.content_types
return stack
}
/**
* @public
* @method contentTypes
* @summary Get content type schemas
* @example
* Stack.contentTypes().find()
*
* @returns {this} - Returns `stack's` instance
*/
public contentTypes() {
const stack = new Stack(this.config)
stack.q.content_type_uid = stack.types.content_types
return stack
}
/**
* @public
* @method schema
* @summary Get a single content type's schema
* @param {String} uid - Optional 'uid' of the content type, who's schema is to be fetched
* @example
* Stack.schema(uid?: string).find()
*
* @returns {this} - Returns `stack's` instance
*/
public schema(uid?: string) {
const stack = new Stack(this.config)
stack.q.isSingle = true
stack.q.content_type_uid = stack.types.content_types
if (uid && typeof uid === 'string') {
stack.q.query.uid = uid
}
return stack
}
/**
* @public
* @method equalTo
* @description Retrieve entries in which a specific field satisfies the value provided
* @param {String} key - uid of the field
* @param {Any} value - value used to match or compare
* @example
* let blogQuery = Stack.contentType('example').entries()
* let data = blogQuery.equalTo('title','Demo').find()
* data.then((result) => {
* // ‘result’ contains the list of entries where value of
* //‘title’ is equal to ‘Demo’.
* }).catch((error) => {
* // error trace
* })
*
* @returns {this} - Returns `stack's` instance
*/
public equalTo(key, value) {
if (!key || typeof key !== 'string' && typeof value === 'undefined') {
throw new Error('Kindly provide valid parameters for .equalTo()!')
}
this.q.query[key] = value
return this
}
/**
* @public
* @method where
* @summary Pass JS expression or a full function to the query system
* @description Evaluate js expressions
* @param field
* @param value
*
* @example
* const query = Stack.contentType('example').entries().where("this.title === 'Amazon_Echo_Black'").find()
* query.then((result) => {
* // ‘result’ contains the list of entries where value of
* //‘title’ is equal to ‘Demo’.
* }).catch(error) => {
* // error trace
* })
*
* @returns {this} - Returns `stack's` instance
*/
public where(expr) {
if (!expr) {
throw new Error('Kindly provide a valid field and expr/fn value for \'.where()\'')
}
this.q.query.$where = expr
return this
}
/**
* @public
* @method count
* @description Returns the total number of entries
* @example
* const query = Stack.contentType('example').entries().count().find()
* query.then((result) => {
* // returns 'example' content type's entries
* }).catch(error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
public count() {
this.q.countOnly = 'count'
return this.find()
}
/**
* @public
* @method query
* @description Retrieve entries based on raw queries
* @param {object} userQuery - RAW (JSON) queries
* @returns {this} - Returns `stack's` instance
* @example
* Stack.contentType('example').entries().query({"authors.name": "John Doe"}).find()
* .then((result) => {
* // returns entries, who's reference author's name equals "John Doe"
* })
* .catch((error) => {
* // handle query errors
* })
*/
public query(userQuery) {
if (!userQuery || typeof userQuery !== 'object') {
throw new Error('Kindly provide valid parameters for \'.query()\'')
}
this.q.query = merge(this.q.query, userQuery)
return this
}
/**
* @public
* @method tags
* @description Retrieves entries based on the provided tags
* @param {Array} values - Entries/Assets that have the specified tags
* @example
* const query = Stack.contentType('example').entries().tags(['technology', 'business']).find()
* query.then((result) => {
* // ‘result’ contains list of entries which have tags "’technology’" and ‘"business’".
* }).catch((error) => {
* // error trace
* })
* @returns {this} - Returns `stack's` instance
*/
public tags(values) {
if (values && typeof values === 'object' && values instanceof Array) {
if (values.length === 0) {
this.q.query.tags = {
$size: 0,
}
} else {
this.q.query.tags = {
$in: values,
}
}
return this
}
throw new Error('Kindly provide valid parameters for \'.tags()\'')
}
/**
* @public
* @method includeCount
* @description Includes the total number of entries returned in the response.
* @example
* Stack.contentType('example')
* .entries()
* .includeCount()
* .find()
* @returns {this} - Returns `stack's` instance
*/
public includeCount() {
this.q.includeCount = true
return this
}
/**
* @public
* @method language
* @description to retrive the result bsed on the specific locale.
* @param {String} languageCode - Language to query on
* @example
* Stack.contentType('example')
* .entries()
* .language('fr-fr')
* .find()
*
* @returns {this} - Returns `stack's` instance
*/
public language(languageCode) {
if (!languageCode || typeof languageCode !== 'string') {
throw new Error(`${languageCode} should be of type string and non-empty!`)
}
this.q.locale = languageCode
return this
}
/**
* @public
* @method include
* @summary
* Includes references of provided fields of the entries being scanned
* @param {*} key - uid/uid's of the field
*
* @example
* Stack.contentType('example')
* .entries()
* .include(['authors','categories'])
* .find()
*
* @returns {this} - Returns `stack's` instance
*/
public include(fields) {
if (fields && typeof fields === 'object' && fields instanceof Array && fields.length) {
this.q.includeSpecificReferences = fields
} else if (fields && typeof fields === 'string') {
this.q.includeSpecificReferences = [fields]
} else {
throw new Error('Kindly pass \'string\' OR \'array\' fields for .include()!')
}
return this
}
/**
* @public
* @method includeReferences
* @summary
* Includes all references of the entries being scanned
* @param {number} depth - Optional parameter. Use this to override the default reference depth/level i.e. 4
*
* @example
* Stack.contentType('example')
* .entries()
* .includeReferences()
* .find()
*
* @returns {this} - Returns `stack's` instance
*/
public includeReferences(depth?: number) {
console.warn('.includeReferences() is a relatively slow query..!')
this.q.includeAllReferences = true
if (typeof depth === 'number') {
this.q.referenceDepth = depth
}
return this
}
/**
* @public
* @method excludeReferences
* @summary
* Excludes all references of the entries being scanned
*
* @example
* Stack.contentType('example')
* .entries()
* .excludeReferences()
* .find()
* .then((result) => {
* // ‘result’ entries without references
* }).catch((error) => {
* // error trace
* })
*
* @returns {this} - Returns `stack's` instance
*/
public excludeReferences() {
this.q.excludeAllReferences = true
return this
}
/**
* @public
* @method includeContentType
* @description Includes the total number of entries returned in the response.
* @example
* Stack.contentType('example')
* .entries()
* .includeContentType()
* .find()
* .then((result) => {
* // Expected result
* {
* entries: [
* {
* ...,
* },
* ],
* content_type_uid: 'example',
* locale: 'en-us',
* content_type: {
* ..., // Content type example's schema
* }
* }
* }).catch((error) => {
* // error trace
* })
*
* @returns {this} - Returns `stack's` instance
*/
public includeContentType() {
this.q.include_content_type = true
return this
}
/**
* @public
* @method getQuery
* @description Returns the raw (JSON) query based on the filters applied on Query object.
* @example
* Stack.contentType('example')
* .eqaulTo('title','Demo')
* .getQuery()
* .find()
*
* @returns {this} - Returns `stack's` instance
*/
public getQuery() {
return this.q.query
}
/**
* @public
* @method regex
* @description Retrieve entries that match the provided regular expressions
* @param {String} key - uid of the field
* @param {*} value - value used to match or compare
* @param {String} [options] - match or compare value in entry
* @example
* let blogQuery = Stack.contentType('example').entries()
* blogQuery.regex('title','^Demo').find() //regex without options
* //or
* blogQuery.regex('title','^Demo', 'i').find() //regex without options
* @returns {this} - Returns `stack's` instance
*/
public regex(key, value, options = 'g') {
if (key && value && typeof key === 'string' && typeof value === 'string') {
this.q.query[key] = {
$options: options,
$regex: value,
}
return this
}
throw new Error('Kindly provide valid parameters for .regex()!')
}
/**
* @public
* @method only
* @description
* Similar to MongoDB projections. Accepts an array.
* Only fields mentioned in the array would be returned in the result.
* @param {Array} result - Array of field properties
* @example
* const query = Stack.contentType('example').entries().only(['title','uid']).find()
* query.then((result) => {
* // ‘result’ contains a list of entries with field title and uid only
* }).catch((error) => {
* // error trace
* })
*
* @returns {this} - Returns `stack's` instance
*/
public only(fields) {
if (fields && typeof fields === 'object' && fields instanceof Array && fields.length) {
this.q.only = fields
return this
}
throw new Error('Kindly provide valid parameters for .only()!')
}
/**
* @public
* @method except
* @description
* Similar to MongoDB projections. Accepts an array.
* Only fields mentioned in the array would be removed from the result.
* @param {Array} result - Array of field properties
* @example
* const query = Stack.contentType('example').entries().except(['title','uid']).find()
* query.then((result) => {
* // ‘result’ contains a list of entries with field title and uid only
* }).catch((error) => {
* // error trace
* })
*
* @returns {this} - Returns `stack's` instance
*/
public except(fields) {
if (fields && typeof fields === 'object' && fields instanceof Array && fields.length) {
this.q.except = []
const keys = Object.keys(this.contentStore.projections).filter(key => this.contentStore.projections[key] === 0)
this.q.except = keys.concat(fields)
return this
}
throw new Error('Kindly provide valid parameters for .except()!')
}
/**
* @public
* @method queryReferences
* @summary
* Wrapper, that allows querying on the entry's references.
* @note