@@ -146,34 +146,8 @@ class Stack {
146
146
yield client . connect ( ) ;
147
147
this . db = client . db ( dbName ) ;
148
148
return this . db ;
149
- // // Create indexes in the background
150
- // const bucket: any = []
151
- // const indexes = this.config.contentStore.indexes
152
- // const collectionName = this.config.contentStore.collectionName
153
- // for (let index in indexes) {
154
- // if (indexes[index] === 1 || indexes[index] === -1) {
155
- // bucket.push(this.createIndexes(this.config.contentStore.collectionName, index, indexes[index]))
156
- // }
157
- // }
158
- // Promise.all(bucket)
159
- // .then(() => {
160
- // console.info(`Indexes created successfully in '${collectionName}' collection`)
161
- // })
162
- // .catch((error) => {
163
- // console.error(`Failed while creating indexes in '${collectionName}' collection`)
164
- // console.error(error)
165
- // })
166
149
} ) ;
167
150
}
168
- // private createIndexes(collectionId, index, type) {
169
- // return this.db.collection(collectionId)
170
- // .createIndex({
171
- // [index]: type
172
- // })
173
- // .then(() => {
174
- // return
175
- // })
176
- // }
177
151
/**
178
152
* @public
179
153
* @method close
@@ -240,7 +214,10 @@ class Stack {
240
214
* @returns {Stack } Returns an instance of 'stack'
241
215
*/
242
216
and ( queries ) {
243
- if ( this . q . query && typeof this . q . query === 'object' ) {
217
+ if ( typeof queries !== 'object' || ! Array . isArray ( queries ) ) {
218
+ throw new Error ( 'Kindly provide valid parameters for .and()!' ) ;
219
+ }
220
+ else if ( this . q . query && typeof this . q . query === 'object' ) {
244
221
this . q . query = lodash_1 . merge ( this . q . query , {
245
222
$and : queries ,
246
223
} ) ;
@@ -281,7 +258,10 @@ class Stack {
281
258
* @returns {Stack } Returns an instance of 'stack'
282
259
*/
283
260
or ( queries ) {
284
- if ( this . q . query && typeof this . q . query === 'object' ) {
261
+ if ( typeof queries !== 'object' || ! Array . isArray ( queries ) ) {
262
+ throw new Error ( 'Kindly provide valid parameters for .or()!' ) ;
263
+ }
264
+ else if ( this . q . query && typeof this . q . query === 'object' ) {
285
265
this . q . query = lodash_1 . merge ( this . q . query , {
286
266
$or : queries ,
287
267
} ) ;
@@ -772,7 +752,8 @@ class Stack {
772
752
throw new Error ( 'Kindly call \'contentType()\' before \'entry()\'!' ) ;
773
753
}
774
754
if ( uid && typeof uid === 'string' ) {
775
- this . q . uid = uid ;
755
+ this . q . query = this . q . query || { } ;
756
+ this . q . query . uid = uid ;
776
757
}
777
758
this . internal . limit = 1 ;
778
759
this . internal . single = true ;
@@ -828,7 +809,8 @@ class Stack {
828
809
asset ( uid ) {
829
810
const stack = new Stack ( this . config , this . db ) ;
830
811
if ( uid && typeof uid === 'string' ) {
831
- stack . q . uid = uid ;
812
+ stack . q . query = stack . q . query || { } ;
813
+ stack . q . query . uid = uid ;
832
814
}
833
815
stack . q . content_type_uid = this . types . assets ;
834
816
// stack.collection = stack.db.collection(stack.contentStore.collectionName)
@@ -886,7 +868,8 @@ class Stack {
886
868
schema ( uid ) {
887
869
const stack = new Stack ( this . config , this . db ) ;
888
870
if ( uid && typeof uid === 'string' ) {
889
- stack . q . uid = uid ;
871
+ stack . q . query = stack . q . query || { } ;
872
+ stack . q . query . uid = uid ;
890
873
}
891
874
stack . q . content_type_uid = this . types . content_types ;
892
875
// stack.collection = stack.db.collection(stack.contentStore.collectionName)
@@ -1174,23 +1157,22 @@ class Stack {
1174
1157
* @returns {Stack } Returns an instance of 'stack'
1175
1158
*/
1176
1159
tags ( values ) {
1177
- if ( ! values || typeof values !== 'object' || ! ( values instanceof Array ) || values . length === 0 ) {
1160
+ if ( ! values || typeof values !== 'object' || ! ( values instanceof Array ) ) {
1178
1161
throw new Error ( 'Kindly provide valid \'field\' values for \'tags()\'' ) ;
1179
1162
}
1180
1163
// filter non-string keys
1181
1164
lodash_1 . remove ( values , ( value ) => {
1182
1165
return typeof value !== 'string' ;
1183
1166
} ) ;
1184
- if ( this . q . query && typeof this . q . query === 'object' ) {
1167
+ this . q . query = this . q . query || { } ;
1168
+ if ( values . length === 0 ) {
1185
1169
this . q . query . tags = {
1186
- $in : values ,
1170
+ $size : 0 ,
1187
1171
} ;
1188
1172
}
1189
1173
else {
1190
- this . q . query = {
1191
- tags : {
1192
- $in : values ,
1193
- } ,
1174
+ this . q . query . tags = {
1175
+ $in : values ,
1194
1176
} ;
1195
1177
}
1196
1178
return this ;
@@ -1907,9 +1889,37 @@ class Stack {
1907
1889
// since we've reached last of the paths, return!
1908
1890
return ;
1909
1891
}
1892
+ bindLeftoverAssets ( queries , locale , pointerList ) {
1893
+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
1894
+ // const contents = await readFile(getAssetsPath(locale) + '.json')
1895
+ const filteredAssets = yield this . db . collection ( util_1 . getCollectionName ( {
1896
+ content_type_uid : this . types . assets ,
1897
+ locale,
1898
+ } , this . collectionNames ) )
1899
+ . find ( queries )
1900
+ . project ( {
1901
+ _content_type_uid : 0 ,
1902
+ _id : 0 ,
1903
+ } )
1904
+ . toArray ( ) ;
1905
+ for ( let l = 0 , m = pointerList . length ; l < m ; l ++ ) {
1906
+ for ( let n = 0 , o = filteredAssets . length ; n < o ; n ++ ) {
1907
+ if ( pointerList [ l ] . uid === filteredAssets [ n ] . uid ) {
1908
+ pointerList [ l ] . path [ pointerList [ l ] . position ] = filteredAssets [ n ] ;
1909
+ break ;
1910
+ }
1911
+ }
1912
+ }
1913
+ return ;
1914
+ } ) ;
1915
+ }
1910
1916
includeReferenceIteration ( eQuery , ctQuery , locale , include , oldShelf ) {
1911
1917
return __awaiter ( this , void 0 , void 0 , function * ( ) {
1912
- if ( oldShelf . length === 0 || ctQuery . $or . length === 0 ) {
1918
+ if ( oldShelf . length === 0 ) {
1919
+ return ;
1920
+ }
1921
+ else if ( ctQuery . $or . length === 0 && eQuery . $or . length > 0 ) {
1922
+ yield this . bindLeftoverAssets ( eQuery , locale , oldShelf ) ;
1913
1923
return ;
1914
1924
}
1915
1925
const { paths, pendingPath, schemaList, } = yield this . getReferencePath ( ctQuery , locale , include ) ;
@@ -2097,7 +2107,11 @@ class Stack {
2097
2107
}
2098
2108
includeAllReferencesIteration ( oldEntryQueries , oldCtQueries , locale , oldObjectPointerList , depth = 0 ) {
2099
2109
return __awaiter ( this , void 0 , void 0 , function * ( ) {
2100
- if ( depth > this . q . referenceDepth || oldObjectPointerList . length === 0 || oldCtQueries . $or . length === 0 ) {
2110
+ if ( depth > this . q . referenceDepth || oldObjectPointerList . length === 0 ) {
2111
+ return ;
2112
+ }
2113
+ else if ( oldCtQueries . $or . length === 0 && oldObjectPointerList . length > 0 && oldEntryQueries . $or . length > 0 ) {
2114
+ yield this . bindLeftoverAssets ( oldEntryQueries , locale , oldObjectPointerList ) ;
2101
2115
return ;
2102
2116
}
2103
2117
const { ctQueries, paths, } = yield this . getAllReferencePaths ( oldCtQueries , locale ) ;
0 commit comments