Skip to content

Commit 1627b81

Browse files
committed
v1.0.0 update. .includeAllReferences() + .include() + test case updated
1 parent cbd9fb1 commit 1627b81

21 files changed

+1430
-1373
lines changed

dist/stack.js

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ class Stack {
14711471
// Ignore references include, for empty list, exclude call, content type & assets
14721472
if (result.length === 0 || this.internal.excludeReferences || this.q.content_type_uid === this
14731473
.types.content_types || this.q.content_type_uid
1474-
=== this.types.assets) {
1474+
=== this.types.assets || this.internal.onlyCount) {
14751475
// Do nothing
14761476
}
14771477
else if (this.internal.includeSpecificReferences) {
@@ -1485,7 +1485,7 @@ class Stack {
14851485
yield this.includeAssetsOnly(result, this.q.content_type_uid, this.q.locale);
14861486
}
14871487
if (this.internal.queryReferences) {
1488-
result = sift_1.default(this.internal.queryReferences, result);
1488+
result = result.filter(sift_1.default(this.internal.queryReferences));
14891489
if (this.internal.skip) {
14901490
result = result.splice(this.internal.skip, this.internal.limit);
14911491
}
@@ -1522,21 +1522,9 @@ class Stack {
15221522
* @returns {object} Returns count of the entries/asset's matched
15231523
*/
15241524
count(query) {
1525-
return new Promise((resolve, reject) => {
1526-
const queryFilters = this.preProcess(query);
1527-
this.collection = this.collection.find(queryFilters);
1528-
return this.collection
1529-
.count()
1530-
.then((result) => {
1531-
this.cleanup();
1532-
return resolve({
1533-
count: result,
1534-
});
1535-
})
1536-
.catch((error) => {
1537-
this.cleanup();
1538-
return reject(error);
1539-
});
1525+
return __awaiter(this, void 0, void 0, function* () {
1526+
this.internal.onlyCount = true;
1527+
return this.find(query);
15401528
});
15411529
}
15421530
/**
@@ -1635,7 +1623,13 @@ class Stack {
16351623
postProcess(result) {
16361624
return __awaiter(this, void 0, void 0, function* () {
16371625
const count = (result === null) ? 0 : result.length;
1638-
const output = {};
1626+
const output = {
1627+
locale: this.q.locale,
1628+
};
1629+
if (this.internal.onlyCount) {
1630+
output.count = count;
1631+
return output;
1632+
}
16391633
switch (this.q.content_type_uid) {
16401634
case this.types.assets:
16411635
if (this.internal.single) {
@@ -1644,8 +1638,7 @@ class Stack {
16441638
else {
16451639
output.assets = result;
16461640
}
1647-
result.content_type_uid = 'assets';
1648-
result.locale = this.q.locale;
1641+
output.content_type_uid = 'assets';
16491642
break;
16501643
case this.types.content_types:
16511644
if (this.internal.single) {
@@ -1654,7 +1647,7 @@ class Stack {
16541647
else {
16551648
output.content_types = result;
16561649
}
1657-
result.content_type_uid = 'content_types';
1650+
output.content_type_uid = 'content_types';
16581651
break;
16591652
default:
16601653
if (this.internal.single) {
@@ -1663,18 +1656,18 @@ class Stack {
16631656
else {
16641657
output.entries = result;
16651658
}
1666-
result.content_type_uid = this.q.content_type_uid;
1667-
result.locale = this.q.locale;
1659+
output.content_type_uid = this.q.content_type_uid;
16681660
break;
16691661
}
16701662
if (this.internal.includeCount) {
1671-
result.count = count;
1663+
output.count = count;
16721664
}
1665+
// console.log('this.internal.includeSchema', this.internal.includeSchema)
16731666
if (this.internal.includeSchema) {
1674-
result.content_type = yield this.db.collection({
1667+
output.content_type = yield this.db.collection(util_1.getCollectionName({
16751668
content_type_uid: '_content_types',
16761669
locale: this.q.locale,
1677-
}, this.collectionNames)
1670+
}, this.collectionNames))
16781671
.findOne({
16791672
uid: this.q.content_type_uid,
16801673
}, {
@@ -1685,7 +1678,7 @@ class Stack {
16851678
});
16861679
}
16871680
this.cleanup();
1688-
return result;
1681+
return output;
16891682
});
16901683
}
16911684
includeAssetsOnly(entries, contentTypeUid, locale) {
@@ -1772,9 +1765,6 @@ class Stack {
17721765
const { paths, // ref. fields in the current content types
17731766
pendingPath, // left over of *paths*
17741767
schemaList, } = yield this.getReferencePath(ctQuery, locale, include);
1775-
// console.log('paths: ' + paths)
1776-
// console.log('pending paths: ' + pendingPath)
1777-
// console.log('schema list: ' + JSON.stringify(schemaList))
17781768
const queries = {
17791769
$or: [],
17801770
}; // reference field paths
@@ -1784,13 +1774,10 @@ class Stack {
17841774
for (let i = 0, j = paths.length; i < j; i++) {
17851775
this.fetchPathDetails(entries, locale, paths[i].split('.'), queries, shelf, true, entries, 0);
17861776
}
1787-
// console.log('@sub queries', queries)
1788-
// console.log('@shelf', JSON.stringify(shelf))
17891777
// even after traversing, if no references were found, simply return the entries found thusfar
17901778
if (shelf.length === 0) {
17911779
return entries;
17921780
}
1793-
// console.log('@shelf-1', JSON.stringify(shelf))
17941781
// else, self-recursively iterate and fetch references
17951782
// Note: Shelf is the one holding `pointers` to the actual entry
17961783
// Once the pointer has been used, for GC, point the object to null
@@ -1799,17 +1786,13 @@ class Stack {
17991786
}
18001787
fetchPathDetails(data, locale, pathArr, queryBucket, shelf, assetsOnly = false, parent, pos, counter = 0) {
18011788
if (counter === (pathArr.length)) {
1802-
// console.log('data', data)
1803-
// console.log('parent', parent)
18041789
if (data && typeof data === 'object') {
1805-
// console.log('data is object')
18061790
if (data instanceof Array && data.length) {
1807-
// console.log('data is array')
18081791
data.forEach((elem, idx) => {
1809-
// console.log('elem', elem)
18101792
if (typeof elem === 'string') {
18111793
queryBucket.$or.push({
18121794
_content_type_uid: '_assets',
1795+
_version: { $exists: true },
18131796
locale,
18141797
uid: elem,
18151798
});
@@ -1834,7 +1817,6 @@ class Stack {
18341817
});
18351818
}
18361819
else if (typeof data === 'object') {
1837-
// console.log('data is plain object')
18381820
if (data.hasOwnProperty('_content_type_uid')) {
18391821
queryBucket.$or.push({
18401822
_content_type_uid: data._content_type_uid,
@@ -1850,13 +1832,12 @@ class Stack {
18501832
}
18511833
}
18521834
else if (typeof data === 'string') {
1853-
// console.log('data is string')
18541835
queryBucket.$or.push({
18551836
_content_type_uid: '_assets',
1837+
_version: { $exists: true },
18561838
locale,
18571839
uid: data,
18581840
});
1859-
// console.log('shelf for assets: ' + JSON.stringify(parent), pos)
18601841
shelf.push({
18611842
path: parent,
18621843
position: pos,
@@ -1866,20 +1847,17 @@ class Stack {
18661847
}
18671848
else {
18681849
const currentField = pathArr[counter];
1869-
// console.log('path not over. current field is', currentField)
18701850
counter++;
18711851
if (data instanceof Array) {
18721852
// tslint:disable-next-line: prefer-for-of
18731853
for (let i = 0; i < data.length; i++) {
18741854
if (data[i][currentField]) {
1875-
// console.log('data was array, lookin for field in data.current field')
18761855
this.fetchPathDetails(data[i][currentField], locale, pathArr, queryBucket, shelf, assetsOnly, data[i], currentField, counter);
18771856
}
18781857
}
18791858
}
18801859
else {
18811860
if (data[currentField]) {
1882-
// console.log('data was object, lookin for field in data.current field')
18831861
this.fetchPathDetails(data[currentField], locale, pathArr, queryBucket, shelf, assetsOnly, data, currentField, counter);
18841862
}
18851863
}
@@ -1893,15 +1871,9 @@ class Stack {
18931871
return;
18941872
}
18951873
const { paths, pendingPath, schemaList, } = yield this.getReferencePath(ctQuery, locale, include);
1896-
// console.log('@paths 2', JSON.stringify(paths))
1897-
// console.log('@pending path', pendingPath)
1898-
// console.log('@schema list 2', JSON.stringify(schemaList))
18991874
const { result, queries, shelf, } = yield this.fetchEntries(eQuery, locale, paths, include);
19001875
// GC to avoid mem leaks!
1901-
// eQuery = null
1902-
// console.log('@entries 2', JSON.stringify(result))
1903-
// console.log('@queries 2', queries)
1904-
// console.log('@shelf-2', JSON.stringify(shelf))
1876+
eQuery = null;
19051877
for (let i = 0, j = oldShelf.length; i < j; i++) {
19061878
const element = oldShelf[i];
19071879
for (let k = 0, l = result.length; k < l; k++) {
@@ -1958,9 +1930,9 @@ class Stack {
19581930
// tslint:disable-next-line: forin
19591931
for (const path in entryReferences) {
19601932
const idx = includePath.indexOf(path);
1933+
// tslint:disable-next-line: no-bitwise
19611934
if (~idx) {
19621935
let subPath;
1963-
// console.log('path vs includePath', path, includePath)
19641936
// Its the complete path!! Hurrah!
19651937
if (path.length !== includePath.length) {
19661938
subPath = includePath.slice(0, path.length);
@@ -2001,7 +1973,6 @@ class Stack {
20011973
}
20021974
fetchEntries(query, locale, paths, include, includeAll = false) {
20031975
return __awaiter(this, void 0, void 0, function* () {
2004-
// console.log('@fetch entries query', JSON.stringify(query))
20051976
const result = yield this.db.collection(util_1.getCollectionName({
20061977
content_type_uid: 'entries',
20071978
locale,
@@ -2053,8 +2024,6 @@ class Stack {
20532024
};
20542025
const { paths, // ref. fields in the current content types
20552026
ctQueries, } = yield this.getAllReferencePaths(ctQuery, locale);
2056-
// console.log('paths: ' + paths)
2057-
// console.log('schema list: ' + JSON.stringify(schemaList))
20582027
const queries = {
20592028
$or: [],
20602029
}; // reference field paths
@@ -2064,13 +2033,10 @@ class Stack {
20642033
for (let i = 0, j = paths.length; i < j; i++) {
20652034
this.fetchPathDetails(entries, locale, paths[i].split('.'), queries, objectPointerList, true, entries, 0);
20662035
}
2067-
// console.log('@sub queries', queries)
2068-
// console.log('@objectPointerList', JSON.stringify(objectPointerList))
20692036
// even after traversing, if no references were found, simply return the entries found thusfar
20702037
if (objectPointerList.length === 0) {
20712038
return entries;
20722039
}
2073-
// console.log('@shelf-1', JSON.stringify(shelf))
20742040
// else, self-recursively iterate and fetch references
20752041
// Note: Shelf is the one holding `pointers` to the actual entry
20762042
// Once the pointer has been used, for GC, point the object to null
@@ -2083,7 +2049,6 @@ class Stack {
20832049
return;
20842050
}
20852051
const { ctQueries, paths, } = yield this.getAllReferencePaths(oldCtQueries, locale);
2086-
// console.log('@paths', paths)
20872052
// GC to aviod mem leaks
20882053
oldCtQueries = null;
20892054
const { result, queries, shelf, } = yield this.fetchEntries(oldEntryQueries, locale, paths, [], true);
@@ -2124,8 +2089,6 @@ class Stack {
21242089
};
21252090
let paths = [];
21262091
for (let i = 0, j = contents.length; i < j; i++) {
2127-
// console.log('_assets', contents[i]._assets)
2128-
// console.log('references', contents[i]._references)
21292092
let assetFieldPaths;
21302093
let entryReferencePaths;
21312094
if (contents[i].hasOwnProperty('_assets')) {
@@ -2157,8 +2120,6 @@ class Stack {
21572120
}
21582121
}
21592122
}
2160-
// console.log('@paths later..', JSON.stringify(paths))
2161-
// console.log('@ctQueries later..', JSON.stringify(ctQueries))
21622123
return {
21632124
ctQueries,
21642125
paths,

0 commit comments

Comments
 (0)