@@ -17,41 +17,58 @@ class Stack {
17
17
constructor ( stackConfig , existingDB ) {
18
18
this . config = lodash_1 . merge ( config_1 . config , stackConfig ) ;
19
19
this . q = { } ;
20
- this . q = { } ;
21
20
this . internal = { } ;
22
21
this . db = existingDB ;
23
22
}
24
23
ascending ( field ) {
25
- if ( ! ( field ) || typeof field !== 'string' ) {
26
- throw new Error ( 'Kindly provide a valid field name for \'.ascending()\'' ) ;
27
- }
28
- else if ( typeof this . q . content_type_uid !== 'string' ) {
24
+ if ( typeof this . q . content_type_uid !== 'string' ) {
29
25
throw new Error ( 'Kindly call \'.contentType()\' before \.ascending()\'' ) ;
30
26
}
31
- if ( this . internal . sort && typeof this . internal . sort === 'object' ) {
32
- this . internal . sort [ field ] = 1 ;
27
+ if ( ! ( field ) || typeof field !== 'string' ) {
28
+ if ( this . internal . sort && typeof this . internal . sort === 'object' ) {
29
+ this . internal . sort . published_at = 1 ;
30
+ }
31
+ else {
32
+ this . internal . sort = {
33
+ published_at : 1 ,
34
+ } ;
35
+ }
33
36
}
34
37
else {
35
- this . internal . sort = {
36
- [ field ] : 1 ,
37
- } ;
38
+ if ( this . internal . sort && typeof this . internal . sort === 'object' ) {
39
+ this . internal . sort [ field ] = 1 ;
40
+ }
41
+ else {
42
+ this . internal . sort = {
43
+ [ field ] : 1 ,
44
+ } ;
45
+ }
38
46
}
39
47
return this ;
40
48
}
41
49
descending ( field ) {
42
- if ( ! ( field ) || typeof field !== 'string' ) {
43
- throw new Error ( 'Kindly provide a valid field name for \'.descending()\'' ) ;
44
- }
45
- else if ( typeof this . q . content_type_uid !== 'string' ) {
50
+ if ( typeof this . q . content_type_uid !== 'string' ) {
46
51
throw new Error ( 'Kindly call \'.contentType()\' before \.descending()\'' ) ;
47
52
}
48
- if ( this . internal . sort && typeof this . internal . sort === 'object' ) {
49
- this . internal . sort [ field ] = - 1 ;
53
+ if ( ! ( field ) || typeof field !== 'string' ) {
54
+ if ( this . internal . sort && typeof this . internal . sort === 'object' ) {
55
+ this . internal . sort [ field ] = - 1 ;
56
+ }
57
+ else {
58
+ this . internal . sort = {
59
+ [ field ] : - 1 ,
60
+ } ;
61
+ }
50
62
}
51
63
else {
52
- this . internal . sort = {
53
- [ field ] : - 1 ,
54
- } ;
64
+ if ( this . internal . sort && typeof this . internal . sort === 'object' ) {
65
+ this . internal . sort [ field ] = - 1 ;
66
+ }
67
+ else {
68
+ this . internal . sort = {
69
+ [ field ] : - 1 ,
70
+ } ;
71
+ }
55
72
}
56
73
return this ;
57
74
}
@@ -301,9 +318,9 @@ class Stack {
301
318
asset ( uid ) {
302
319
const stack = new Stack ( this . config , this . db ) ;
303
320
if ( uid && typeof uid === 'string' ) {
304
- stack . q . content_type_uid = '_assets' ;
305
321
stack . q . uid = uid ;
306
322
}
323
+ stack . q . content_type_uid = '_assets' ;
307
324
stack . collection = stack . db . collection ( stack . config . collectionName ) ;
308
325
stack . internal . limit = 1 ;
309
326
stack . internal . single = true ;
@@ -316,19 +333,21 @@ class Stack {
316
333
return stack ;
317
334
}
318
335
schema ( uid ) {
336
+ const stack = new Stack ( this . config , this . db ) ;
319
337
if ( uid && typeof uid === 'string' ) {
320
- this . q . content_type_uid = 'contentTypes' ;
321
- this . q . uid = uid ;
338
+ stack . q . uid = uid ;
322
339
}
323
- this . collection = this . db . collection ( this . config . collectionName ) ;
324
- this . internal . limit = 1 ;
325
- this . internal . single = true ;
326
- return this ;
340
+ stack . q . content_type_uid = 'contentTypes' ;
341
+ stack . collection = stack . db . collection ( stack . config . collectionName ) ;
342
+ stack . internal . limit = 1 ;
343
+ stack . internal . single = true ;
344
+ return stack ;
327
345
}
328
346
schemas ( ) {
329
- this . q . content_type_uid = 'contentTypes' ;
330
- this . collection = this . db . collection ( this . config . collectionName ) ;
331
- return this ;
347
+ const stack = new Stack ( this . config , this . db ) ;
348
+ stack . q . content_type_uid = 'contentTypes' ;
349
+ stack . collection = stack . db . collection ( stack . config . collectionName ) ;
350
+ return stack ;
332
351
}
333
352
limit ( no ) {
334
353
if ( typeof no === 'number' && ( no >= 0 ) && typeof this . q . content_type_uid === 'string' ) {
@@ -476,12 +495,14 @@ class Stack {
476
495
. toArray ( )
477
496
. then ( ( result ) => {
478
497
let contentType ;
479
- if ( this . internal . includeSchema ) {
498
+ if ( this . internal . includeSchema && this . q . content_type_uid !== 'contentTypes' && this . q . content_type_uid !==
499
+ '_assets' ) {
480
500
contentType = lodash_1 . remove ( result , { uid : this . q . content_type_uid } ) ;
481
501
contentType = ( typeof contentType === 'object' && contentType instanceof Array && contentType . length ) ?
482
502
contentType [ 0 ] : null ;
483
503
}
484
- if ( this . internal . excludeReferences ) {
504
+ if ( this . internal . excludeReferences || this . q . content_type_uid === 'contentTypes' || this . q . content_type_uid
505
+ === '_assets' ) {
485
506
result = this . postProcess ( result , contentType ) ;
486
507
return resolve ( result ) ;
487
508
}
@@ -506,9 +527,12 @@ class Stack {
506
527
count ( query ) {
507
528
return new Promise ( ( resolve , reject ) => {
508
529
const queryFilters = this . preProcess ( query ) ;
530
+ this . collection = this . collection . find ( queryFilters ) ;
531
+ if ( this . internal . sort ) {
532
+ this . collection = this . collection . sort ( this . internal . sort ) ;
533
+ }
509
534
if ( this . internal . queryReferences ) {
510
535
return this . collection
511
- . find ( queryFilters )
512
536
. project ( this . internal . projections )
513
537
. toArray ( )
514
538
. then ( ( result ) => {
@@ -529,7 +553,6 @@ class Stack {
529
553
} ) ;
530
554
}
531
555
return this . collection
532
- . find ( queryFilters )
533
556
. project ( this . internal . projections )
534
557
. count ( )
535
558
. then ( ( result ) => {
@@ -621,9 +644,6 @@ class Stack {
621
644
else {
622
645
queryFilters = filters ;
623
646
}
624
- if ( this . internal . sort ) {
625
- this . collection = this . collection . sort ( this . internal . sort ) ;
626
- }
627
647
return queryFilters ;
628
648
}
629
649
cleanup ( ) {
@@ -645,6 +665,8 @@ class Stack {
645
665
assets : result ,
646
666
} ;
647
667
}
668
+ result . content_type_uid = 'assets' ;
669
+ result . locale = this . q . locale ;
648
670
break ;
649
671
case 'contentTypes' :
650
672
if ( this . internal . single ) {
@@ -657,6 +679,7 @@ class Stack {
657
679
content_types : result ,
658
680
} ;
659
681
}
682
+ result . content_type_uid = 'content_types' ;
660
683
break ;
661
684
default :
662
685
if ( this . internal . single ) {
@@ -669,19 +692,16 @@ class Stack {
669
692
entries : result ,
670
693
} ;
671
694
}
695
+ result . content_type_uid = this . q . content_type_uid ;
696
+ result . locale = this . q . locale ;
672
697
break ;
673
698
}
674
699
if ( this . internal . includeCount ) {
675
700
result . count = count ;
676
701
}
677
- if ( this . q . content_type_uid === '_assets' ) {
678
- this . q . content_type_uid = 'assets' ;
679
- }
680
702
if ( this . internal . includeSchema ) {
681
703
result . content_type = contentType ;
682
704
}
683
- result . content_type_uid = this . q . content_type_uid ;
684
- result . locale = this . q . locale ;
685
705
this . cleanup ( ) ;
686
706
return result ;
687
707
}
0 commit comments