diff --git a/README.md b/README.md index aeb8d7d7..8d9f1c7c 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ Contentstack is a headless CMS with an API-first approach. It is a CMS that deve ### Prerequisite -- nodejs, v8 or higher -- You should have the data synced through [Contentstack DataSync](https://www.contentstack.com/docs/guide/synchronization/contentstack-datasync) +- Nodejs, v8 or higher +- You should have the data synced through [Contentstack DataSync](https://www.contentstack.com/docs/guide/synchronization/contentstack-datasync) ### Configuration diff --git a/dist/config.js b/dist/config.js index cf701982..ee145b58 100644 --- a/dist/config.js +++ b/dist/config.js @@ -15,6 +15,7 @@ exports.defaultConfig = { types: { assets: '_assets', content_types: '_content_types', + references: '_references', }, }, locale: 'en-us', diff --git a/dist/stack.js b/dist/stack.js index d8f3daee..62d62b59 100644 --- a/dist/stack.js +++ b/dist/stack.js @@ -487,6 +487,20 @@ class Stack { stack.q.content_type_uid = stack.types.content_types; return stack; } + /** + * @public + * @method contentTypes + * @summary Get content type schemas + * @example + * Stack.contentTypes().find() + * + * @returns {this} - Returns `stack's` instance + */ + contentTypes() { + const stack = new Stack(this.config); + stack.q.content_type_uid = stack.types.content_types; + return stack; + } /** * @public * @method schema @@ -711,8 +725,11 @@ class Stack { * Excludes all references of the entries being scanned * * @example - * Stack.contentType('example').entries().excludeReferences().find() - * .then((result) => { + * Stack.contentType('example') + * .entries() + * .excludeReferences() + * .find() + * .then((result) => { * // ‘result’ entries without references * }).catch((error) => { * // error trace @@ -729,12 +746,27 @@ class Stack { * @method includeContentType * @description Includes the total number of entries returned in the response. * @example - * const query = Stack.contentType('example').entries().includeContentType().find() - * query.then((result) => { - * // ‘result’ contains a list of entries along contentType - * }).catch((error) => { - * // error trace - * }) + * Stack.contentType('example') + * .entries() + * .includeContentType() + * .find() + * .then((result) => { + * // Expected result + * { + * entries: [ + * { + * ..., + * }, + * ], + * content_type_uid: 'example', + * locale: 'en-us', + * content_type: { + * ..., // Content type example's schema + * } + * } + * }).catch((error) => { + * // error trace + * }) * * @returns {this} - Returns `stack's` instance */ @@ -747,7 +779,7 @@ class Stack { * @method getQuery * @description Returns the raw (JSON) query based on the filters applied on Query object. * @example - * Stack.contentType('content_type_uid') + * Stack.contentType('example') * .eqaulTo('title','Demo') * .getQuery() * .find() @@ -859,11 +891,12 @@ class Stack { /** * @public * @method referenceDepth + * @deprecated * @summary * Use it along with .includeReferences() * Overrides the default reference depths defined for references - 2 - * i.e. If A -> B -> C -> D -> E, so calling .includeReferences() on content type A, - * would result in all references being resolved until its nested child reference E + * i.e. If A -> B -> C -> D, so calling .includeReferences() on content type A, + * would result in all references being resolved until its nested child reference D * @param {number} depth - Level of nested references to be fetched * @example * Stack.contentType('blog') @@ -940,6 +973,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 @@ -956,6 +990,25 @@ class Stack { this.q.isSingle = true; return this.find(); } + /** + * @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() { + this.q.isSingle = true; + return this.find(); + } /** * @private * @method preProcess @@ -1189,9 +1242,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); } }); @@ -1369,11 +1422,10 @@ class Stack { } // should not enter this section // if the schema doesn't exist, error should have occurred before - if (typeof schema === 'undefined') { + if (typeof schema === 'undefined' || typeof 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: [], @@ -1503,23 +1555,23 @@ class Stack { let assetFieldPaths; let entryReferencePaths; if (filteredContents[i].hasOwnProperty(this.types.assets)) { - assetFieldPaths = Object.keys(filteredContents[i]._assets); + assetFieldPaths = Object.keys(filteredContents[i][this.types.assets]); paths = paths.concat(assetFieldPaths); } if (filteredContents[i].hasOwnProperty('_references')) { - entryReferencePaths = Object.keys(filteredContents[i]._references); + entryReferencePaths = Object.keys(filteredContents[i][this.types.references]); paths = paths.concat(entryReferencePaths); for (let k = 0, l = entryReferencePaths.length; k < l; k++) { - if (typeof filteredContents[i]._references[entryReferencePaths[k]] === 'string') { + if (typeof filteredContents[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? // locale, - uid: filteredContents[i]._references[entryReferencePaths[k]], + uid: filteredContents[i][this.types.references][entryReferencePaths[k]], }); } - else if (filteredContents[i]._references[entryReferencePaths[k]].length) { - filteredContents[i]._references[entryReferencePaths[k]].forEach((uid) => { + else if (filteredContents[i][this.types.references][entryReferencePaths[k]].length) { + filteredContents[i][this.types.references][entryReferencePaths[k]].forEach((uid) => { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // Question: Adding extra key in query, slows querying down? Probably yes. diff --git a/docs/Contentstack.html b/docs/Contentstack.html index 41e180ba..5b912ff4 100644 --- a/docs/Contentstack.html +++ b/docs/Contentstack.html @@ -271,13 +271,13 @@
Example

diff --git a/docs/Stack.html b/docs/Stack.html index 23d98d24..2e67d6a6 100644 --- a/docs/Stack.html +++ b/docs/Stack.html @@ -175,13 +175,13 @@
Returns:

diff --git a/docs/global.html b/docs/global.html index cfb986a7..e8b8f6ee 100644 --- a/docs/global.html +++ b/docs/global.html @@ -1272,6 +1272,119 @@
Example
+

contentTypes() → {this}

+ + + +

Get content type schemas

+ + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ - Returns `stack's` instance +
+ + + +
+
+ Type +
+
+ +this + + +
+
+ + + + + + +
Example
+ +
Stack.contentTypes().find()
+ + + + + + + + +

count() → {this}

@@ -1324,7 +1437,7 @@

countSource:
@@ -1966,7 +2079,7 @@
Parameters:
Source:
@@ -2138,7 +2251,7 @@
Parameters:
Source:
@@ -2256,7 +2369,7 @@

excl
Source:
@@ -2309,8 +2422,11 @@

Returns:
Example
-
Stack.contentType('example').entries().excludeReferences().find()
-.then((result) => {
+    
Stack.contentType('example')
+ .entries()
+ .excludeReferences()
+ .find()
+ .then((result) => {
    // ‘result’ entries without references
  }).catch((error) => {
    // error trace
@@ -2597,7 +2713,7 @@ 
Parameters:
Source:
@@ -2768,7 +2884,7 @@
Parameters:
Source:
@@ -3287,7 +3403,7 @@

getQuerySource:
@@ -3340,7 +3456,7 @@
Returns:
Example
-
Stack.contentType('content_type_uid')
+    
Stack.contentType('example')
  .eqaulTo('title','Demo')
  .getQuery()
  .find()
@@ -3840,7 +3956,7 @@
Parameters:
Source:
@@ -3958,7 +4074,7 @@

inc
Source:
@@ -4011,12 +4127,27 @@

Returns:
Example
-
const query = Stack.contentType('example').entries().includeContentType().find()
-query.then((result) => {
-  // ‘result’ contains a list of entries along contentType
-}).catch((error) => {
-  // error trace
-})
+
Stack.contentType('example')
+ .entries()
+ .includeContentType()
+ .find()
+ .then((result) => {
+   // Expected result
+   {
+     entries: [
+       {
+         ...,
+       },
+     ],
+     content_type_uid: 'example',
+     locale: 'en-us',
+     content_type: {
+       ..., // Content type example's schema
+     }
+   }
+ }).catch((error) => {
+   // error trace
+ })
@@ -4078,7 +4209,7 @@

includeCo
Source:
@@ -4243,7 +4374,7 @@

Parameters:
Source:
@@ -4410,7 +4541,7 @@
Parameters:
Source:
@@ -5695,7 +5826,7 @@
Parameters:
Source:
@@ -6030,7 +6161,7 @@
Parameters:
Source:
@@ -6198,7 +6329,7 @@
Parameters:
Source:
@@ -6271,8 +6402,8 @@

referen

Use it along with .includeReferences() Overrides the default reference depths defined for references - 2 -i.e. If A -> B -> C -> D -> E, so calling .includeReferences() on content type A, -would result in all references being resolved until its nested child reference E

+i.e. If A -> B -> C -> D, so calling .includeReferences() on content type A, +would result in all references being resolved until its nested child reference D

@@ -6355,6 +6486,8 @@

Parameters:
+
Deprecated:
  • Yes
+ @@ -6367,7 +6500,7 @@
Parameters:
Source:
@@ -6609,7 +6742,7 @@
Parameters:
Source:
@@ -6774,7 +6907,7 @@
Parameters:
Source:
@@ -7221,7 +7354,7 @@
Parameters:
Source:
@@ -7405,7 +7538,7 @@
Parameters:
Source:
@@ -7484,13 +7617,13 @@
Example

- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:36:15 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.2 on Sat Aug 03 2019 21:10:17 GMT+0530 (India Standard Time)
diff --git a/docs/index.html b/docs/index.html index 44f9bdac..e6467d1b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -221,13 +221,13 @@

License


- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:36:15 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.2 on Sat Aug 03 2019 21:10:17 GMT+0530 (India Standard Time)
diff --git a/docs/index.js.html b/docs/index.js.html index 33544841..76a4440e 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -75,13 +75,13 @@

Source: index.js


- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:36:15 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.2 on Sat Aug 03 2019 21:10:17 GMT+0530 (India Standard Time)
diff --git a/docs/stack.js.html b/docs/stack.js.html index dc2035a1..1fae032e 100644 --- a/docs/stack.js.html +++ b/docs/stack.js.html @@ -515,6 +515,20 @@

Source: stack.js

stack.q.content_type_uid = stack.types.content_types; return stack; } + /** + * @public + * @method contentTypes + * @summary Get content type schemas + * @example + * Stack.contentTypes().find() + * + * @returns {this} - Returns `stack's` instance + */ + contentTypes() { + const stack = new Stack(this.config); + stack.q.content_type_uid = stack.types.content_types; + return stack; + } /** * @public * @method schema @@ -739,8 +753,11 @@

Source: stack.js

* Excludes all references of the entries being scanned * * @example - * Stack.contentType('example').entries().excludeReferences().find() - * .then((result) => { + * Stack.contentType('example') + * .entries() + * .excludeReferences() + * .find() + * .then((result) => { * // ‘result’ entries without references * }).catch((error) => { * // error trace @@ -757,12 +774,27 @@

Source: stack.js

* @method includeContentType * @description Includes the total number of entries returned in the response. * @example - * const query = Stack.contentType('example').entries().includeContentType().find() - * query.then((result) => { - * // ‘result’ contains a list of entries along contentType - * }).catch((error) => { - * // error trace - * }) + * Stack.contentType('example') + * .entries() + * .includeContentType() + * .find() + * .then((result) => { + * // Expected result + * { + * entries: [ + * { + * ..., + * }, + * ], + * content_type_uid: 'example', + * locale: 'en-us', + * content_type: { + * ..., // Content type example's schema + * } + * } + * }).catch((error) => { + * // error trace + * }) * * @returns {this} - Returns `stack's` instance */ @@ -775,7 +807,7 @@

Source: stack.js

* @method getQuery * @description Returns the raw (JSON) query based on the filters applied on Query object. * @example - * Stack.contentType('content_type_uid') + * Stack.contentType('example') * .eqaulTo('title','Demo') * .getQuery() * .find() @@ -801,28 +833,10 @@

Source: stack.js

*/ regex(key, value, options = 'g') { if (key && value && typeof key === 'string' && typeof value === 'string') { - if (this.q.query.hasOwnProperty(key)) { - if (this.q.query.hasOwnProperty('$or')) { - this.q.query.$or.push({ - [key]: { - $options: options, - $regex: value, - }, - }); - } - else { - this.q.query.$or = [{ - $options: options, - $regex: value, - }]; - } - } - else { - this.q.query[key] = { - $options: options, - $regex: value, - }; - } + this.q.query[key] = { + $options: options, + $regex: value, + }; return this; } throw new Error('Kindly provide valid parameters for .regex()!'); @@ -905,11 +919,12 @@

Source: stack.js

/** * @public * @method referenceDepth + * @deprecated * @summary * Use it along with .includeReferences() * Overrides the default reference depths defined for references - 2 - * i.e. If A -> B -> C -> D -> E, so calling .includeReferences() on content type A, - * would result in all references being resolved until its nested child reference E + * i.e. If A -> B -> C -> D, so calling .includeReferences() on content type A, + * would result in all references being resolved until its nested child reference D * @param {number} depth - Level of nested references to be fetched * @example * Stack.contentType('blog') @@ -1015,11 +1030,11 @@

Source: stack.js

let key; let filePath; switch (this.q.content_type_uid) { - case '_assets': + case this.types.assets: filePath = utils_1.getAssetsPath(locale) + '.json'; key = (this.q.isSingle) ? 'asset' : 'assets'; break; - case '_content_types': + case this.types.content_types: filePath = utils_1.getContentTypesPath(locale) + '.json'; key = (this.q.isSingle) ? 'content_type' : 'content_types'; break; @@ -1038,7 +1053,8 @@

Source: stack.js

const keys = Object.keys(this.contentStore.projections); this.q.except = keys; } - this.q.referenceDepth = this.q.referenceDepth || this.contentStore.referenceDepth; + this.q.referenceDepth = (typeof this.q.referenceDepth === 'number') ? this.q.referenceDepth : this.contentStore + .referenceDepth; return { filePath, key, @@ -1172,12 +1188,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.docs.length; k < l; k++) { if (result.docs[k].uid === element.uid) { element.path[element.position] = result.docs[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; @@ -1223,9 +1250,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); } }); @@ -1233,12 +1260,12 @@

Source: stack.js

const includePath = currentInclude[i]; // tslint:disable-next-line: forin for (const path in entryReferences) { - const idx = includePath.indexOf(path); - 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 { @@ -1246,14 +1273,14 @@

Source: stack.js

} if (typeof entryReferences[path] === 'string') { schemasReferred.push({ - _content_type_uid: '_content_types', + _content_type_uid: this.types.content_types, uid: entryReferences[path], }); } - else { + else if (entryReferences[path].length) { entryReferences[path].forEach((contentTypeUid) => { schemasReferred.push({ - _content_type_uid: '_content_types', + _content_type_uid: this.types.content_types, uid: contentTypeUid, }); }); @@ -1282,7 +1309,7 @@

Source: stack.js

data.forEach((elem, idx) => { if (typeof elem === 'string') { queryBucket.$or.push({ - _content_type_uid: '_assets', + _content_type_uid: this.types.assets, _version: { $exists: true }, locale, uid: elem, @@ -1324,7 +1351,7 @@

Source: stack.js

} else if (typeof data === 'string') { queryBucket.$or.push({ - _content_type_uid: '_assets', + _content_type_uid: this.types.assets, _version: { $exists: true }, locale, uid: data, @@ -1403,11 +1430,10 @@

Source: stack.js

} // should not enter this section // if the schema doesn't exist, error should have occurred before - if (typeof schema === 'undefined') { + if (typeof schema === 'undefined' || typeof 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: [], @@ -1436,7 +1462,7 @@

Source: stack.js

return __awaiter(this, void 0, void 0, function* () { const ctQuery = { $or: [{ - _content_type_uid: '_content_types', + _content_type_uid: this.types.content_types, uid: contentTypeUid, }], }; @@ -1482,12 +1508,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.docs.length; k < l; k++) { if (result.docs[k].uid === element.uid) { element.path[element.position] = result.docs[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; @@ -1526,23 +1563,23 @@

Source: stack.js

let assetFieldPaths; let entryReferencePaths; if (filteredContents[i].hasOwnProperty(this.types.assets)) { - assetFieldPaths = Object.keys(filteredContents[i]._assets); + assetFieldPaths = Object.keys(filteredContents[i][this.types.assets]); paths = paths.concat(assetFieldPaths); } if (filteredContents[i].hasOwnProperty('_references')) { - entryReferencePaths = Object.keys(filteredContents[i]._references); + entryReferencePaths = Object.keys(filteredContents[i][this.types.references]); paths = paths.concat(entryReferencePaths); for (let k = 0, l = entryReferencePaths.length; k < l; k++) { - if (typeof filteredContents[i]._references[entryReferencePaths[k]] === 'string') { + if (typeof filteredContents[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? // locale, - uid: filteredContents[i]._references[entryReferencePaths[k]], + uid: filteredContents[i][this.types.references][entryReferencePaths[k]], }); } - else if (filteredContents[i]._references[entryReferencePaths[k]].length) { - filteredContents[i]._references[entryReferencePaths[k]].forEach((uid) => { + else if (filteredContents[i][this.types.references][entryReferencePaths[k]].length) { + filteredContents[i][this.types.references][entryReferencePaths[k]].forEach((uid) => { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // Question: Adding extra key in query, slows querying down? Probably yes. @@ -1572,13 +1609,13 @@

Source: stack.js


- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:36:15 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.2 on Sat Aug 03 2019 21:10:17 GMT+0530 (India Standard Time)
diff --git a/docs/utils.js.html b/docs/utils.js.html index acea4eaf..e424c483 100644 --- a/docs/utils.js.html +++ b/docs/utils.js.html @@ -211,13 +211,13 @@

Source: utils.js


- Documentation generated by JSDoc 3.6.3 on Sun Jul 21 2019 23:36:15 GMT+0530 (India Standard Time) + Documentation generated by JSDoc 3.6.2 on Sat Aug 03 2019 21:10:17 GMT+0530 (India Standard Time)
diff --git a/package.json b/package.json index 8c54a037..872d62c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasync-filesystem-sdk", - "version": "1.0.0", + "version": "1.0.1", "description": "JavaScript filesystem SDK to query data synced via @contentstack/datasync-content-store-filesystem", "main": "dist/index.js", "scripts": { diff --git a/src/config.ts b/src/config.ts index 4dadf403..71468be9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,6 +14,7 @@ export const defaultConfig = { types: { assets: '_assets', content_types: '_content_types', + references: '_references', }, }, locale: 'en-us', diff --git a/src/stack.ts b/src/stack.ts index a1f28499..f918c569 100755 --- a/src/stack.ts +++ b/src/stack.ts @@ -574,6 +574,22 @@ export class Stack { return stack } + /** + * @public + * @method contentTypes + * @summary Get content type schemas + * @example + * Stack.contentTypes().find() + * + * @returns {this} - Returns `stack's` instance + */ + public contentTypes() { + const stack = new Stack(this.config) + stack.q.content_type_uid = stack.types.content_types + + return stack + } + /** * @public * @method schema @@ -817,8 +833,11 @@ export class Stack { * Excludes all references of the entries being scanned * * @example - * Stack.contentType('example').entries().excludeReferences().find() - * .then((result) => { + * Stack.contentType('example') + * .entries() + * .excludeReferences() + * .find() + * .then((result) => { * // ‘result’ entries without references * }).catch((error) => { * // error trace @@ -837,12 +856,27 @@ export class Stack { * @method includeContentType * @description Includes the total number of entries returned in the response. * @example - * const query = Stack.contentType('example').entries().includeContentType().find() - * query.then((result) => { - * // ‘result’ contains a list of entries along contentType - * }).catch((error) => { - * // error trace - * }) + * Stack.contentType('example') + * .entries() + * .includeContentType() + * .find() + * .then((result) => { + * // Expected result + * { + * entries: [ + * { + * ..., + * }, + * ], + * content_type_uid: 'example', + * locale: 'en-us', + * content_type: { + * ..., // Content type example's schema + * } + * } + * }).catch((error) => { + * // error trace + * }) * * @returns {this} - Returns `stack's` instance */ @@ -858,7 +892,7 @@ export class Stack { * @method getQuery * @description Returns the raw (JSON) query based on the filters applied on Query object. * @example - * Stack.contentType('content_type_uid') + * Stack.contentType('example') * .eqaulTo('title','Demo') * .getQuery() * .find() @@ -982,11 +1016,12 @@ export class Stack { /** * @public * @method referenceDepth + * @deprecated * @summary * Use it along with .includeReferences() * Overrides the default reference depths defined for references - 2 - * i.e. If A -> B -> C -> D -> E, so calling .includeReferences() on content type A, - * would result in all references being resolved until its nested child reference E + * i.e. If A -> B -> C -> D, so calling .includeReferences() on content type A, + * would result in all references being resolved until its nested child reference D * @param {number} depth - Level of nested references to be fetched * @example * Stack.contentType('blog') @@ -1072,6 +1107,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 @@ -1090,6 +1126,27 @@ export class Stack { return this.find() } + /** + * @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() { + this.q.isSingle = true + + return this.find() + } + /** * @private * @method preProcess @@ -1365,9 +1422,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) } }) @@ -1554,12 +1611,11 @@ export class Stack { // should not enter this section // if the schema doesn't exist, error should have occurred before - if (typeof schema === 'undefined') { + if (typeof schema === 'undefined' || typeof 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: any = { $or: [], @@ -1712,23 +1768,23 @@ export class Stack { let assetFieldPaths: string[] let entryReferencePaths: string[] if (filteredContents[i].hasOwnProperty(this.types.assets)) { - assetFieldPaths = Object.keys(filteredContents[i]._assets) + assetFieldPaths = Object.keys(filteredContents[i][this.types.assets]) paths = paths.concat(assetFieldPaths) } if (filteredContents[i].hasOwnProperty('_references')) { - entryReferencePaths = Object.keys(filteredContents[i]._references) + entryReferencePaths = Object.keys(filteredContents[i][this.types.references]) paths = paths.concat(entryReferencePaths) for (let k = 0, l = entryReferencePaths.length; k < l; k++) { - if (typeof filteredContents[i]._references[entryReferencePaths[k]] === 'string') { + if (typeof filteredContents[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? // locale, - uid: filteredContents[i]._references[entryReferencePaths[k]], + uid: filteredContents[i][this.types.references][entryReferencePaths[k]], }) - } else if (filteredContents[i]._references[entryReferencePaths[k]].length) { - filteredContents[i]._references[entryReferencePaths[k]].forEach((uid) => { + } else if (filteredContents[i][this.types.references][entryReferencePaths[k]].length) { + filteredContents[i][this.types.references][entryReferencePaths[k]].forEach((uid) => { ctQueries.$or.push({ _content_type_uid: this.types.content_types, // Question: Adding extra key in query, slows querying down? Probably yes. diff --git a/typings/config.d.ts b/typings/config.d.ts index e28384d6..310258e6 100644 --- a/typings/config.d.ts +++ b/typings/config.d.ts @@ -13,6 +13,7 @@ export declare const defaultConfig: { types: { assets: string; content_types: string; + references: string; }; }; locale: string; diff --git a/typings/stack.d.ts b/typings/stack.d.ts index b67bfa9b..ec94f4a5 100644 --- a/typings/stack.d.ts +++ b/typings/stack.d.ts @@ -129,6 +129,16 @@ export declare class Stack { * @returns {this} - Returns `stack's` instance */ schemas(): Stack; + /** + * @public + * @method contentTypes + * @summary Get content type schemas + * @example + * Stack.contentTypes().find() + * + * @returns {this} - Returns `stack's` instance + */ + contentTypes(): Stack; /** * @public * @method schema @@ -289,8 +299,11 @@ export declare class Stack { * Excludes all references of the entries being scanned * * @example - * Stack.contentType('example').entries().excludeReferences().find() - * .then((result) => { + * Stack.contentType('example') + * .entries() + * .excludeReferences() + * .find() + * .then((result) => { * // ‘result’ entries without references * }).catch((error) => { * // error trace @@ -304,12 +317,27 @@ export declare class Stack { * @method includeContentType * @description Includes the total number of entries returned in the response. * @example - * const query = Stack.contentType('example').entries().includeContentType().find() - * query.then((result) => { - * // ‘result’ contains a list of entries along contentType - * }).catch((error) => { - * // error trace - * }) + * Stack.contentType('example') + * .entries() + * .includeContentType() + * .find() + * .then((result) => { + * // Expected result + * { + * entries: [ + * { + * ..., + * }, + * ], + * content_type_uid: 'example', + * locale: 'en-us', + * content_type: { + * ..., // Content type example's schema + * } + * } + * }).catch((error) => { + * // error trace + * }) * * @returns {this} - Returns `stack's` instance */ @@ -319,7 +347,7 @@ export declare class Stack { * @method getQuery * @description Returns the raw (JSON) query based on the filters applied on Query object. * @example - * Stack.contentType('content_type_uid') + * Stack.contentType('example') * .eqaulTo('title','Demo') * .getQuery() * .find() @@ -400,11 +428,12 @@ export declare class Stack { /** * @public * @method referenceDepth + * @deprecated * @summary * Use it along with .includeReferences() * Overrides the default reference depths defined for references - 2 - * i.e. If A -> B -> C -> D -> E, so calling .includeReferences() on content type A, - * would result in all references being resolved until its nested child reference E + * i.e. If A -> B -> C -> D, so calling .includeReferences() on content type A, + * would result in all references being resolved until its nested child reference D * @param {number} depth - Level of nested references to be fetched * @example * Stack.contentType('blog') @@ -439,6 +468,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 @@ -452,6 +482,22 @@ export declare class Stack { * @returns {object} - Returns an object, that has been processed, filtered and referenced */ findOne(): 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(): Promise; /** * @private * @method preProcess