Skip to content

Commit 0f02f25

Browse files
committed
.include() and referenceDepth bug fix
1 parent 8194af5 commit 0f02f25

File tree

3 files changed

+61
-22
lines changed

3 files changed

+61
-22
lines changed

dist/stack.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,8 @@ class Stack {
15601560
else {
15611561
this.q.query = {};
15621562
}
1563-
this.q.referenceDepth = this.q.referenceDepth || this.contentStore.referenceDepth;
1563+
// tslint:disable-next-line: max-line-length
1564+
this.q.referenceDepth = (typeof this.q.referenceDepth === 'number') ? this.q.referenceDepth : this.contentStore.referenceDepth;
15641565
if (this.internal.only) {
15651566
this.internal.projections = this.internal.only;
15661567
}
@@ -1675,7 +1676,6 @@ class Stack {
16751676
_content_type_uid: this.q.content_type_uid,
16761677
});
16771678
}
1678-
// console.log('this.internal.includeSchema', this.internal.includeSchema)
16791679
if (this.internal.includeSchema) {
16801680
output.content_type = yield this.db.collection(util_1.getCollectionName({
16811681
content_type_uid: this.types.content_types,
@@ -1889,12 +1889,23 @@ class Stack {
18891889
eQuery = null;
18901890
for (let i = 0, j = oldShelf.length; i < j; i++) {
18911891
const element = oldShelf[i];
1892+
let flag = true;
18921893
for (let k = 0, l = result.length; k < l; k++) {
18931894
if (result[k].uid === element.uid) {
18941895
element.path[element.position] = result[k];
1896+
flag = false;
18951897
break;
18961898
}
18971899
}
1900+
if (flag) {
1901+
for (let e = 0, f = oldShelf[i].path.length; e < f; e++) {
1902+
// tslint:disable-next-line: max-line-length
1903+
if (oldShelf[i].path[e].hasOwnProperty('_content_type_uid') && Object.keys(oldShelf[i].path[e]).length === 2) {
1904+
oldShelf[i].path.splice(e, 1);
1905+
break;
1906+
}
1907+
}
1908+
}
18981909
}
18991910
// GC to avoid mem leaks!
19001911
oldShelf = null;
@@ -1942,13 +1953,12 @@ class Stack {
19421953
const includePath = currentInclude[i];
19431954
// tslint:disable-next-line: forin
19441955
for (const path in entryReferences) {
1945-
const idx = includePath.indexOf(path);
1946-
// tslint:disable-next-line: no-bitwise
1947-
if (~idx) {
1956+
const subStr = includePath.slice(0, path.length);
1957+
if (subStr === path) {
19481958
let subPath;
19491959
// Its the complete path!! Hurrah!
19501960
if (path.length !== includePath.length) {
1951-
subPath = includePath.slice(0, path.length);
1961+
subPath = subStr;
19521962
pendingPath.push(includePath.slice(path.length + 1));
19531963
}
19541964
else {
@@ -1960,7 +1970,7 @@ class Stack {
19601970
uid: entryReferences[path],
19611971
});
19621972
}
1963-
else {
1973+
else if (entryReferences[path].length) {
19641974
entryReferences[path].forEach((contentTypeUid) => {
19651975
schemasReferred.push({
19661976
_content_type_uid: this.types.content_types,
@@ -2069,12 +2079,23 @@ class Stack {
20692079
oldEntryQueries = null;
20702080
for (let i = 0, j = oldObjectPointerList.length; i < j; i++) {
20712081
const element = oldObjectPointerList[i];
2082+
let flag = true;
20722083
for (let k = 0, l = result.length; k < l; k++) {
20732084
if (result[k].uid === element.uid) {
20742085
element.path[element.position] = result[k];
2086+
flag = false;
20752087
break;
20762088
}
20772089
}
2090+
if (flag) {
2091+
for (let e = 0, f = oldObjectPointerList[i].path.length; e < f; e++) {
2092+
// tslint:disable-next-line: max-line-length
2093+
if (oldObjectPointerList[i].path[e].hasOwnProperty('_content_type_uid') && Object.keys(oldObjectPointerList[i].path[e]).length === 2) {
2094+
oldObjectPointerList[i].path.splice(e, 1);
2095+
break;
2096+
}
2097+
}
2098+
}
20782099
}
20792100
// GC to avoid mem leaks!
20802101
oldObjectPointerList = null;

example/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ const Stack = Contentstack.Stack({
1313
})
1414

1515
function connect () {
16-
return new Promise((resolve, reject) => {
17-
return Stack.connect()
18-
.then(resolve)
19-
.catch(reject)
20-
})
16+
return Stack.connect()
2117
}
2218

2319
function close () {
@@ -57,4 +53,4 @@ return connect()
5753
})
5854
})
5955
.then(close)
60-
.catch(console.error)
56+
.catch(console.error)

src/stack.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,8 @@ export class Stack {
16521652
this.q.query = {}
16531653
}
16541654

1655-
this.q.referenceDepth = this.q.referenceDepth || this.contentStore.referenceDepth
1655+
// tslint:disable-next-line: max-line-length
1656+
this.q.referenceDepth = (typeof this.q.referenceDepth === 'number') ? this.q.referenceDepth : this.contentStore.referenceDepth
16561657

16571658
if (this.internal.only) {
16581659
this.internal.projections = this.internal.only
@@ -1780,7 +1781,6 @@ export class Stack {
17801781
})
17811782
}
17821783

1783-
// console.log('this.internal.includeSchema', this.internal.includeSchema)
17841784
if (this.internal.includeSchema) {
17851785
output.content_type = await this.db.collection(getCollectionName({
17861786
content_type_uid: this.types.content_types,
@@ -1898,7 +1898,6 @@ export class Stack {
18981898
$or: [],
18991899
} // reference field paths
19001900
const shelf = [] // a mapper object, that holds pointer to the original element
1901-
19021901
// iterate over each path in the entries and fetch the references
19031902
// while fetching, keep track of their location
19041903
for (let i = 0, j = paths.length; i < j; i++) {
@@ -2023,12 +2022,24 @@ export class Stack {
20232022

20242023
for (let i = 0, j = oldShelf.length; i < j; i++) {
20252024
const element: IShelf = oldShelf[i]
2025+
let flag = true
20262026
for (let k = 0, l = result.length; k < l; k++) {
20272027
if (result[k].uid === element.uid) {
20282028
element.path[element.position] = result[k]
2029+
flag = false
20292030
break
20302031
}
20312032
}
2033+
2034+
if (flag) {
2035+
for (let e = 0, f = oldShelf[i].path.length; e < f; e++) {
2036+
// tslint:disable-next-line: max-line-length
2037+
if (oldShelf[i].path[e].hasOwnProperty('_content_type_uid') && Object.keys(oldShelf[i].path[e]).length === 2) {
2038+
(oldShelf[i].path as any).splice(e, 1)
2039+
break
2040+
}
2041+
}
2042+
}
20322043
}
20332044

20342045
// GC to avoid mem leaks!
@@ -2083,13 +2094,12 @@ export class Stack {
20832094
const includePath = currentInclude[i]
20842095
// tslint:disable-next-line: forin
20852096
for (const path in entryReferences) {
2086-
const idx = includePath.indexOf(path)
2087-
// tslint:disable-next-line: no-bitwise
2088-
if (~idx) {
2097+
const subStr = includePath.slice(0, path.length)
2098+
if (subStr === path) {
20892099
let subPath
20902100
// Its the complete path!! Hurrah!
20912101
if (path.length !== includePath.length) {
2092-
subPath = includePath.slice(0, path.length)
2102+
subPath = subStr
20932103
pendingPath.push(includePath.slice(path.length + 1))
20942104
} else {
20952105
subPath = includePath
@@ -2100,8 +2110,7 @@ export class Stack {
21002110
_content_type_uid: this.types.content_types,
21012111
uid: entryReferences[path],
21022112
})
2103-
} else {
2104-
2113+
} else if (entryReferences[path].length) {
21052114
entryReferences[path].forEach((contentTypeUid) => {
21062115
schemasReferred.push({
21072116
_content_type_uid: this.types.content_types,
@@ -2230,13 +2239,26 @@ export class Stack {
22302239

22312240
for (let i = 0, j = oldObjectPointerList.length; i < j; i++) {
22322241
const element: IShelf = oldObjectPointerList[i]
2242+
let flag = true
22332243
for (let k = 0, l = result.length; k < l; k++) {
22342244
if (result[k].uid === element.uid) {
22352245
element.path[element.position] = result[k]
2246+
flag = false
22362247
break
22372248
}
22382249
}
2250+
2251+
if (flag) {
2252+
for (let e = 0, f = oldObjectPointerList[i].path.length; e < f; e++) {
2253+
// tslint:disable-next-line: max-line-length
2254+
if (oldObjectPointerList[i].path[e].hasOwnProperty('_content_type_uid') && Object.keys(oldObjectPointerList[i].path[e]).length === 2) {
2255+
(oldObjectPointerList[i].path as any).splice(e, 1)
2256+
break
2257+
}
2258+
}
2259+
}
22392260
}
2261+
22402262
// GC to avoid mem leaks!
22412263
oldObjectPointerList = null
22422264

0 commit comments

Comments
 (0)