Skip to content

Commit

Permalink
include asset bug fix, doc update, .fetch() added, README updated
Browse files Browse the repository at this point in the history
  • Loading branch information
iyerrama29 committed Aug 3, 2019
1 parent e16480d commit 43a77a8
Show file tree
Hide file tree
Showing 15 changed files with 486 additions and 159 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions dist/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports.defaultConfig = {
types: {
assets: '_assets',
content_types: '_content_types',
references: '_references',
},
},
locale: 'en-us',
Expand Down
96 changes: 74 additions & 22 deletions dist/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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()
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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: [],
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/Contentstack.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ <h5>Example</h5>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Contentstack.html">Contentstack</a></li><li><a href="Stack.html">Stack</a></li></ul><h3>Global</h3><ul><li><a href="global.html#and">and</a></li><li><a href="global.html#ascending">ascending</a></li><li><a href="global.html#asset">asset</a></li><li><a href="global.html#assets">assets</a></li><li><a href="global.html#connect">connect</a></li><li><a href="global.html#containedIn">containedIn</a></li><li><a href="global.html#contentType">contentType</a></li><li><a href="global.html#count">count</a></li><li><a href="global.html#descending">descending</a></li><li><a href="global.html#entries">entries</a></li><li><a href="global.html#entry">entry</a></li><li><a href="global.html#equalTo">equalTo</a></li><li><a href="global.html#except">except</a></li><li><a href="global.html#excludeReferences">excludeReferences</a></li><li><a href="global.html#exists">exists</a></li><li><a href="global.html#find">find</a></li><li><a href="global.html#findOne">findOne</a></li><li><a href="global.html#getAssetsPath">getAssetsPath</a></li><li><a href="global.html#getContentTypesPath">getContentTypesPath</a></li><li><a href="global.html#getEntriesPath">getEntriesPath</a></li><li><a href="global.html#getQuery">getQuery</a></li><li><a href="global.html#greaterThan">greaterThan</a></li><li><a href="global.html#greaterThanOrEqualTo">greaterThanOrEqualTo</a></li><li><a href="global.html#include">include</a></li><li><a href="global.html#includeContentType">includeContentType</a></li><li><a href="global.html#includeCount">includeCount</a></li><li><a href="global.html#includeReferences">includeReferences</a></li><li><a href="global.html#language">language</a></li><li><a href="global.html#lessThan">lessThan</a></li><li><a href="global.html#lessThanOrEqualTo">lessThanOrEqualTo</a></li><li><a href="global.html#limit">limit</a></li><li><a href="global.html#notContainedIn">notContainedIn</a></li><li><a href="global.html#notEqualTo">notEqualTo</a></li><li><a href="global.html#notExists">notExists</a></li><li><a href="global.html#only">only</a></li><li><a href="global.html#or">or</a></li><li><a href="global.html#query">query</a></li><li><a href="global.html#queryReferences">queryReferences</a></li><li><a href="global.html#referenceDepth">referenceDepth</a></li><li><a href="global.html#regex">regex</a></li><li><a href="global.html#schema">schema</a></li><li><a href="global.html#schemas">schemas</a></li><li><a href="global.html#skip">skip</a></li><li><a href="global.html#tags">tags</a></li><li><a href="global.html#where">where</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Contentstack.html">Contentstack</a></li><li><a href="Stack.html">Stack</a></li></ul><h3>Global</h3><ul><li><a href="global.html#and">and</a></li><li><a href="global.html#ascending">ascending</a></li><li><a href="global.html#asset">asset</a></li><li><a href="global.html#assets">assets</a></li><li><a href="global.html#connect">connect</a></li><li><a href="global.html#containedIn">containedIn</a></li><li><a href="global.html#contentType">contentType</a></li><li><a href="global.html#contentTypes">contentTypes</a></li><li><a href="global.html#count">count</a></li><li><a href="global.html#descending">descending</a></li><li><a href="global.html#entries">entries</a></li><li><a href="global.html#entry">entry</a></li><li><a href="global.html#equalTo">equalTo</a></li><li><a href="global.html#except">except</a></li><li><a href="global.html#excludeReferences">excludeReferences</a></li><li><a href="global.html#exists">exists</a></li><li><a href="global.html#find">find</a></li><li><a href="global.html#findOne">findOne</a></li><li><a href="global.html#getAssetsPath">getAssetsPath</a></li><li><a href="global.html#getContentTypesPath">getContentTypesPath</a></li><li><a href="global.html#getEntriesPath">getEntriesPath</a></li><li><a href="global.html#getQuery">getQuery</a></li><li><a href="global.html#greaterThan">greaterThan</a></li><li><a href="global.html#greaterThanOrEqualTo">greaterThanOrEqualTo</a></li><li><a href="global.html#include">include</a></li><li><a href="global.html#includeContentType">includeContentType</a></li><li><a href="global.html#includeCount">includeCount</a></li><li><a href="global.html#includeReferences">includeReferences</a></li><li><a href="global.html#language">language</a></li><li><a href="global.html#lessThan">lessThan</a></li><li><a href="global.html#lessThanOrEqualTo">lessThanOrEqualTo</a></li><li><a href="global.html#limit">limit</a></li><li><a href="global.html#notContainedIn">notContainedIn</a></li><li><a href="global.html#notEqualTo">notEqualTo</a></li><li><a href="global.html#notExists">notExists</a></li><li><a href="global.html#only">only</a></li><li><a href="global.html#or">or</a></li><li><a href="global.html#query">query</a></li><li><a href="global.html#queryReferences">queryReferences</a></li><li><a href="global.html#referenceDepth">referenceDepth</a></li><li><a href="global.html#regex">regex</a></li><li><a href="global.html#schema">schema</a></li><li><a href="global.html#schemas">schemas</a></li><li><a href="global.html#skip">skip</a></li><li><a href="global.html#tags">tags</a></li><li><a href="global.html#where">where</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Sun Jul 21 2019 23:36:15 GMT+0530 (India Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Sat Aug 03 2019 21:10:17 GMT+0530 (India Standard Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
4 changes: 2 additions & 2 deletions docs/Stack.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ <h5>Returns:</h5>
</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Contentstack.html">Contentstack</a></li><li><a href="Stack.html">Stack</a></li></ul><h3>Global</h3><ul><li><a href="global.html#and">and</a></li><li><a href="global.html#ascending">ascending</a></li><li><a href="global.html#asset">asset</a></li><li><a href="global.html#assets">assets</a></li><li><a href="global.html#connect">connect</a></li><li><a href="global.html#containedIn">containedIn</a></li><li><a href="global.html#contentType">contentType</a></li><li><a href="global.html#count">count</a></li><li><a href="global.html#descending">descending</a></li><li><a href="global.html#entries">entries</a></li><li><a href="global.html#entry">entry</a></li><li><a href="global.html#equalTo">equalTo</a></li><li><a href="global.html#except">except</a></li><li><a href="global.html#excludeReferences">excludeReferences</a></li><li><a href="global.html#exists">exists</a></li><li><a href="global.html#find">find</a></li><li><a href="global.html#findOne">findOne</a></li><li><a href="global.html#getAssetsPath">getAssetsPath</a></li><li><a href="global.html#getContentTypesPath">getContentTypesPath</a></li><li><a href="global.html#getEntriesPath">getEntriesPath</a></li><li><a href="global.html#getQuery">getQuery</a></li><li><a href="global.html#greaterThan">greaterThan</a></li><li><a href="global.html#greaterThanOrEqualTo">greaterThanOrEqualTo</a></li><li><a href="global.html#include">include</a></li><li><a href="global.html#includeContentType">includeContentType</a></li><li><a href="global.html#includeCount">includeCount</a></li><li><a href="global.html#includeReferences">includeReferences</a></li><li><a href="global.html#language">language</a></li><li><a href="global.html#lessThan">lessThan</a></li><li><a href="global.html#lessThanOrEqualTo">lessThanOrEqualTo</a></li><li><a href="global.html#limit">limit</a></li><li><a href="global.html#notContainedIn">notContainedIn</a></li><li><a href="global.html#notEqualTo">notEqualTo</a></li><li><a href="global.html#notExists">notExists</a></li><li><a href="global.html#only">only</a></li><li><a href="global.html#or">or</a></li><li><a href="global.html#query">query</a></li><li><a href="global.html#queryReferences">queryReferences</a></li><li><a href="global.html#referenceDepth">referenceDepth</a></li><li><a href="global.html#regex">regex</a></li><li><a href="global.html#schema">schema</a></li><li><a href="global.html#schemas">schemas</a></li><li><a href="global.html#skip">skip</a></li><li><a href="global.html#tags">tags</a></li><li><a href="global.html#where">where</a></li></ul>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Contentstack.html">Contentstack</a></li><li><a href="Stack.html">Stack</a></li></ul><h3>Global</h3><ul><li><a href="global.html#and">and</a></li><li><a href="global.html#ascending">ascending</a></li><li><a href="global.html#asset">asset</a></li><li><a href="global.html#assets">assets</a></li><li><a href="global.html#connect">connect</a></li><li><a href="global.html#containedIn">containedIn</a></li><li><a href="global.html#contentType">contentType</a></li><li><a href="global.html#contentTypes">contentTypes</a></li><li><a href="global.html#count">count</a></li><li><a href="global.html#descending">descending</a></li><li><a href="global.html#entries">entries</a></li><li><a href="global.html#entry">entry</a></li><li><a href="global.html#equalTo">equalTo</a></li><li><a href="global.html#except">except</a></li><li><a href="global.html#excludeReferences">excludeReferences</a></li><li><a href="global.html#exists">exists</a></li><li><a href="global.html#find">find</a></li><li><a href="global.html#findOne">findOne</a></li><li><a href="global.html#getAssetsPath">getAssetsPath</a></li><li><a href="global.html#getContentTypesPath">getContentTypesPath</a></li><li><a href="global.html#getEntriesPath">getEntriesPath</a></li><li><a href="global.html#getQuery">getQuery</a></li><li><a href="global.html#greaterThan">greaterThan</a></li><li><a href="global.html#greaterThanOrEqualTo">greaterThanOrEqualTo</a></li><li><a href="global.html#include">include</a></li><li><a href="global.html#includeContentType">includeContentType</a></li><li><a href="global.html#includeCount">includeCount</a></li><li><a href="global.html#includeReferences">includeReferences</a></li><li><a href="global.html#language">language</a></li><li><a href="global.html#lessThan">lessThan</a></li><li><a href="global.html#lessThanOrEqualTo">lessThanOrEqualTo</a></li><li><a href="global.html#limit">limit</a></li><li><a href="global.html#notContainedIn">notContainedIn</a></li><li><a href="global.html#notEqualTo">notEqualTo</a></li><li><a href="global.html#notExists">notExists</a></li><li><a href="global.html#only">only</a></li><li><a href="global.html#or">or</a></li><li><a href="global.html#query">query</a></li><li><a href="global.html#queryReferences">queryReferences</a></li><li><a href="global.html#referenceDepth">referenceDepth</a></li><li><a href="global.html#regex">regex</a></li><li><a href="global.html#schema">schema</a></li><li><a href="global.html#schemas">schemas</a></li><li><a href="global.html#skip">skip</a></li><li><a href="global.html#tags">tags</a></li><li><a href="global.html#where">where</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Sun Jul 21 2019 23:36:15 GMT+0530 (India Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Sat Aug 03 2019 21:10:17 GMT+0530 (India Standard Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
Loading

0 comments on commit 43a77a8

Please sign in to comment.