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 0f02f25 commit f6b912a
Show file tree
Hide file tree
Showing 13 changed files with 628 additions and 173 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
<!-- Since by default, the contents are sorted in descending order on 'updated_at' key -->
- `updated_at: -1`

### Configuration

|Property|Type|Default value|Description|
Expand Down
1 change: 1 addition & 0 deletions dist/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports.config = {
types: {
assets: '_assets',
content_types: '_content_types',
references: '_references',
},
},
limit: 100,
Expand Down
97 changes: 63 additions & 34 deletions dist/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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: [],
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit f6b912a

Please sign in to comment.