diff --git a/README.md b/README.md index 7792997..7133b2b 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,18 @@ Contentstack is a headless CMS with an API-first approach. It is a CMS that deve ### Prerequisite -- nodejs, v8+ +- Nodejs, v8+ - MongoDB, v3.6 or higher - You should have the data synced through [Contentstack DataSync](https://www.contentstack.com/docs/guide/synchronization/contentstack-datasync) +**Note** +For optimal performance, we recommend that you add indexes on the following keys of your collections +- `_content_type_uid: 1` +- `uid: 1` +- `locale: 1` + +- `updated_at: -1` + ### Configuration |Property|Type|Default value|Description| diff --git a/dist/config.js b/dist/config.js index 21556c7..bbc35ad 100644 --- a/dist/config.js +++ b/dist/config.js @@ -23,6 +23,7 @@ exports.config = { types: { assets: '_assets', content_types: '_content_types', + references: '_references', }, }, limit: 100, diff --git a/dist/stack.js b/dist/stack.js index 5e3b9d8..4cce4bd 100644 --- a/dist/stack.js +++ b/dist/stack.js @@ -918,6 +918,30 @@ class Stack { // stack.collection = stack.db.collection(stack.contentStore.collectionName) return stack; } + /** + * @public + * @method contentTypes + * @description + * Query for a set of content type schemas + * @example + * Stack + * .contentTypes() + * .find() + * .then((result) => { + * // returns a set of content type schemas + * }) + * .catch((error) => { + * // handle query errors + * }) + * + * @returns {Stack} Returns an instance of 'stack' + */ + contentTypes() { + const stack = new Stack(this.config, this.db); + stack.q.content_type_uid = this.types.content_types; + // stack.collection = stack.db.collection(stack.contentStore.collectionName) + return stack; + } /** * @public * @method limit @@ -1300,7 +1324,7 @@ class Stack { * @public * @method excludeReferences * @description - * Excludes all references of the entries being scanned + * Excludes all references of the entries being scanned. * Note: On calling this, assets will not be binded in the result being returned. * * @example @@ -1373,14 +1397,15 @@ class Stack { } /** * @public - * @method includeAllReferences + * @method includeReferences * @description - * This method would return all the references of your queried entries (until depth 4) - * Note: If you wish to increase the depth of the references fetched, call .referenceDepth() + * This method would return all the references of your queried entries (until depth 2) + * Note: If you wish to increase the depth of the references fetched, call pass a numeric parameter * @example - * Stack.contentType('blog') + * Stack + * .contentType('blog') * .entries() - * .includeAllReferences() + * .includeReferences(3) * @returns {Stack} Returns 'this' instance (of Stack) */ includeReferences(depth) { @@ -1396,7 +1421,7 @@ class Stack { * @method include * @description * Pass in reference field uids, that you want included in your result. - * If you want all the references, use .includeAllReferences() + * If you want all the references, use .includeReferences() * @example * Stack.contentType('blog') * .entries() @@ -1528,6 +1553,7 @@ class Stack { /** * @public * @method findOne + * @deprecated - Use .fetch() instead * @description * Queries the db using the query built/passed. Returns a single entry/asset/content type object * Does all the processing, filtering, referencing after querying the DB @@ -1545,6 +1571,26 @@ class Stack { this.internal.single = true; return this.find(query); } + /** + * @public + * @method fetch + * @description + * Queries the db using the query built/passed. Returns a single entry/asset/content type object + * Does all the processing, filtering, referencing after querying the DB + * @param {object} query Optional query object, that overrides all the previously build queries + * + * @example + * Stack + * .contentType('blog') + * .entries() + * .fetch() + * + * @returns {object} - Returns an object, that has been processed, filtered and referenced + */ + fetch(query = {}) { + this.internal.single = true; + return this.find(query); + } /** * @private * @method preProcess @@ -1708,11 +1754,10 @@ class Stack { _assets: 1, _id: 0, }); - if (schema === null) { + if (schema === null || schema[this.types.assets] !== 'object') { return; } - const assetPaths = schema._assets; - const paths = Object.keys(assetPaths); + const paths = Object.keys(schema[this.types.assets]); const shelf = []; const queryBucket = { $or: [], @@ -1759,22 +1804,6 @@ class Stack { _content_type_uid: this.types.content_types, uid: contentTypeUid, }; - /** - * key: { - * $in_query: { - * $or: [ - * { - * range: { $in: [10, 100] } - * }, - * { - * key2: { - * $in_query: ... - * } - * } - * ] - * } - * } - */ const { paths, // ref. fields in the current content types pendingPath, // left over of *paths* schemaList, } = yield this.getReferencePath(ctQuery, locale, include); @@ -1943,9 +1972,9 @@ class Stack { let entryReferences = {}; schemas.forEach((schema) => { // Entry references - entryReferences = lodash_1.merge(entryReferences, schema._references); + entryReferences = lodash_1.merge(entryReferences, schema[this.types.references]); // tslint:disable-next-line: forin - for (const path in schema._assets) { + for (const path in schema[this.types.assets]) { paths.push(path); } }); @@ -2126,23 +2155,23 @@ class Stack { let assetFieldPaths; let entryReferencePaths; if (contents[i].hasOwnProperty(this.types.assets)) { - assetFieldPaths = Object.keys(contents[i]._assets); + assetFieldPaths = Object.keys(contents[i][this.types.assets]); paths = paths.concat(assetFieldPaths); } if (contents[i].hasOwnProperty('_references')) { - entryReferencePaths = Object.keys(contents[i]._references); + entryReferencePaths = Object.keys(contents[i][this.types.references]); paths = paths.concat(entryReferencePaths); for (let k = 0, l = entryReferencePaths.length; k < l; k++) { - if (typeof contents[i]._references[entryReferencePaths[k]] === 'string') { + if (typeof contents[i][this.types.references][entryReferencePaths[k]] === 'string') { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // this would probably make it slow in FS, avoid this there? // locale, - uid: contents[i]._references[entryReferencePaths[k]], + uid: contents[i][this.types.references][entryReferencePaths[k]], }); } - else if (contents[i]._references[entryReferencePaths[k]].length) { - contents[i]._references[entryReferencePaths[k]].forEach((uid) => { + else if (contents[i][this.types.references][entryReferencePaths[k]].length) { + contents[i][this.types.references][entryReferencePaths[k]].forEach((uid) => { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // avoiding locale here, not sure if its required diff --git a/docs/global.html b/docs/global.html index a55434d..797fdca 100644 --- a/docs/global.html +++ b/docs/global.html @@ -1390,6 +1390,129 @@
Example
+

contentTypes() → {Stack}

+ + + + + + +
+ Query for a set of content type schemas +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Returns an instance of 'stack' +
+ + + +
+
+ Type +
+
+ +Stack + + +
+
+ + + + + + +
Example
+ +
Stack
+ .contentTypes()
+ .find()
+ .then((result) => {
+   // returns a set of content type schemas
+ })
+ .catch((error) => {
+   // handle query errors
+ })
+ + + + + + + + +

count(query) → {object}

@@ -1487,7 +1610,7 @@
Parameters:
Source:
@@ -2135,7 +2258,7 @@
Parameters:
Source:
@@ -2216,7 +2339,7 @@

excl
- Excludes all references of the entries being scanned + Excludes all references of the entries being scanned. Note: On calling this, assets will not be binded in the result being returned.
@@ -2261,7 +2384,7 @@

excl
Source:
@@ -2540,6 +2663,174 @@

Example
+

fetch(query) → {object}

+ + + + + + +
+ Queries the db using the query built/passed. Returns a single entry/asset/content type object +Does all the processing, filtering, referencing after querying the DB +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
query + + +object + + + + Optional query object, that overrides all the previously build queries
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ - Returns an object, that has been processed, filtered and referenced +
+ + + +
+
+ Type +
+
+ +object + + +
+
+ + + + + + +
Example
+ +
Stack
+ .contentType('blog')
+ .entries()
+ .fetch()
+ + + + + + + + +

find(query) → {object}

@@ -2643,7 +2934,7 @@
Parameters:
Source:
@@ -2805,6 +3096,8 @@
Parameters:
+
Deprecated:
+ @@ -2817,7 +3110,7 @@
Parameters:
Source:
@@ -2935,7 +3228,7 @@

getQuerySource:
@@ -3415,7 +3708,7 @@

include Pass in reference field uids, that you want included in your result. -If you want all the references, use .includeAllReferences() +If you want all the references, use .includeReferences() @@ -3508,7 +3801,7 @@
Parameters:
Source:
@@ -3573,7 +3866,7 @@
Example
-

includeAllReferences() → {Stack}

+

includeContentType() → {Stack}

@@ -3581,8 +3874,7 @@

i
- This method would return all the references of your queried entries (until depth 4) -Note: If you wish to increase the depth of the references fetched, call .referenceDepth() + Includes 'content_type' key in response, which is the content type schema of the entries filtered/scanned
@@ -3626,7 +3918,7 @@

i
Source:
@@ -3655,7 +3947,7 @@

Returns:
- Returns 'this' instance (of Stack) + Returns an instance of 'stack'
@@ -3679,9 +3971,17 @@
Returns:
Example
-
Stack.contentType('blog')
+    
Stack
+ .contentType('blog')
  .entries()
- .includeAllReferences()
+ .includeContentType() + .find() + .then((result) => { + // returns entries, along with a 'content_type' property, which is 'blog' content type's schema + }) + .catch((error) => { + // handle query errors + })
@@ -3691,7 +3991,7 @@
Example
-

includeContentType() → {Stack}

+

includeCount() → {Stack}

@@ -3699,7 +3999,7 @@

inc
- Includes 'content_type' key in response, which is the content type schema of the entries filtered/scanned + Includes 'count' key in response, which is the total count of the items being returned
@@ -3743,7 +4043,7 @@

inc
Source:
@@ -3799,10 +4099,10 @@

Example
Stack
  .contentType('blog')
  .entries()
- .includeContentType()
+ .includeCount()
  .find()
  .then((result) => {
-   // returns entries, along with a 'content_type' property, which is 'blog' content type's schema
+   // returns entries, along with a 'count' property, with the total count of entries being returned
  })
  .catch((error) => {
    // handle query errors
@@ -3816,7 +4116,7 @@ 
Example
-

includeCount() → {Stack}

+

includeReferences() → {Stack}

@@ -3824,7 +4124,8 @@

includeCo
- Includes 'count' key in response, which is the total count of the items being returned + This method would return all the references of your queried entries (until depth 2) +Note: If you wish to increase the depth of the references fetched, call pass a numeric parameter
@@ -3868,7 +4169,7 @@

includeCo
Source:
@@ -3897,7 +4198,7 @@

Returns:
- Returns an instance of 'stack' + Returns 'this' instance (of Stack)
@@ -3924,14 +4225,7 @@
Example
Stack
  .contentType('blog')
  .entries()
- .includeCount()
- .find()
- .then((result) => {
-   // returns entries, along with a 'count' property, with the total count of entries being returned
- })
- .catch((error) => {
-   // handle query errors
- })
+ .includeReferences(3)
@@ -4622,7 +4916,7 @@
Parameters:
Source:
@@ -5416,7 +5710,7 @@
Parameters:
Source:
@@ -5769,7 +6063,7 @@
Parameters:
Source:
@@ -5898,7 +6192,7 @@

queryR
Source:
@@ -6118,7 +6412,7 @@

Parameters:
Source:
@@ -6589,7 +6883,7 @@
Parameters:
Source:
@@ -6942,7 +7236,7 @@
Parameters:
Source:
@@ -7126,7 +7420,7 @@
Parameters:
Source:
@@ -7211,13 +7505,13 @@
Example

- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:27:42 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.3 on Sat Aug 03 2019 21:26:39 GMT+0530 (India Standard Time)
diff --git a/docs/global.html#Stack b/docs/global.html#Stack index 921ff2a..614ff1e 100644 --- a/docs/global.html#Stack +++ b/docs/global.html#Stack @@ -223,7 +223,7 @@
Source:
@@ -464,7 +464,7 @@
Source:
@@ -746,7 +746,7 @@
Source:
@@ -987,7 +987,7 @@
Source:
@@ -1055,13 +1055,13 @@
- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:27:42 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.3 on Sat Aug 03 2019 21:26:39 GMT+0530 (India Standard Time)
diff --git a/docs/index.html b/docs/index.html index 78ee0d9..513ceea 100644 --- a/docs/index.html +++ b/docs/index.html @@ -276,13 +276,13 @@

License


- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:27:42 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.3 on Sat Aug 03 2019 21:26:39 GMT+0530 (India Standard Time)
diff --git a/docs/index.js.html b/docs/index.js.html index bd2a58e..0aa4e21 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -77,13 +77,13 @@

Source: index.js


- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:27:42 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.3 on Sat Aug 03 2019 21:26:39 GMT+0530 (India Standard Time)
diff --git a/docs/stack.js.html b/docs/stack.js.html index f809aa7..e434a35 100644 --- a/docs/stack.js.html +++ b/docs/stack.js.html @@ -946,6 +946,30 @@

Source: stack.js

// stack.collection = stack.db.collection(stack.contentStore.collectionName) return stack; } + /** + * @public + * @method contentTypes + * @description + * Query for a set of content type schemas + * @example + * Stack + * .contentTypes() + * .find() + * .then((result) => { + * // returns a set of content type schemas + * }) + * .catch((error) => { + * // handle query errors + * }) + * + * @returns {Stack} Returns an instance of 'stack' + */ + contentTypes() { + const stack = new Stack(this.config, this.db); + stack.q.content_type_uid = this.types.content_types; + // stack.collection = stack.db.collection(stack.contentStore.collectionName) + return stack; + } /** * @public * @method limit @@ -1328,7 +1352,7 @@

Source: stack.js

* @public * @method excludeReferences * @description - * Excludes all references of the entries being scanned + * Excludes all references of the entries being scanned. * Note: On calling this, assets will not be binded in the result being returned. * * @example @@ -1401,14 +1425,15 @@

Source: stack.js

} /** * @public - * @method includeAllReferences + * @method includeReferences * @description - * This method would return all the references of your queried entries (until depth 4) - * Note: If you wish to increase the depth of the references fetched, call .referenceDepth() + * This method would return all the references of your queried entries (until depth 2) + * Note: If you wish to increase the depth of the references fetched, call pass a numeric parameter * @example - * Stack.contentType('blog') + * Stack + * .contentType('blog') * .entries() - * .includeAllReferences() + * .includeReferences(3) * @returns {Stack} Returns 'this' instance (of Stack) */ includeReferences(depth) { @@ -1424,7 +1449,7 @@

Source: stack.js

* @method include * @description * Pass in reference field uids, that you want included in your result. - * If you want all the references, use .includeAllReferences() + * If you want all the references, use .includeReferences() * @example * Stack.contentType('blog') * .entries() @@ -1556,6 +1581,7 @@

Source: stack.js

/** * @public * @method findOne + * @deprecated - Use .fetch() instead * @description * Queries the db using the query built/passed. Returns a single entry/asset/content type object * Does all the processing, filtering, referencing after querying the DB @@ -1573,6 +1599,26 @@

Source: stack.js

this.internal.single = true; return this.find(query); } + /** + * @public + * @method fetch + * @description + * Queries the db using the query built/passed. Returns a single entry/asset/content type object + * Does all the processing, filtering, referencing after querying the DB + * @param {object} query Optional query object, that overrides all the previously build queries + * + * @example + * Stack + * .contentType('blog') + * .entries() + * .fetch() + * + * @returns {object} - Returns an object, that has been processed, filtered and referenced + */ + fetch(query = {}) { + this.internal.single = true; + return this.find(query); + } /** * @private * @method preProcess @@ -1588,7 +1634,8 @@

Source: stack.js

else { this.q.query = {}; } - this.q.referenceDepth = this.q.referenceDepth || this.contentStore.referenceDepth; + // tslint:disable-next-line: max-line-length + this.q.referenceDepth = (typeof this.q.referenceDepth === 'number') ? this.q.referenceDepth : this.contentStore.referenceDepth; if (this.internal.only) { this.internal.projections = this.internal.only; } @@ -1703,7 +1750,6 @@

Source: stack.js

_content_type_uid: this.q.content_type_uid, }); } - // console.log('this.internal.includeSchema', this.internal.includeSchema) if (this.internal.includeSchema) { output.content_type = yield this.db.collection(util_1.getCollectionName({ content_type_uid: this.types.content_types, @@ -1736,11 +1782,10 @@

Source: stack.js

_assets: 1, _id: 0, }); - if (schema === null) { + if (schema === null || schema[this.types.assets] !== 'object') { return; } - const assetPaths = schema._assets; - const paths = Object.keys(assetPaths); + const paths = Object.keys(schema[this.types.assets]); const shelf = []; const queryBucket = { $or: [], @@ -1787,22 +1832,6 @@

Source: stack.js

_content_type_uid: this.types.content_types, uid: contentTypeUid, }; - /** - * key: { - * $in_query: { - * $or: [ - * { - * range: { $in: [10, 100] } - * }, - * { - * key2: { - * $in_query: ... - * } - * } - * ] - * } - * } - */ const { paths, // ref. fields in the current content types pendingPath, // left over of *paths* schemaList, } = yield this.getReferencePath(ctQuery, locale, include); @@ -1917,12 +1946,23 @@

Source: stack.js

eQuery = null; for (let i = 0, j = oldShelf.length; i < j; i++) { const element = oldShelf[i]; + let flag = true; for (let k = 0, l = result.length; k < l; k++) { if (result[k].uid === element.uid) { element.path[element.position] = result[k]; + flag = false; break; } } + if (flag) { + for (let e = 0, f = oldShelf[i].path.length; e < f; e++) { + // tslint:disable-next-line: max-line-length + if (oldShelf[i].path[e].hasOwnProperty('_content_type_uid') && Object.keys(oldShelf[i].path[e]).length === 2) { + oldShelf[i].path.splice(e, 1); + break; + } + } + } } // GC to avoid mem leaks! oldShelf = null; @@ -1960,9 +2000,9 @@

Source: stack.js

let entryReferences = {}; schemas.forEach((schema) => { // Entry references - entryReferences = lodash_1.merge(entryReferences, schema._references); + entryReferences = lodash_1.merge(entryReferences, schema[this.types.references]); // tslint:disable-next-line: forin - for (const path in schema._assets) { + for (const path in schema[this.types.assets]) { paths.push(path); } }); @@ -1970,13 +2010,12 @@

Source: stack.js

const includePath = currentInclude[i]; // tslint:disable-next-line: forin for (const path in entryReferences) { - const idx = includePath.indexOf(path); - // tslint:disable-next-line: no-bitwise - if (~idx) { + const subStr = includePath.slice(0, path.length); + if (subStr === path) { let subPath; // Its the complete path!! Hurrah! if (path.length !== includePath.length) { - subPath = includePath.slice(0, path.length); + subPath = subStr; pendingPath.push(includePath.slice(path.length + 1)); } else { @@ -1988,7 +2027,7 @@

Source: stack.js

uid: entryReferences[path], }); } - else { + else if (entryReferences[path].length) { entryReferences[path].forEach((contentTypeUid) => { schemasReferred.push({ _content_type_uid: this.types.content_types, @@ -2097,12 +2136,23 @@

Source: stack.js

oldEntryQueries = null; for (let i = 0, j = oldObjectPointerList.length; i < j; i++) { const element = oldObjectPointerList[i]; + let flag = true; for (let k = 0, l = result.length; k < l; k++) { if (result[k].uid === element.uid) { element.path[element.position] = result[k]; + flag = false; break; } } + if (flag) { + for (let e = 0, f = oldObjectPointerList[i].path.length; e < f; e++) { + // tslint:disable-next-line: max-line-length + if (oldObjectPointerList[i].path[e].hasOwnProperty('_content_type_uid') && Object.keys(oldObjectPointerList[i].path[e]).length === 2) { + oldObjectPointerList[i].path.splice(e, 1); + break; + } + } + } } // GC to avoid mem leaks! oldObjectPointerList = null; @@ -2133,23 +2183,23 @@

Source: stack.js

let assetFieldPaths; let entryReferencePaths; if (contents[i].hasOwnProperty(this.types.assets)) { - assetFieldPaths = Object.keys(contents[i]._assets); + assetFieldPaths = Object.keys(contents[i][this.types.assets]); paths = paths.concat(assetFieldPaths); } if (contents[i].hasOwnProperty('_references')) { - entryReferencePaths = Object.keys(contents[i]._references); + entryReferencePaths = Object.keys(contents[i][this.types.references]); paths = paths.concat(entryReferencePaths); for (let k = 0, l = entryReferencePaths.length; k < l; k++) { - if (typeof contents[i]._references[entryReferencePaths[k]] === 'string') { + if (typeof contents[i][this.types.references][entryReferencePaths[k]] === 'string') { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // this would probably make it slow in FS, avoid this there? // locale, - uid: contents[i]._references[entryReferencePaths[k]], + uid: contents[i][this.types.references][entryReferencePaths[k]], }); } - else if (contents[i]._references[entryReferencePaths[k]].length) { - contents[i]._references[entryReferencePaths[k]].forEach((uid) => { + else if (contents[i][this.types.references][entryReferencePaths[k]].length) { + contents[i][this.types.references][entryReferencePaths[k]].forEach((uid) => { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // avoiding locale here, not sure if its required @@ -2179,13 +2229,13 @@

Source: stack.js


- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:27:42 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.3 on Sat Aug 03 2019 21:26:39 GMT+0530 (India Standard Time)
diff --git a/package.json b/package.json index e22be0a..0b25b7e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "author": "Contentstack Ecosystem ", "name": "datasync-mongodb-sdk", - "version": "1.0.0", + "version": "1.0.1", "description": "Mongodb query wrapper around contents synced via @contentstack/content-store-mongodb", "main": "dist/index.js", "scripts": { diff --git a/src/config.ts b/src/config.ts index 507bec5..c3e1a00 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,6 +22,7 @@ export const config = { types: { assets: '_assets', content_types: '_content_types', + references: '_references', }, }, limit: 100, diff --git a/src/stack.ts b/src/stack.ts index 3d3e92a..c59a6ae 100644 --- a/src/stack.ts +++ b/src/stack.ts @@ -978,6 +978,32 @@ export class Stack { return stack } + /** + * @public + * @method contentTypes + * @description + * Query for a set of content type schemas + * @example + * Stack + * .contentTypes() + * .find() + * .then((result) => { + * // returns a set of content type schemas + * }) + * .catch((error) => { + * // handle query errors + * }) + * + * @returns {Stack} Returns an instance of 'stack' + */ + public contentTypes() { + const stack = new Stack(this.config, this.db) + stack.q.content_type_uid = this.types.content_types + // stack.collection = stack.db.collection(stack.contentStore.collectionName) + + return stack + } + /** * @public * @method limit @@ -1380,7 +1406,7 @@ export class Stack { * @public * @method excludeReferences * @description - * Excludes all references of the entries being scanned + * Excludes all references of the entries being scanned. * Note: On calling this, assets will not be binded in the result being returned. * * @example @@ -1461,14 +1487,15 @@ export class Stack { /** * @public - * @method includeAllReferences + * @method includeReferences * @description - * This method would return all the references of your queried entries (until depth 4) - * Note: If you wish to increase the depth of the references fetched, call .referenceDepth() + * This method would return all the references of your queried entries (until depth 2) + * Note: If you wish to increase the depth of the references fetched, call pass a numeric parameter * @example - * Stack.contentType('blog') + * Stack + * .contentType('blog') * .entries() - * .includeAllReferences() + * .includeReferences(3) * @returns {Stack} Returns 'this' instance (of Stack) */ public includeReferences(depth?: number) { @@ -1486,7 +1513,7 @@ export class Stack { * @method include * @description * Pass in reference field uids, that you want included in your result. - * If you want all the references, use .includeAllReferences() + * If you want all the references, use .includeReferences() * @example * Stack.contentType('blog') * .entries() @@ -1618,6 +1645,7 @@ export class Stack { /** * @public * @method findOne + * @deprecated - Use .fetch() instead * @description * Queries the db using the query built/passed. Returns a single entry/asset/content type object * Does all the processing, filtering, referencing after querying the DB @@ -1637,6 +1665,28 @@ export class Stack { return this.find(query) } + /** + * @public + * @method fetch + * @description + * Queries the db using the query built/passed. Returns a single entry/asset/content type object + * Does all the processing, filtering, referencing after querying the DB + * @param {object} query Optional query object, that overrides all the previously build queries + * + * @example + * Stack + * .contentType('blog') + * .entries() + * .fetch() + * + * @returns {object} - Returns an object, that has been processed, filtered and referenced + */ + public fetch(query = {}) { + this.internal.single = true + + return this.find(query) + } + /** * @private * @method preProcess @@ -1815,12 +1865,11 @@ export class Stack { _id: 0, }) - if (schema === null) { + if (schema === null || schema[this.types.assets] !== 'object') { return } - const assetPaths = schema._assets - const paths = Object.keys(assetPaths) + const paths = Object.keys(schema[this.types.assets]) const shelf = [] const queryBucket = { $or: [], @@ -1871,22 +1920,6 @@ export class Stack { _content_type_uid: this.types.content_types, uid: contentTypeUid, } - /** - * key: { - * $in_query: { - * $or: [ - * { - * range: { $in: [10, 100] } - * }, - * { - * key2: { - * $in_query: ... - * } - * } - * ] - * } - * } - */ const { paths, // ref. fields in the current content types @@ -2083,9 +2116,9 @@ export class Stack { schemas.forEach((schema) => { // Entry references - entryReferences = merge(entryReferences, schema._references) + entryReferences = merge(entryReferences, schema[this.types.references]) // tslint:disable-next-line: forin - for (const path in schema._assets) { + for (const path in schema[this.types.assets]) { paths.push(path) } }) @@ -2292,23 +2325,23 @@ export class Stack { let assetFieldPaths: string[] let entryReferencePaths: string[] if (contents[i].hasOwnProperty(this.types.assets)) { - assetFieldPaths = Object.keys(contents[i]._assets) + assetFieldPaths = Object.keys(contents[i][this.types.assets]) paths = paths.concat(assetFieldPaths) } if (contents[i].hasOwnProperty('_references')) { - entryReferencePaths = Object.keys(contents[i]._references) + entryReferencePaths = Object.keys(contents[i][this.types.references]) paths = paths.concat(entryReferencePaths) for (let k = 0, l = entryReferencePaths.length; k < l; k++) { - if (typeof contents[i]._references[entryReferencePaths[k]] === 'string') { + if (typeof contents[i][this.types.references][entryReferencePaths[k]] === 'string') { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // this would probably make it slow in FS, avoid this there? // locale, - uid: contents[i]._references[entryReferencePaths[k]], + uid: contents[i][this.types.references][entryReferencePaths[k]], }) - } else if (contents[i]._references[entryReferencePaths[k]].length) { - contents[i]._references[entryReferencePaths[k]].forEach((uid) => { + } else if (contents[i][this.types.references][entryReferencePaths[k]].length) { + contents[i][this.types.references][entryReferencePaths[k]].forEach((uid) => { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // avoiding locale here, not sure if its required diff --git a/typings/config.d.ts b/typings/config.d.ts index 1932874..fe295ea 100644 --- a/typings/config.d.ts +++ b/typings/config.d.ts @@ -21,6 +21,7 @@ export declare const config: { types: { assets: string; content_types: string; + references: string; }; }; limit: number; diff --git a/typings/stack.d.ts b/typings/stack.d.ts index 8b094da..bb20684 100644 --- a/typings/stack.d.ts +++ b/typings/stack.d.ts @@ -597,6 +597,25 @@ export declare class Stack { * @returns {Stack} Returns an instance of 'stack' */ schemas(): Stack; + /** + * @public + * @method contentTypes + * @description + * Query for a set of content type schemas + * @example + * Stack + * .contentTypes() + * .find() + * .then((result) => { + * // returns a set of content type schemas + * }) + * .catch((error) => { + * // handle query errors + * }) + * + * @returns {Stack} Returns an instance of 'stack' + */ + contentTypes(): Stack; /** * @public * @method limit @@ -863,7 +882,7 @@ export declare class Stack { * @public * @method excludeReferences * @description - * Excludes all references of the entries being scanned + * Excludes all references of the entries being scanned. * Note: On calling this, assets will not be binded in the result being returned. * * @example @@ -925,14 +944,15 @@ export declare class Stack { getQuery(): any; /** * @public - * @method includeAllReferences + * @method includeReferences * @description - * This method would return all the references of your queried entries (until depth 4) - * Note: If you wish to increase the depth of the references fetched, call .referenceDepth() + * This method would return all the references of your queried entries (until depth 2) + * Note: If you wish to increase the depth of the references fetched, call pass a numeric parameter * @example - * Stack.contentType('blog') + * Stack + * .contentType('blog') * .entries() - * .includeAllReferences() + * .includeReferences(3) * @returns {Stack} Returns 'this' instance (of Stack) */ includeReferences(depth?: number): this; @@ -941,7 +961,7 @@ export declare class Stack { * @method include * @description * Pass in reference field uids, that you want included in your result. - * If you want all the references, use .includeAllReferences() + * If you want all the references, use .includeReferences() * @example * Stack.contentType('blog') * .entries() @@ -998,6 +1018,7 @@ export declare class Stack { /** * @public * @method findOne + * @deprecated - Use .fetch() instead * @description * Queries the db using the query built/passed. Returns a single entry/asset/content type object * Does all the processing, filtering, referencing after querying the DB @@ -1012,6 +1033,23 @@ export declare class Stack { * @returns {object} - Returns an object, that has been processed, filtered and referenced */ findOne(query?: {}): Promise; + /** + * @public + * @method fetch + * @description + * Queries the db using the query built/passed. Returns a single entry/asset/content type object + * Does all the processing, filtering, referencing after querying the DB + * @param {object} query Optional query object, that overrides all the previously build queries + * + * @example + * Stack + * .contentType('blog') + * .entries() + * .fetch() + * + * @returns {object} - Returns an object, that has been processed, filtered and referenced + */ + fetch(query?: {}): Promise; /** * @private * @method preProcess