@@ -146,34 +146,8 @@ class Stack {
146146 yield client . connect ( ) ;
147147 this . db = client . db ( dbName ) ;
148148 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- // })
166149 } ) ;
167150 }
168- // private createIndexes(collectionId, index, type) {
169- // return this.db.collection(collectionId)
170- // .createIndex({
171- // [index]: type
172- // })
173- // .then(() => {
174- // return
175- // })
176- // }
177151 /**
178152 * @public
179153 * @method close
@@ -240,7 +214,10 @@ class Stack {
240214 * @returns {Stack } Returns an instance of 'stack'
241215 */
242216 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' ) {
244221 this . q . query = lodash_1 . merge ( this . q . query , {
245222 $and : queries ,
246223 } ) ;
@@ -281,7 +258,10 @@ class Stack {
281258 * @returns {Stack } Returns an instance of 'stack'
282259 */
283260 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' ) {
285265 this . q . query = lodash_1 . merge ( this . q . query , {
286266 $or : queries ,
287267 } ) ;
@@ -772,7 +752,8 @@ class Stack {
772752 throw new Error ( 'Kindly call \'contentType()\' before \'entry()\'!' ) ;
773753 }
774754 if ( uid && typeof uid === 'string' ) {
775- this . q . uid = uid ;
755+ this . q . query = this . q . query || { } ;
756+ this . q . query . uid = uid ;
776757 }
777758 this . internal . limit = 1 ;
778759 this . internal . single = true ;
@@ -828,7 +809,8 @@ class Stack {
828809 asset ( uid ) {
829810 const stack = new Stack ( this . config , this . db ) ;
830811 if ( uid && typeof uid === 'string' ) {
831- stack . q . uid = uid ;
812+ stack . q . query = stack . q . query || { } ;
813+ stack . q . query . uid = uid ;
832814 }
833815 stack . q . content_type_uid = this . types . assets ;
834816 // stack.collection = stack.db.collection(stack.contentStore.collectionName)
@@ -886,7 +868,8 @@ class Stack {
886868 schema ( uid ) {
887869 const stack = new Stack ( this . config , this . db ) ;
888870 if ( uid && typeof uid === 'string' ) {
889- stack . q . uid = uid ;
871+ stack . q . query = stack . q . query || { } ;
872+ stack . q . query . uid = uid ;
890873 }
891874 stack . q . content_type_uid = this . types . content_types ;
892875 // stack.collection = stack.db.collection(stack.contentStore.collectionName)
@@ -1174,23 +1157,22 @@ class Stack {
11741157 * @returns {Stack } Returns an instance of 'stack'
11751158 */
11761159 tags ( values ) {
1177- if ( ! values || typeof values !== 'object' || ! ( values instanceof Array ) || values . length === 0 ) {
1160+ if ( ! values || typeof values !== 'object' || ! ( values instanceof Array ) ) {
11781161 throw new Error ( 'Kindly provide valid \'field\' values for \'tags()\'' ) ;
11791162 }
11801163 // filter non-string keys
11811164 lodash_1 . remove ( values , ( value ) => {
11821165 return typeof value !== 'string' ;
11831166 } ) ;
1184- if ( this . q . query && typeof this . q . query === 'object' ) {
1167+ this . q . query = this . q . query || { } ;
1168+ if ( values . length === 0 ) {
11851169 this . q . query . tags = {
1186- $in : values ,
1170+ $size : 0 ,
11871171 } ;
11881172 }
11891173 else {
1190- this . q . query = {
1191- tags : {
1192- $in : values ,
1193- } ,
1174+ this . q . query . tags = {
1175+ $in : values ,
11941176 } ;
11951177 }
11961178 return this ;
@@ -1907,9 +1889,37 @@ class Stack {
19071889 // since we've reached last of the paths, return!
19081890 return ;
19091891 }
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+ }
19101916 includeReferenceIteration ( eQuery , ctQuery , locale , include , oldShelf ) {
19111917 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 ) ;
19131923 return ;
19141924 }
19151925 const { paths, pendingPath, schemaList, } = yield this . getReferencePath ( ctQuery , locale , include ) ;
@@ -2097,7 +2107,11 @@ class Stack {
20972107 }
20982108 includeAllReferencesIteration ( oldEntryQueries , oldCtQueries , locale , oldObjectPointerList , depth = 0 ) {
20992109 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 ) ;
21012115 return ;
21022116 }
21032117 const { ctQueries, paths, } = yield this . getAllReferencePaths ( oldCtQueries , locale ) ;
0 commit comments