forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules_query.js.html
866 lines (810 loc) · 38.1 KB
/
modules_query.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>modules/query.js - Documentation</title>
<link rel="shortcut icon" href="./favicon.ico">
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="canonical" href="https://www.contentstack.com/docs/developers/platforms/javascript/api-reference/">
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-WGP99J7');</script>
<!-- End Google Tag Manager -->
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WGP99J7" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Contentstack.html">Contentstack</a><ul class='methods'><li data-type='method'><a href="Contentstack.html#Stack">Stack</a></li></ul></li><li><a href="Stack.html">Stack</a><ul class='methods'><li data-type='method'><a href="Stack.html#setPort">setPort</a></li><li data-type='method'><a href="Stack.html#setProtocol">setProtocol</a></li><li data-type='method'><a href="Stack.html#setHost">setHost</a></li><li data-type='method'><a href="Stack.html#setCachePolicy">setCachePolicy</a></li><li data-type='method'><a href="Stack.html#setCacheProvider">setCacheProvider</a></li><li data-type='method'><a href="Stack.html#clearByQuery">clearByQuery</a></li><li data-type='method'><a href="Stack.html#clearByContentType">clearByContentType</a></li><li data-type='method'><a href="Stack.html#clearAll">clearAll</a></li><li data-type='method'><a href="Stack.html#getCacheProvider">getCacheProvider</a></li><li data-type='method'><a href="Stack.html#ContentType">ContentType</a></li><li data-type='method'><a href="Stack.html#Assets">Assets</a></li><li data-type='method'><a href="Stack.html#Query">Query</a></li><li data-type='method'><a href="Stack.html#getLastActivites">getLastActivites</a></li><li data-type='method'><a href="Stack.html#getContentTypes">getContentTypes</a></li><li data-type='method'><a href="Stack.html#sync">sync</a></li><li data-type='method'><a href="Stack.html#imageTransform">imageTransform</a></li></ul></li><li><a href="Assets.html">Assets</a><ul class='methods'><li data-type='method'><a href="Assets.html#toJSON">toJSON</a></li><li data-type='method'><a href="Assets.html#addParam">addParam</a></li><li data-type='method'><a href="Assets.html#fetch">fetch</a></li></ul></li><li><a href="Entry.html">Entry</a><ul class='methods'><li data-type='method'><a href="Entry.html#includeFallback">includeFallback</a></li><li data-type='method'><a href="Entry.html#only">only</a></li><li data-type='method'><a href="Entry.html#except">except</a></li><li data-type='method'><a href="Entry.html#includeReference">includeReference</a></li><li data-type='method'><a href="Entry.html#language">language</a></li><li data-type='method'><a href="Entry.html#addQuery">addQuery</a></li><li data-type='method'><a href="Entry.html#includeSchema">includeSchema</a></li><li data-type='method'><a href="Entry.html#includeReferenceContentTypeUid">includeReferenceContentTypeUid</a></li><li data-type='method'><a href="Entry.html#includeFallback">includeFallback</a></li><li data-type='method'><a href="Entry.html#includeContentType">includeContentType</a></li><li data-type='method'><a href="Entry.html#includeOwner">includeOwner</a></li><li data-type='method'><a href="Entry.html#toJSON">toJSON</a></li><li data-type='method'><a href="Entry.html#addParam">addParam</a></li><li data-type='method'><a href="Entry.html#fetch">fetch</a></li></ul></li><li><a href="Query.html">Query</a><ul class='methods'><li data-type='method'><a href="Query.html#lessThan">lessThan</a></li><li data-type='method'><a href="Query.html#lessThanOrEqualTo">lessThanOrEqualTo</a></li><li data-type='method'><a href="Query.html#greaterThan">greaterThan</a></li><li data-type='method'><a href="Query.html#greaterThanOrEqualTo">greaterThanOrEqualTo</a></li><li data-type='method'><a href="Query.html#notEqualTo">notEqualTo</a></li><li data-type='method'><a href="Query.html#containedIn">containedIn</a></li><li data-type='method'><a href="Query.html#notContainedIn">notContainedIn</a></li><li data-type='method'><a href="Query.html#exists">exists</a></li><li data-type='method'><a href="Query.html#notExists">notExists</a></li><li data-type='method'><a href="Query.html#ascending">ascending</a></li><li data-type='method'><a href="Query.html#descending">descending</a></li><li data-type='method'><a href="Query.html#beforeUid">beforeUid</a></li><li data-type='method'><a href="Query.html#afterUid">afterUid</a></li><li data-type='method'><a href="Query.html#skip">skip</a></li><li data-type='method'><a href="Query.html#limit">limit</a></li><li data-type='method'><a href="Query.html#or">or</a></li><li data-type='method'><a href="Query.html#and">and</a></li><li data-type='method'><a href="Query.html#where">where</a></li><li data-type='method'><a href="Query.html#count">count</a></li><li data-type='method'><a href="Query.html#query">query</a></li><li data-type='method'><a href="Query.html#referenceIn">referenceIn</a></li><li data-type='method'><a href="Query.html#referenceNotIn">referenceNotIn</a></li><li data-type='method'><a href="Query.html#tags">tags</a></li><li data-type='method'><a href="Query.html#includeReferenceContentTypeUid">includeReferenceContentTypeUid</a></li><li data-type='method'><a href="Query.html#includeCount">includeCount</a></li><li data-type='method'><a href="Query.html#addParam">addParam</a></li><li data-type='method'><a href="Query.html#getQuery">getQuery</a></li><li data-type='method'><a href="Query.html#regex">regex</a></li><li data-type='method'><a href="Query.html#search">search</a></li><li data-type='method'><a href="Query.html#find">find</a></li><li data-type='method'><a href="Query.html#findOne">findOne</a></li></ul></li><li><a href="Result.html">Result</a><ul class='methods'><li data-type='method'><a href="Result.html#toJSON">toJSON</a></li><li data-type='method'><a href="Result.html#get">get</a></li><li data-type='method'><a href="Result.html#getDownloadUrl">getDownloadUrl</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">modules/query.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import Request from '../lib/request';
import * as Utils from '../lib/utils.js';
import Entry from './entry';
const _extend = {
compare: function(type) {
return function(key, value) {
if (key && value && typeof key === 'string' && typeof value !== 'undefined') {
this._query['query'][key] = this._query['query']['file_size'] || {};
this._query['query'][key][type] = value;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
};
},
contained: function(bool) {
let type = (bool) ? '$in' : '$nin';
return function(key, value) {
if (key && value && typeof key === 'string' && Array.isArray(value)) {
this._query['query'][key] = this._query['query'][key] || {};
this._query['query'][key][type] = this._query['query'][key][type] || [];
this._query['query'][key][type] = this._query['query'][key][type].concat(value);
return this;
} else {
console.error("Kindly provide valid parameters.");
}
};
},
exists: function(bool) {
return function(key) {
if (key && typeof key === 'string') {
this._query['query'][key] = this._query['query'][key] || {};
this._query['query'][key]['$exists'] = bool;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
};
},
logical: function(type) {
return function() {
let _query = [];
for (let i = 0, _i = arguments.length; i < _i; i++) {
if (arguments[i] instanceof Query && arguments[i]._query.query) {
_query.push(arguments[i]._query.query);
} else if (typeof arguments[i] === "object") {
_query.push(arguments[i]);
}
}
if (this._query['query'][type]) {
this._query['query'][type] = this._query['query'][type].concat(_query);
} else {
this._query['query'][type] = _query;
}
return this;
};
},
sort: function(type) {
return function(key) {
if (key && typeof key === 'string') {
this._query[type] = key;
return this;
} else {
console.error("Argument should be a string.");
}
};
},
pagination: function(type) {
return function(value) {
if (typeof value === 'number') {
this._query[type] = value;
return this;
} else {
console.error("Argument should be a number.");
}
}
}
};
/**
* @class
Query
* @description
* An initializer is responsible for creating Query object.Provides support for all search queries
* @example
* <caption>Query instance creation.</caption>
* let Query = Contentstack.Stack().ContentType('example').Query();
* let assetQuery = Contentstack.Stack().Assets().Query();
* @returns {Query}
*/
export default class Query extends Entry {
constructor() {
super();
this._query = this._query || {};
this._query['query'] = this._query['query'] || {};
/**
* @method lessThan
* @memberOf Query
* @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').Query();
* let data = blogQuery.lessThan('created_at','2015-06-22').find()
* data.then(function (result) {
* // result content the data who's 'created_at date' is less than '2015-06-22'
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.lessThan = _extend.compare('$lt');
/**
* @method lessThanOrEqualTo
* @memberOf Query
* @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').Query();
* let data = blogQuery.lessThanOrEqualTo('created_at','2015-06-22').find()
* data.then(function (result) {
* // result contain the data of entries where the 'created_at' date will be less than or equalto '2015-06-22'.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.lessThanOrEqualTo = _extend.compare('$lte');
/**
* @method greaterThan
* @memberOf Query
* @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').Query();
* let data = blogQuery.greaterThan('created_at','2015-03-12').find()
* data.then(function(result) {
* // result contains the data of entries where the 'created_at' date will be greaterthan '2015-06-22'
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.greaterThan = _extend.compare('$gt');
/**
* @method greaterThanOrEqualTo
* @memberOf Query
* @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').Query();
* let data = blogQuery.greaterThanOrEqualTo('created_at','2015-03-12').find()
* data.then(function(result) {
* // result contains the data of entries where the 'created_at' date will be greaterThan or equalto '2015-06-22'
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.greaterThanOrEqualTo = _extend.compare('$gte');
/**
* @method notEqualTo
* @memberOf Query
* @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').Query();
* let data = blogQuery.notEqualTo('title','Demo').find()
* data.then(function(result) {
* // ‘result’ contains the list of entries where value of the ‘title’ field will not be 'Demo'.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.notEqualTo = _extend.compare('$ne');
/**
* @method containedIn
* @memberOf Query
* @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').Query();
* let data = blogQuery.containedIn('title', ['Demo', 'Welcome']).find()
* data.then(function(result) {
* // ‘result’ contains the list of entries where value of the ‘title’ field will contain either 'Demo' or ‘Welcome’.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.containedIn = _extend.contained(true);
/**
* @method notContainedIn
* @memberOf Query
* @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').Query();
* let data = blogQuery.notContainedIn('title', ['Demo', 'Welcome']).find()
* data.then(function(result) {
* // 'result' contains the list of entries where value of the title field should not be either "Demo" or ‘Welcome’
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.notContainedIn = _extend.contained(false);
/**
* @method exists
* @memberOf Query
* @description Retrieve entries if value of the field, mentioned in the condition, exists.
* @param {String} key - uid of the field
* @example blogQuery.exists('featured')
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.exists('featured').find()
* data.then(function(result) {
* // ‘result’ contains the list of entries in which "featured" exists.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.exists = _extend.exists(true);
/**
* @method notExists
* @memberOf Query
* @description Retrieve entries if value of the field, mentioned in the condition, does not exists.
* @param {String} key - uid of the field
* @example blogQuery.notExists('featured')
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.notExists('featured').find()
* data.then(function(result) {
* // result is the list of non-existing’featured’" data.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.notExists = _extend.exists(false);
/**
* @method ascending
* @memberOf Query
* @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').Query();
* let data = blogQuery.ascending('created_at').find()
* data.then(function(result) {
* // ‘result’ contains the list of entries which is sorted in ascending order on the basis of ‘created_at’.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.ascending = _extend.sort('asc');
/**
* @method descending
* @memberOf Query
* @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').Query();
* let data = blogQuery.descending('created_at').find()
* data.then(function(result) {
* // ‘result’ contains the list of entries which is sorted in descending order on the basis of ‘created_at’.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.descending = _extend.sort('desc');
/**
* @method beforeUid
* @memberOf Query
* @description Sort fetched entries in the descending order with respect to a specific field
* @param {String} uid - field uid based on which the ordering will be done.
* @example blogQuery.beforeUid('blt1234567890abcdef')
* @returns {Query}
* @instance
*/
this.beforeUid = _extend.sort('before_uid');
/**
* @method afterUid
* @memberOf Query
* @description This method provides only the entries after the specified entry id.
* @param {String} uid - uid of the entry
* @example blogQuery.afterUid('blt1234567890abcdef')
* @returns {Query}
* @instance
*/
this.afterUid = _extend.sort('after_uid');
/**
* @method skip
* @memberOf Query
* @description Skips at specific number of entries.
* @param {Number} skip - number of entries to be skipped
* @example blogQuery.skip(5)
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.skip(5).find()
* data.then(function(result) {
* // result contains the list of data which is sorted in descending order on 'created_at' bases.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.skip = _extend.pagination('skip');
/**
* @method limit
* @memberOf Query
* @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').Query();
* let data = blogQuery.limit(10).find()
* data.then(function(result) {
* // result contains the limited number of entries
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
this.limit = _extend.pagination('limit');
/**
* @method or
* @memberOf Query
* @description Retrieves entries that satisfy at least one of the given conditions
* @param {object} queries - array of Query objects or raw queries
* @example
* <caption> .or with Query instances</caption>
* let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo').find()
* let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10).find()
* blogQuery.or(Query1, Query2)
* @example
* <caption> .or with raw queries</caption>
* let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo').getQuery()
* let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10).getQuery()
* blogQuery.or(Query1, Query2)
* @returns {Query}
* @instance
*/
this.or = _extend.logical('$or');
/**
* @method and
* @memberOf Query
* @description Retrieve entries that satisfy all the provided conditions.
* @param {object} queries - array of query objects or raw queries.
* @example
* <caption> .and with Query instances</caption>
* let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo')
* let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10)
* blogQuery.and(Query1, Query2)
* @example
* <caption> .and with raw queries</caption>
* let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo').getQuery()
* let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10).getQuery()
* blogQuery.and(Query1, Query2)
* @returns {Query}
* @instance
*/
this.and = _extend.logical('$and');
}
equalTo(key, value) {
if (key && typeof key === 'string') {
this._query['query'][key] = value;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
}
/**
* @memberOf Query
* @description Retrieve entries in which a specific field satisfies the value provided
* @param {String} key - uid of the field
* @param {*} value - value used to match or compare
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.where('title','Demo').find()
* data.then(function(result) {
* // ‘result’ contains the list of entries where value of ‘title’ is equal to ‘Demo’.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
where(key, value) {
if (key && typeof key === 'string') {
this._query['query'][key] = value;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
}
/**
* @method count
* @memberOf Query
* @description Returns the total number of entries
* @example blogQuery.count()
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.count().find()
* data.then(function(result) {
* // ‘result’ contains the total count.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
count() {
const host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version,
url = (this.type && this.type === 'asset') ? host + this.config.urls.assets : host + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
this._query['count'] = true;
this.requestParams = {
method: 'POST',
headers: this.headers,
url: url,
body: {
_method: 'GET',
query: this._query
}
};
return this;
}
/**
* @method query
* @memberOf Query
* @description Retrieve entries based on raw queries
* @param {object} query - RAW (JSON) queries
* @returns {Query}
* @instance
* @example
* let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.query({"brand": {"$nin_query": {"title": "Apple Inc."}}}).find()
* data.then(function(result) {
* // ‘result’ contains the total count.
* },function (error) {
* // error function
* })
*/
query(query) {
if (typeof query === "object") {
this._query['query'] = Utils.mergeDeep(this._query['query'], query);
return this;
} else {
console.error("Kindly provide valid parameters");
}
}
/**
* @method referenceIn
* @memberOf Query
* @description Retrieve entries that satisfy the query conditions made on referenced fields.
* @param {Query} query - RAW (JSON) queries
* @returns {Query}
* @instance
* @example
* <caption> referenceIn with Query instances</caption>
* let blogQuery = Stack().ContentType('example').Query();
* let Query = Stack.ContentType('blog').Query().where('title', 'Demo')
* let data = blogQuery.referenceIn("brand", Query).find()
* data.then(function(result) {
* // ‘result’ contains the total count.
* },function (error) {
* // error function
* })
*
* @example
* <caption> referenceIn with raw queries</caption>
* let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.referenceIn("brand", {'title': 'Demo'}).find()
* data.then(function(result) {
* // ‘result’ contains the total count.
* },function (error) {
* // error function
* })
*/
referenceIn(key, query) {
var _query = {}
if (query instanceof Query && query._query.query) {
_query["$in_query"] = query._query.query;
} else if (typeof query === "object") {
_query["$in_query"] = query;
}
if (this._query['query'][key]) {
this._query['query'][key] = this._query['query'][key].concat(_query);
} else {
this._query['query'][key] = _query;
}
return this;
}
/**
* @method referenceNotIn
* @memberOf Query
* @description Retrieve entries that does not satisfy the query conditions made on referenced fields.
* @param {Query} query - RAW (JSON) queries
* @returns {Query}
* @instance
* @example
* <caption> referenceNotIn with Query instances</caption>
* let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.referenceNotIn("brand", {'title': 'Demo'}).find()
* data.then(function(result) {
* // ‘result’ contains the total count.
* },function (error) {
* // error function
* })
*
* @example
* <caption> referenceNotIn with raw queries</caption>
* let blogQuery = Stack().ContentType('example').Query();
* let Query = Stack.ContentType('blog').Query().where('title', 'Demo')
* let data = blogQuery.referenceNotIn("brand", Query).find()
* data.then(function(result) {
* // ‘result’ contains the total count.
* },function (error) {
* // error function
* })
*/
referenceNotIn(key, query) {
var _query = {}
if (query instanceof Query && query._query.query) {
_query["$nin_query"] = query._query.query;
} else if (typeof query === "object") {
_query["$nin_query"] = query;
}
if (this._query['query'][key]) {
this._query['query'][key] = this._query['query'][key].concat(_query);
} else {
this._query['query'][key] = _query;
}
return this;
}
/**
* @method tags
* @memberOf Query
* @description Retrieves entries based on the provided tags
* @param {Array} values - tags
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.tags(['technology', 'business']).find()
* data.then(function(result) {
* // ‘result’ contains list of entries which have tags "’technology’" and ‘"business’".
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
tags(values) {
if (Array.isArray(values)) {
this._query['tags'] = values;
return this;
} else {
console.error("Kindly provide valid parameters");
}
}
/**
* @method includeReferenceContentTypeUid
* @memberOf Query
* @description This method also includes the content type UIDs of the referenced entries returned in the response.
* @example Stack.ContentType("contentType_uid").Query().includeReferenceContentTypeUID().find()
* @example
* let blogQuery = Stack.ContentType("contentType_uid").Query();
* let data = blogQuery.includeReferenceContentTypeUID().find()
* data.then(function(result) {
* // ‘result’ contains a list of entries in which content type UIDs is present.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
includeReferenceContentTypeUID() {
this._query['include_reference_content_type_uid'] = true;
return this;
}
/**
* @method includeCount
* @memberOf Query
* @description Includes the total number of entries returned in the response.
* @example blogQuery.includeCount()
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.includeCount().find()
* data.then(function(result) {
* // ‘result’ contains a list of entries in which count of object is present at array[1] position.
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
includeCount() {
this._query['include_count'] = true;
return this;
}
/**
* @method addParam
* @description Includes query parameters in your queries.
* @memberOf Query
* @example var data = blogQuery.addParam('include_count', 'true').fetch()
* data.then(function (result) {
* // 'result' is an object which content the data including count in json object form
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
addParam(key, value) {
if (key && value && typeof key === 'string' && typeof value === 'string') {
this._query[key] = value;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
}
/**
* @method getQuery
* @memberOf Query
* @description Returns the raw (JSON) query based on the filters applied on Query object.
* @example Stack.ContentType('contentType_uid').Query().where('title','Demo').getQuery().find()
* @returns {Query}
* @instance
*/
getQuery() {
return this._query.query || {};
}
/**
* @method regex
* @memberOf Query
* @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
* <caption> .regex without options</caption>
* blogQuery.regex('title','^Demo')
* @example
* <caption> .regex with options</caption>
* blogQuery.regex('title','^Demo', 'i')
* @returns {Query}
* @instance
*/
regex(key, value, options) {
if (key && value && typeof key === 'string' && typeof value === 'string') {
this._query['query'][key] = {
$regex: value
};
if (options) this._query['query'][key]['$options'] = options;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
}
/**
* @method search
* @memberOf Query
* @description Retrieve entries that have fields which match the provided search value.
* @param {string} value - value to search in entries
* @example blogQuery.search('Welcome to demo')
* @example let blogQuery = Stack().ContentType('example').Query();
* let data = blogQuery.search('welcome to demo').find()
* data.then(function(result) {
* // ‘result’ contains the object that possess the text "’welcome to demo’".
* },function (error) {
* // error function
* })
* @returns {Query}
* @instance
*/
search(value) {
if (value && typeof value === 'string') {
this._query['typeahead'] = value;
return this;
} else {
console.error("Kindly provide valid parameters.");
}
}
/**
* @method find
* @memberOf Query
* @description Retrieves entries that satisfied the specified query
* @example let blogQuery = Stack().ContentType('example').Query().find();
* blogQuery.then(function(result) {
* // result contains the list of object.
* },function (error) {
* // error function
* })
* blogQuery.find()
* @example
* let blogQuery = Stack.ContentType(contentTypeUid).Query().find({
*
* });
* blogQuery.then(function(result) {
* // result contains the list of object.
* },function (error) {
* // error function
* })
* blogQuery.find()
* @returns {promise}
* @instance
*/
find(fetchOptions) {
const host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version,
url = (this.type && this.type === 'asset') ? host + this.config.urls.assets : host + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
this.requestParams = {
method: 'POST',
headers: this.headers,
url: url,
body: {
_method: 'GET',
query: this._query
}
};
var options = Object.assign({}, this.fetchOptions, fetchOptions);
return Utils.sendRequest(this, options);
}
/**
* @method findOne
* @memberOf Query
* @deprecated since verion 3.3.0
* @description Retrieve a single entry from the result
* @example let blogQuery = Stack().ContentType('example').Query().findOne();
* blogQuery.then(function(result) {
* // result contains the single item object.
* },function (error) {
* // error function
* })
* blogQuery.findOne()
* @returns {promise}
* @instance
*/
findOne() {
const host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version,
url = (this.type && this.type === 'asset') ? host + this.config.urls.assets : host + this.config.urls.content_types + this.content_type_uid + this.config.urls.entries;
this.singleEntry = true;
this._query.limit = 1;
this.requestParams = {
method: 'POST',
headers: this.headers,
url: url,
body: {
_method: 'GET',
query: this._query
}
};
var options = Object.assign({}, this.fetchOptions);
return Utils.sendRequest(this, options);
}
}</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
<div class="inner-content center">
<span class="copy-right"><a href="mailto:support@contentstack.com" target="_self">support@contentstack.com</a> | © 2016-2020 Contentstack. All rights reserved.</span>
</div>
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>