Skip to content

Commit 060cf4a

Browse files
committed
include asset bug fix, doc update, .fetch() added, README updated
1 parent cd99c6a commit 060cf4a

File tree

13 files changed

+628
-173
lines changed

13 files changed

+628
-173
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ Contentstack is a headless CMS with an API-first approach. It is a CMS that deve
88

99
### Prerequisite
1010

11-
- nodejs, v8+
11+
- Nodejs, v8+
1212
- MongoDB, v3.6 or higher
1313
- You should have the data synced through [Contentstack DataSync](https://www.contentstack.com/docs/guide/synchronization/contentstack-datasync)
1414

15+
**Note**
16+
For optimal performance, we recommend that you add indexes on the following keys of your collections
17+
- `_content_type_uid: 1`
18+
- `uid: 1`
19+
- `locale: 1`
20+
<!-- Since by default, the contents are sorted in descending order on 'updated_at' key -->
21+
- `updated_at: -1`
22+
1523
### Configuration
1624

1725
|Property|Type|Default value|Description|

dist/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports.config = {
2323
types: {
2424
assets: '_assets',
2525
content_types: '_content_types',
26+
references: '_references',
2627
},
2728
},
2829
limit: 100,

dist/stack.js

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,30 @@ class Stack {
918918
// stack.collection = stack.db.collection(stack.contentStore.collectionName)
919919
return stack;
920920
}
921+
/**
922+
* @public
923+
* @method contentTypes
924+
* @description
925+
* Query for a set of content type schemas
926+
* @example
927+
* Stack
928+
* .contentTypes()
929+
* .find()
930+
* .then((result) => {
931+
* // returns a set of content type schemas
932+
* })
933+
* .catch((error) => {
934+
* // handle query errors
935+
* })
936+
*
937+
* @returns {Stack} Returns an instance of 'stack'
938+
*/
939+
contentTypes() {
940+
const stack = new Stack(this.config, this.db);
941+
stack.q.content_type_uid = this.types.content_types;
942+
// stack.collection = stack.db.collection(stack.contentStore.collectionName)
943+
return stack;
944+
}
921945
/**
922946
* @public
923947
* @method limit
@@ -1300,7 +1324,7 @@ class Stack {
13001324
* @public
13011325
* @method excludeReferences
13021326
* @description
1303-
* Excludes all references of the entries being scanned
1327+
* Excludes all references of the entries being scanned.
13041328
* Note: On calling this, assets will not be binded in the result being returned.
13051329
*
13061330
* @example
@@ -1373,14 +1397,15 @@ class Stack {
13731397
}
13741398
/**
13751399
* @public
1376-
* @method includeAllReferences
1400+
* @method includeReferences
13771401
* @description
1378-
* This method would return all the references of your queried entries (until depth 4)
1379-
* Note: If you wish to increase the depth of the references fetched, call .referenceDepth()
1402+
* This method would return all the references of your queried entries (until depth 2)
1403+
* Note: If you wish to increase the depth of the references fetched, call pass a numeric parameter
13801404
* @example
1381-
* Stack.contentType('blog')
1405+
* Stack
1406+
* .contentType('blog')
13821407
* .entries()
1383-
* .includeAllReferences()
1408+
* .includeReferences(3)
13841409
* @returns {Stack} Returns 'this' instance (of Stack)
13851410
*/
13861411
includeReferences(depth) {
@@ -1396,7 +1421,7 @@ class Stack {
13961421
* @method include
13971422
* @description
13981423
* Pass in reference field uids, that you want included in your result.
1399-
* If you want all the references, use .includeAllReferences()
1424+
* If you want all the references, use .includeReferences()
14001425
* @example
14011426
* Stack.contentType('blog')
14021427
* .entries()
@@ -1528,6 +1553,7 @@ class Stack {
15281553
/**
15291554
* @public
15301555
* @method findOne
1556+
* @deprecated - Use .fetch() instead
15311557
* @description
15321558
* Queries the db using the query built/passed. Returns a single entry/asset/content type object
15331559
* Does all the processing, filtering, referencing after querying the DB
@@ -1545,6 +1571,26 @@ class Stack {
15451571
this.internal.single = true;
15461572
return this.find(query);
15471573
}
1574+
/**
1575+
* @public
1576+
* @method fetch
1577+
* @description
1578+
* Queries the db using the query built/passed. Returns a single entry/asset/content type object
1579+
* Does all the processing, filtering, referencing after querying the DB
1580+
* @param {object} query Optional query object, that overrides all the previously build queries
1581+
*
1582+
* @example
1583+
* Stack
1584+
* .contentType('blog')
1585+
* .entries()
1586+
* .fetch()
1587+
*
1588+
* @returns {object} - Returns an object, that has been processed, filtered and referenced
1589+
*/
1590+
fetch(query = {}) {
1591+
this.internal.single = true;
1592+
return this.find(query);
1593+
}
15481594
/**
15491595
* @private
15501596
* @method preProcess
@@ -1708,11 +1754,10 @@ class Stack {
17081754
_assets: 1,
17091755
_id: 0,
17101756
});
1711-
if (schema === null) {
1757+
if (schema === null || schema[this.types.assets] !== 'object') {
17121758
return;
17131759
}
1714-
const assetPaths = schema._assets;
1715-
const paths = Object.keys(assetPaths);
1760+
const paths = Object.keys(schema[this.types.assets]);
17161761
const shelf = [];
17171762
const queryBucket = {
17181763
$or: [],
@@ -1759,22 +1804,6 @@ class Stack {
17591804
_content_type_uid: this.types.content_types,
17601805
uid: contentTypeUid,
17611806
};
1762-
/**
1763-
* key: {
1764-
* $in_query: {
1765-
* $or: [
1766-
* {
1767-
* range: { $in: [10, 100] }
1768-
* },
1769-
* {
1770-
* key2: {
1771-
* $in_query: ...
1772-
* }
1773-
* }
1774-
* ]
1775-
* }
1776-
* }
1777-
*/
17781807
const { paths, // ref. fields in the current content types
17791808
pendingPath, // left over of *paths*
17801809
schemaList, } = yield this.getReferencePath(ctQuery, locale, include);
@@ -1943,9 +1972,9 @@ class Stack {
19431972
let entryReferences = {};
19441973
schemas.forEach((schema) => {
19451974
// Entry references
1946-
entryReferences = lodash_1.merge(entryReferences, schema._references);
1975+
entryReferences = lodash_1.merge(entryReferences, schema[this.types.references]);
19471976
// tslint:disable-next-line: forin
1948-
for (const path in schema._assets) {
1977+
for (const path in schema[this.types.assets]) {
19491978
paths.push(path);
19501979
}
19511980
});
@@ -2126,23 +2155,23 @@ class Stack {
21262155
let assetFieldPaths;
21272156
let entryReferencePaths;
21282157
if (contents[i].hasOwnProperty(this.types.assets)) {
2129-
assetFieldPaths = Object.keys(contents[i]._assets);
2158+
assetFieldPaths = Object.keys(contents[i][this.types.assets]);
21302159
paths = paths.concat(assetFieldPaths);
21312160
}
21322161
if (contents[i].hasOwnProperty('_references')) {
2133-
entryReferencePaths = Object.keys(contents[i]._references);
2162+
entryReferencePaths = Object.keys(contents[i][this.types.references]);
21342163
paths = paths.concat(entryReferencePaths);
21352164
for (let k = 0, l = entryReferencePaths.length; k < l; k++) {
2136-
if (typeof contents[i]._references[entryReferencePaths[k]] === 'string') {
2165+
if (typeof contents[i][this.types.references][entryReferencePaths[k]] === 'string') {
21372166
ctQueries.$or.push({
21382167
_content_type_uid: this.types.content_types,
21392168
// this would probably make it slow in FS, avoid this there?
21402169
// locale,
2141-
uid: contents[i]._references[entryReferencePaths[k]],
2170+
uid: contents[i][this.types.references][entryReferencePaths[k]],
21422171
});
21432172
}
2144-
else if (contents[i]._references[entryReferencePaths[k]].length) {
2145-
contents[i]._references[entryReferencePaths[k]].forEach((uid) => {
2173+
else if (contents[i][this.types.references][entryReferencePaths[k]].length) {
2174+
contents[i][this.types.references][entryReferencePaths[k]].forEach((uid) => {
21462175
ctQueries.$or.push({
21472176
_content_type_uid: this.types.content_types,
21482177
// avoiding locale here, not sure if its required

0 commit comments

Comments
 (0)