Skip to content

Commit

Permalink
#1, #2 and #3 issues bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
iyerrama29 committed Aug 13, 2019
1 parent 43a77a8 commit 2ffd68c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 16 deletions.
54 changes: 46 additions & 8 deletions dist/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ class Stack {
throw new Error('Please call .contentType() before calling .entries()!');
}
if (uid && typeof uid === 'string') {
this.q.query[uid] = uid;
this.q.query.uid = uid;
}
return this;
}
Expand All @@ -455,7 +455,7 @@ class Stack {
stack.q.isSingle = true;
stack.q.content_type_uid = stack.types.assets;
if (uid && typeof uid === 'string') {
stack.q.query[uid] = uid;
stack.q.query.uid = uid;
}
return stack;
}
Expand Down Expand Up @@ -516,7 +516,7 @@ class Stack {
stack.q.isSingle = true;
stack.q.content_type_uid = stack.types.content_types;
if (uid && typeof uid === 'string') {
stack.q.query[uid] = uid;
stack.q.query.uid = uid;
}
return stack;
}
Expand Down Expand Up @@ -626,9 +626,16 @@ class Stack {
*/
tags(values) {
if (values && typeof values === 'object' && values instanceof Array) {
this.q.query.tags = {
$in: values,
};
if (values.length === 0) {
this.q.query.tags = {
$size: 0,
};
}
else {
this.q.query.tags = {
$in: values,
};
}
return this;
}
throw new Error('Kindly provide valid parameters for \'.tags()\'');
Expand Down Expand Up @@ -1164,7 +1171,11 @@ class Stack {
}
includeReferenceIteration(eQuery, ctQuery, locale, include, oldShelf) {
return __awaiter(this, void 0, void 0, function* () {
if (oldShelf.length === 0 || ctQuery.$or.length === 0) {
if (oldShelf.length === 0) {
return;
}
else if (ctQuery.$or.length === 0 && eQuery.$or.length > 0) {
yield this.bindLeftoverAssets(eQuery, locale, oldShelf);
return;
}
const { paths, pendingPath, schemaList, } = yield this.getReferencePath(ctQuery, locale, include);
Expand Down Expand Up @@ -1479,10 +1490,37 @@ class Stack {
return this.includeAllReferencesIteration(queries, ctQueries, locale, objectPointerList);
});
}
bindLeftoverAssets(queries, locale, pointerList) {
return __awaiter(this, void 0, void 0, function* () {
const contents = yield fs_1.readFile(utils_1.getAssetsPath(locale) + '.json');
const filteredAssets = contents.filter(sift_1.default(queries));
filteredAssets.forEach((doc) => {
this.projections.forEach((key) => {
if (doc.hasOwnProperty(key)) {
delete doc[key];
}
});
});
for (let l = 0, m = pointerList.length; l < m; l++) {
for (let n = 0, o = filteredAssets.length; n < o; n++) {
if (pointerList[l].uid === filteredAssets[n].uid) {
pointerList[l].path[pointerList[l].position] = filteredAssets[n];
break;
}
}
}
return;
});
}
// tslint:disable-next-line: max-line-length
includeAllReferencesIteration(oldEntryQueries, oldCtQueries, locale, oldObjectPointerList, depth = 0) {
return __awaiter(this, void 0, void 0, function* () {
if (depth > this.q.referenceDepth || oldObjectPointerList.length === 0 || oldCtQueries.$or.length === 0) {
if (depth > this.q.referenceDepth || oldObjectPointerList.length === 0) {
return;
}
else if (oldCtQueries.$or.length === 0 && oldObjectPointerList.length > 0 && oldEntryQueries.$or.length > 0) {
// its most likely only assets
yield this.bindLeftoverAssets(oldEntryQueries, locale, oldObjectPointerList);
return;
}
const { ctQueries, paths, } = yield this.getAllReferencePaths(oldCtQueries, locale);
Expand Down
1 change: 1 addition & 0 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function connect () {
function find (contentType = 'blog') {
return Stack.contentType(contentType)
.entries()
.includeReferences()
.find()
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datasync-filesystem-sdk",
"version": "1.0.1",
"version": "1.0.2",
"description": "JavaScript filesystem SDK to query data synced via @contentstack/datasync-content-store-filesystem",
"main": "dist/index.js",
"scripts": {
Expand Down
54 changes: 47 additions & 7 deletions src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export class Stack {
}

if (uid && typeof uid === 'string') {
this.q.query[uid] = uid
this.q.query.uid = uid
}

return this
Expand All @@ -535,7 +535,7 @@ export class Stack {
stack.q.isSingle = true
stack.q.content_type_uid = stack.types.assets
if (uid && typeof uid === 'string') {
stack.q.query[uid] = uid
stack.q.query.uid = uid
}

return stack
Expand Down Expand Up @@ -605,7 +605,7 @@ export class Stack {
stack.q.isSingle = true
stack.q.content_type_uid = stack.types.content_types
if (uid && typeof uid === 'string') {
stack.q.query[uid] = uid
stack.q.query.uid = uid
}

return stack
Expand Down Expand Up @@ -726,8 +726,14 @@ export class Stack {
*/
public tags(values) {
if (values && typeof values === 'object' && values instanceof Array) {
this.q.query.tags = {
$in: values,
if (values.length === 0) {
this.q.query.tags = {
$size: 0,
}
} else {
this.q.query.tags = {
$in: values,
}
}

return this
Expand Down Expand Up @@ -1323,7 +1329,11 @@ export class Stack {

private async includeReferenceIteration(eQuery: any, ctQuery: any, locale: string, include: string[], oldShelf:
IShelf[]) {
if (oldShelf.length === 0 || ctQuery.$or.length === 0) {
if (oldShelf.length === 0) {
return
} else if (ctQuery.$or.length === 0 && eQuery.$or.length > 0) {
await this.bindLeftoverAssets(eQuery, locale, oldShelf)

return
}

Expand Down Expand Up @@ -1680,11 +1690,41 @@ export class Stack {
return this.includeAllReferencesIteration(queries, ctQueries, locale, objectPointerList)
}

private async bindLeftoverAssets(queries: IQuery, locale: string, pointerList: IShelf[]) {
const contents = await readFile(getAssetsPath(locale) + '.json')
const filteredAssets = contents.filter(sift(queries))

filteredAssets.forEach((doc) => {
this.projections.forEach((key) => {
if (doc.hasOwnProperty(key)) {
delete doc[key]
}
})
})

for (let l = 0, m = pointerList.length; l < m; l++) {
for (let n = 0, o = filteredAssets.length; n < o; n++) {
if (pointerList[l].uid === filteredAssets[n].uid) {
pointerList[l].path[pointerList[l].position] = filteredAssets[n]
break
}
}
}

return
}

// tslint:disable-next-line: max-line-length
private async includeAllReferencesIteration(oldEntryQueries: IQuery, oldCtQueries: IQuery, locale: string, oldObjectPointerList: IShelf[], depth = 0) {
if (depth > this.q.referenceDepth || oldObjectPointerList.length === 0 || oldCtQueries.$or.length === 0) {
if (depth > this.q.referenceDepth || oldObjectPointerList.length === 0) {
return
} else if (oldCtQueries.$or.length === 0 && oldObjectPointerList.length > 0 && oldEntryQueries.$or.length > 0) {
// its most likely only assets
await this.bindLeftoverAssets(oldEntryQueries, locale, oldObjectPointerList)

return
}

const {
ctQueries,
paths,
Expand Down
1 change: 1 addition & 0 deletions typings/stack.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ export declare class Stack {
private fetchDocuments;
private includeAssetsOnly;
private bindReferences;
private bindLeftoverAssets;
private includeAllReferencesIteration;
private subIncludeAllReferencesIteration;
private getAllReferencePaths;
Expand Down

0 comments on commit 2ffd68c

Please sign in to comment.