Skip to content

Commit 1237355

Browse files
committed
bug fixes for: .entry(uid), .asset(uid), .schema(uid), .include(), .includeReferences, .tags
1 parent 060cf4a commit 1237355

File tree

4 files changed

+110
-85
lines changed

4 files changed

+110
-85
lines changed

dist/stack.js

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Contentstack Ecosystem <[email protected]>",
33
"name": "datasync-mongodb-sdk",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"description": "Mongodb query wrapper around contents synced via @contentstack/content-store-mongodb",
66
"main": "dist/index.js",
77
"scripts": {

src/stack.ts

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ export class Stack {
171171
* @returns {object} Mongodb 'db' instance
172172
*/
173173
public async connect(overrides = {}) {
174-
175174
const dbConfig = merge({}, this.config, overrides).contentStore
176175

177176
const url = validateURI(dbConfig.url)
@@ -184,37 +183,8 @@ export class Stack {
184183
this.db = client.db(dbName)
185184

186185
return this.db
187-
188-
// // Create indexes in the background
189-
// const bucket: any = []
190-
// const indexes = this.config.contentStore.indexes
191-
// const collectionName = this.config.contentStore.collectionName
192-
// for (let index in indexes) {
193-
// if (indexes[index] === 1 || indexes[index] === -1) {
194-
// bucket.push(this.createIndexes(this.config.contentStore.collectionName, index, indexes[index]))
195-
// }
196-
// }
197-
198-
// Promise.all(bucket)
199-
// .then(() => {
200-
// console.info(`Indexes created successfully in '${collectionName}' collection`)
201-
// })
202-
// .catch((error) => {
203-
// console.error(`Failed while creating indexes in '${collectionName}' collection`)
204-
// console.error(error)
205-
// })
206186
}
207187

208-
// private createIndexes(collectionId, index, type) {
209-
// return this.db.collection(collectionId)
210-
// .createIndex({
211-
// [index]: type
212-
// })
213-
// .then(() => {
214-
// return
215-
// })
216-
// }
217-
218188
/**
219189
* @public
220190
* @method close
@@ -284,7 +254,9 @@ export class Stack {
284254
* @returns {Stack} Returns an instance of 'stack'
285255
*/
286256
public and(queries) {
287-
if (this.q.query && typeof this.q.query === 'object') {
257+
if (typeof queries !== 'object' || !Array.isArray(queries)) {
258+
throw new Error('Kindly provide valid parameters for .and()!')
259+
} else if (this.q.query && typeof this.q.query === 'object') {
288260
this.q.query = merge(this.q.query, {
289261
$and: queries,
290262
})
@@ -326,7 +298,9 @@ export class Stack {
326298
* @returns {Stack} Returns an instance of 'stack'
327299
*/
328300
public or(queries) {
329-
if (this.q.query && typeof this.q.query === 'object') {
301+
if (typeof queries !== 'object' || !Array.isArray(queries)) {
302+
throw new Error('Kindly provide valid parameters for .or()!')
303+
} else if (this.q.query && typeof this.q.query === 'object') {
330304
this.q.query = merge(this.q.query, {
331305
$or: queries,
332306
})
@@ -820,7 +794,8 @@ export class Stack {
820794
throw new Error('Kindly call \'contentType()\' before \'entry()\'!')
821795
}
822796
if (uid && typeof uid === 'string') {
823-
this.q.uid = uid
797+
this.q.query = this.q.query || {}
798+
this.q.query.uid = uid
824799
}
825800
this.internal.limit = 1
826801
this.internal.single = true
@@ -880,7 +855,8 @@ export class Stack {
880855
public asset(uid ? ) {
881856
const stack = new Stack(this.config, this.db)
882857
if (uid && typeof uid === 'string') {
883-
stack.q.uid = uid
858+
stack.q.query = stack.q.query || {}
859+
stack.q.query.uid = uid
884860
}
885861
stack.q.content_type_uid = this.types.assets
886862
// stack.collection = stack.db.collection(stack.contentStore.collectionName)
@@ -942,7 +918,8 @@ export class Stack {
942918
public schema(uid ? ) {
943919
const stack = new Stack(this.config, this.db)
944920
if (uid && typeof uid === 'string') {
945-
stack.q.uid = uid
921+
stack.q.query = stack.q.query || {}
922+
stack.q.query.uid = uid
946923
}
947924
stack.q.content_type_uid = this.types.content_types
948925
// stack.collection = stack.db.collection(stack.contentStore.collectionName)
@@ -1247,23 +1224,22 @@ export class Stack {
12471224
* @returns {Stack} Returns an instance of 'stack'
12481225
*/
12491226
public tags(values) {
1250-
if (!values || typeof values !== 'object' || !(values instanceof Array) || values.length === 0) {
1227+
if (!values || typeof values !== 'object' || !(values instanceof Array)) {
12511228
throw new Error('Kindly provide valid \'field\' values for \'tags()\'')
12521229
}
12531230
// filter non-string keys
12541231
remove(values, (value) => {
12551232
return typeof value !== 'string'
12561233
})
12571234

1258-
if (this.q.query && typeof this.q.query === 'object') {
1235+
this.q.query = this.q.query || {}
1236+
if (values.length === 0) {
12591237
this.q.query.tags = {
1260-
$in: values,
1238+
$size: 0,
12611239
}
12621240
} else {
1263-
this.q.query = {
1264-
tags: {
1265-
$in: values,
1266-
},
1241+
this.q.query.tags = {
1242+
$in: values,
12671243
}
12681244
}
12691245

@@ -2033,9 +2009,38 @@ export class Stack {
20332009
return
20342010
}
20352011

2012+
private async bindLeftoverAssets(queries: IQuery, locale: string, pointerList: IShelf[]) {
2013+
// const contents = await readFile(getAssetsPath(locale) + '.json')
2014+
const filteredAssets = await this.db.collection(getCollectionName({
2015+
content_type_uid: this.types.assets,
2016+
locale,
2017+
}, this.collectionNames))
2018+
.find(queries)
2019+
.project({
2020+
_content_type_uid: 0,
2021+
_id: 0,
2022+
})
2023+
.toArray()
2024+
2025+
for (let l = 0, m = pointerList.length; l < m; l++) {
2026+
for (let n = 0, o = filteredAssets.length; n < o; n++) {
2027+
if (pointerList[l].uid === filteredAssets[n].uid) {
2028+
pointerList[l].path[pointerList[l].position] = filteredAssets[n]
2029+
break
2030+
}
2031+
}
2032+
}
2033+
2034+
return
2035+
}
2036+
20362037
private async includeReferenceIteration(eQuery: any, ctQuery: any, locale: string, include: string[], oldShelf:
20372038
IShelf[]) {
2038-
if (oldShelf.length === 0 || ctQuery.$or.length === 0) {
2039+
if (oldShelf.length === 0) {
2040+
return
2041+
} else if (ctQuery.$or.length === 0 && eQuery.$or.length > 0) {
2042+
await this.bindLeftoverAssets(eQuery, locale, oldShelf)
2043+
20392044
return
20402045
}
20412046

@@ -2253,9 +2258,14 @@ export class Stack {
22532258

22542259
private async includeAllReferencesIteration(oldEntryQueries: IQuery, oldCtQueries: IQuery, locale:
22552260
string, oldObjectPointerList: IShelf[], depth = 0) {
2256-
if (depth > this.q.referenceDepth || oldObjectPointerList.length === 0 || oldCtQueries.$or.length === 0) {
2261+
if (depth > this.q.referenceDepth || oldObjectPointerList.length === 0) {
2262+
return
2263+
} else if (oldCtQueries.$or.length === 0 && oldObjectPointerList.length > 0 && oldEntryQueries.$or.length > 0) {
2264+
await this.bindLeftoverAssets(oldEntryQueries, locale, oldObjectPointerList)
2265+
22572266
return
22582267
}
2268+
22592269
const {
22602270
ctQueries,
22612271
paths,

typings/stack.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ export declare class Stack {
10841084
*/
10851085
private includeSpecificReferences;
10861086
private fetchPathDetails;
1087+
private bindLeftoverAssets;
10871088
private includeReferenceIteration;
10881089
private getReferencePath;
10891090
private fetchEntries;

0 commit comments

Comments
 (0)