Skip to content

Commit bbf16ce

Browse files
committed
test cases updated, 80% coverage achieved
1 parent 8910991 commit bbf16ce

17 files changed

+579
-354
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[![Contentstack](https://www.contentstack.com/docs/static/images/contentstack.png)](https://www.contentstack.com/)
2-
## Contentstack sync Mongodb SDK
2+
## Contentstack Sync Mongodb SDK
33

44
Contentstack is a headless CMS with an API-first approach. It is a CMS that developers can use to build powerful cross-platform applications in their favorite languages. Build your application frontend, and Contentstack will take care of the rest. [Read More](https://www.contentstack.com/).
55

6-
(Contentstack sync utility)[] provides Mongodb SDK to query applications that have locally strored contents in mongodb. Given below is the detailed guide and helpful resources to get started with Mongodb SDK.
6+
[Contentstack sync utility](https://www.contentstack.com/docs/tools-and-frameworks) provides Mongodb SDK to query applications that have locally strored contents in mongodb. Given below is the detailed guide and helpful resources to get started with Mongodb SDK.
77

88
### Prerequisite
99

@@ -14,30 +14,33 @@ Contentstack is a headless CMS with an API-first approach. It is a CMS that deve
1414

1515
To import the SDK in your project, use the following command:
1616
```js
17-
const Contentstack = require ('contentstack-sync-mongodb-sdk)
17+
import { Contentstack } from 'contentstack-sync-mongodb-sdk'
1818
```
1919

2020
To initialize the SDK, you'd need to perform the following steps
2121

2222
1. Initialize stack instance.
2323
```js
24-
const Stack = contentstack.Stack(config)
24+
const Stack = Contentstack.Stack(config)
2525
```
26-
2. Call the connect method. The connect method connects the SDK to the database. Call this, before running SDK queries
26+
27+
2. Call the connect method. This method establishes a connection between the SDK and mongodb database.
2728
```js
2829
Stack.connect(dbConfig)
29-
.then()
30-
.catch()
30+
.then(fnResolve)
31+
.catch(fnReject)
3132
```
33+
> Important: You need to call this, before running SDK queries!
34+
3235
Once you have initialized the SDK, you can start querying on the sync-utility's DB's
3336

3437
### Querying
3538

36-
Notes
37-
- By default, 'content_type_uid' and 'locale' keys as part of the response.
38-
- If `.language()` is not provided, then the 1st language, provided in config would be considered.
39-
- If querying for a single entry/asset (using `.entry()` OR `.findOne()`), the result will be an object i.e. `{ entry: {} }`, if the entry or asset is not found, `{ entry: null }` will be returned.
40-
- Querying multiple entries, would return `{ entries: [ {...} ] }`.
39+
- Notes
40+
- By default, 'content_type_uid' and 'locale' keys as part of the response.
41+
- If `.language()` is not provided, then the 1st language, provided in `config.locales` would be considered.
42+
- If querying for a single entry/asset (using `.entry()` OR `.findOne()`), the result will be an object i.e. `{ entry: {} }`, if the entry or asset is not found, `{ entry: null }` will be returned.
43+
- Querying multiple entries, would return `{ entries: [ {...} ] }`.
4144

4245

4346
1. Query a single entry
@@ -58,9 +61,11 @@ Notes
5861
// locale: ''
5962
// }
6063
})
64+
.catch(reject)
65+
6166
// Sample 2. Returns the 1st entry that matches query filters
6267
Stack.contentType('blogs')
63-
.entries() // OR .assets()
68+
.entries() // for .assets() OR .schemas() - ignore calling .contentType()
6469
.language('en-us')
6570
.findOne()
6671
.then((result) => {
@@ -73,15 +78,13 @@ Notes
7378
// locale: ''
7479
// }
7580
})
76-
.catch()
81+
.catch(reject)
7782
```
7883

79-
2. Querying a set of entries or assets
80-
84+
2. Querying a set of entries, assets or content types
8185
```js
8286
Stack.contentType('blogs')
83-
.entries()
84-
.includeContentType()
87+
.entries() // for .assets() OR .schemas() - ignore calling .contentType()
8588
.includeCount()
8689
.find()
8790
.then((result) => {
@@ -92,14 +95,11 @@ Notes
9295
// title: ''
9396
// }
9497
// ],
95-
// content_type: {
96-
// title: 'Blogs',
97-
// ...
98-
// },
9998
// content_type_uid: 'blogs',
10099
// locale: '',
101100
// count: 1
102101
// }
103102
})
104-
.catch()
105-
```
103+
.catch(reject)
104+
```
105+
For more details on queries supported/querying, [refer this](./mongodb-sdk-querying.md)!

dist/stack.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,20 @@ class Stack {
446446
throw new Error('Kindly provide a valid field and expr/fn value for \'.where()\'');
447447
}
448448
else if (this.q.query && typeof this.q.query === 'object') {
449+
if (typeof expr === 'function') {
450+
expr = expr.toString();
451+
}
449452
this.q.query = lodash_1.merge(this.q.query, {
450453
$where: expr,
451454
});
452455
}
453456
else {
454-
this.q.query.$where = expr;
457+
if (typeof expr === 'function') {
458+
expr = expr.toString();
459+
}
460+
this.q.query = {
461+
$where: expr,
462+
};
455463
}
456464
return this;
457465
}
@@ -535,9 +543,6 @@ class Stack {
535543
return new Promise((resolve, reject) => {
536544
const queryFilters = this.preProcess(query);
537545
this.collection = this.collection.find(queryFilters);
538-
if (this.internal.sort) {
539-
this.collection = this.collection.sort(this.internal.sort);
540-
}
541546
if (this.internal.queryReferences) {
542547
return this.collection
543548
.project(this.internal.projections)
@@ -546,6 +551,7 @@ class Stack {
546551
if (result === null || result.length === 0) {
547552
return resolve({ count: 0 });
548553
}
554+
this.internal.includeReferences = true;
549555
return this.includeReferencesI(result, this.q.locale, {}, undefined)
550556
.then(() => {
551557
result = sift_1.default(this.internal.queryReferences, result);

dist/util.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
*/
77
Object.defineProperty(exports, "__esModule", { value: true });
88
const lodash_1 = require("lodash");
9-
exports.append = (field) => {
10-
return `data.${field}`;
11-
};
129
exports.validateURI = (uri) => {
1310
if (typeof uri !== 'string' || uri.length === 0) {
1411
throw new Error(`Mongodb connection url: ${uri} must be of type string`);
@@ -37,25 +34,3 @@ const getParents = (child, mapping) => {
3734
}
3835
return parents;
3936
};
40-
exports.mask = (json, filter) => {
41-
filter.forEach((field) => {
42-
maskKeys(json, field.split('.'), 0);
43-
});
44-
};
45-
const maskKeys = (json, arr, pos) => {
46-
const key = arr[pos];
47-
if (json.hasOwnProperty(key)) {
48-
if (pos === arr.length - 1) {
49-
delete json[key];
50-
}
51-
else {
52-
pos++;
53-
maskKeys(json[key], arr, pos);
54-
}
55-
}
56-
else if (typeof json === 'object' && json instanceof Array && json.length) {
57-
json.forEach((sub) => {
58-
maskKeys(sub, arr, pos);
59-
});
60-
}
61-
};

example/random.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Stack = Contentstack.Stack({
1414
}
1515
],
1616
dbName: 'sync-test',
17-
collectionName: 'references'
17+
collectionName: 'count'
1818
})
1919

2020
Stack.connect({collectionName: 'logical'}).then(() => {
@@ -26,13 +26,18 @@ Stack.connect({collectionName: 'logical'}).then(() => {
2626
// .catch(console.error)
2727
Stack.contentType('blog')
2828
.entries()
29-
.includeReferences()
29+
// .includeReferences()
30+
// .where(function () {
31+
// return (this.no === 1)
32+
// })
33+
.queryReferences({'authors.uid': 'a10'})
3034
// .only(['published_at'])
3135
// .except(['published_at'])
3236
// .ascending()
3337
// .descending()
3438
// .and([{uid: 'b1'}, {no: 1}])
35-
.find()
39+
// .find()
40+
.count()
3641
.then(console.log)
3742
.catch(console.error)
3843
})

json-mask.js

Lines changed: 0 additions & 71 deletions
This file was deleted.

mongodb-fs-sdk.info.md

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)