Skip to content

Commit b95649c

Browse files
committed
added test cases
1 parent 0178a92 commit b95649c

File tree

10 files changed

+422
-58
lines changed

10 files changed

+422
-58
lines changed

dist/stack.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
88
return (mod && mod.__esModule) ? mod : { "default": mod };
99
};
1010
Object.defineProperty(exports, "__esModule", { value: true });
11-
const debug_1 = __importDefault(require("debug"));
1211
const lodash_1 = require("lodash");
1312
const mongodb_1 = require("mongodb");
1413
const sift_1 = __importDefault(require("sift"));
1514
const config_1 = require("./config");
1615
const util_1 = require("./util");
17-
const debug = debug_1.default('stack');
1816
class Stack {
1917
constructor(stackConfig, existingDB) {
2018
this.config = lodash_1.merge(config_1.config, stackConfig);
@@ -68,7 +66,6 @@ class Stack {
6866
this.client = client;
6967
return client.connect().then(() => {
7068
this.db = client.db(dbName);
71-
debug('Db connection set successfully!');
7269
return resolve(this.db);
7370
}).catch(reject);
7471
}
@@ -78,7 +75,6 @@ class Stack {
7875
});
7976
}
8077
close() {
81-
debug('Closing db connection!');
8278
this.client.close();
8379
}
8480
language(code) {
@@ -381,7 +377,7 @@ class Stack {
381377
});
382378
return this;
383379
}
384-
regex(field, pattern, options = 'g') {
380+
regex(field, pattern, options = 'i') {
385381
if (!(field) || !(pattern) || typeof field !== 'string' || typeof pattern !== 'string') {
386382
throw new Error('Kindly provide a valid field and pattern value for \'.regex()\'');
387383
}
@@ -424,7 +420,7 @@ class Stack {
424420
}
425421
return this;
426422
}
427-
where(...expr) {
423+
where(expr) {
428424
if (!(expr)) {
429425
throw new Error('Kindly provide a valid field and expr/fn value for \'.where()\'');
430426
}
@@ -438,10 +434,6 @@ class Stack {
438434
}
439435
return this;
440436
}
441-
count() {
442-
this.collection = this.collection.count();
443-
return this;
444-
}
445437
includeCount() {
446438
this.internal.includeCount = true;
447439
return this;
@@ -511,6 +503,45 @@ class Stack {
511503
});
512504
});
513505
}
506+
count(query) {
507+
return new Promise((resolve, reject) => {
508+
const queryFilters = this.preProcess(query);
509+
if (this.internal.queryReferences) {
510+
return this.collection
511+
.find(queryFilters)
512+
.project(this.internal.projections)
513+
.toArray()
514+
.then((result) => {
515+
if (result === null || result.length === 0) {
516+
return resolve({ count: 0 });
517+
}
518+
return this.includeReferencesI(result, this.q.locale, {}, undefined)
519+
.then(() => {
520+
result = sift_1.default(this.internal.queryReferences, result);
521+
result = result.length;
522+
this.cleanup();
523+
return resolve({ count: result });
524+
});
525+
})
526+
.catch((error) => {
527+
this.cleanup();
528+
return reject(error);
529+
});
530+
}
531+
return this.collection
532+
.find(queryFilters)
533+
.project(this.internal.projections)
534+
.count()
535+
.then((result) => {
536+
this.cleanup();
537+
return resolve({ count: result });
538+
})
539+
.catch((error) => {
540+
this.cleanup();
541+
return reject(error);
542+
});
543+
});
544+
}
514545
findOne(query = {}) {
515546
return new Promise((resolve, reject) => {
516547
this.internal.single = true;
@@ -573,7 +604,6 @@ class Stack {
573604
this.q.locale = this.config.locales[0].code;
574605
}
575606
if (this.q.content_type_uid === 'contentTypes') {
576-
debug('Removing \'locale\' filter, since the query is on content types');
577607
delete this.q.locale;
578608
}
579609
const filters = Object.assign({ content_type_uid: this.q.content_type_uid, locale: this.q.locale }, this.q.query);
@@ -598,8 +628,8 @@ class Stack {
598628
}
599629
cleanup() {
600630
this.collection = null;
601-
this.internal = {};
602-
this.q = {};
631+
this.internal = null;
632+
this.q = null;
603633
}
604634
postProcess(result, contentType) {
605635
const count = (result === null) ? 0 : result.length;

example/count.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const Contentstack = require('../dist/contentstack').Contentstack
2+
3+
const Stack = Contentstack.Stack({
4+
api_key: '',
5+
access_token: '',
6+
locales: [
7+
{
8+
code: 'en-us',
9+
relative_url_prefix: '/'
10+
},
11+
{
12+
code: 'es-es',
13+
relative_url_prefix: '/es/'
14+
}
15+
],
16+
})
17+
18+
function connect () {
19+
return new Promise((resolve, reject) => {
20+
return Stack.connect()
21+
.then(resolve)
22+
.catch(reject)
23+
})
24+
}
25+
26+
function close () {
27+
return Stack.close()
28+
}
29+
30+
function count (contentType = 'blog') {
31+
return new Promise((resolve, reject) => {
32+
Stack.contentType(contentType)
33+
.entries()
34+
// .includeCount()
35+
// .includeReferences()
36+
// .queryReferences({'authors.category': {}})
37+
// .includeSchema()
38+
// .language('es-es')
39+
// .notEqualTo('title', 'Blog One')
40+
// .query({tags: {$in: ['one', 'two']}})
41+
.queryReferences({'authors.category.uid': 'c2'})
42+
// .excludeReferences()
43+
// .excludeAssets()
44+
// .limit(1)
45+
// .skip(1)
46+
// .query({
47+
// uid: 'blt17559b99fee73d6f'
48+
// })
49+
.count()
50+
.then(resolve)
51+
.catch(reject)
52+
})
53+
}
54+
55+
return connect()
56+
.then(() => {
57+
return count()
58+
.then((result) => {
59+
const keys = Object.keys(result)
60+
// Sample output
61+
// {
62+
// entries: [
63+
// {
64+
// title: 'French author',
65+
// gender: null,
66+
// age: null,
67+
// summary: '',
68+
// tags: [
69+
70+
// ],
71+
// locale: 'es-es',
72+
// uid: 'blt17559b99fee73d6f'
73+
// }
74+
// ],
75+
// content_type_uid: 'authors',
76+
// locale: 'es-es'
77+
// }
78+
console.log(JSON.stringify(result, null, 1))
79+
})
80+
})
81+
.then(close)
82+
.then(() => {
83+
console.info('Data received successfully!')
84+
})
85+
.catch(console.error)

example/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function find (contentType = 'blog') {
3333
.entries()
3434
// .includeCount()
3535
.includeReferences()
36-
.queryReferences({'authors.category': {}})
36+
// .queryReferences({'authors.category': {}})
3737
// .includeSchema()
3838
// .language('es-es')
3939
// .notEqualTo('title', 'Blog One')

mongodb-fs-sdk.info.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
- .connect()
2+
New method
3+
Use this method, to connect with the DB
4+
5+
- .close()
6+
New method
7+
This method closes the connection with the DB (mongodb-sdk-specific)
8+
9+
- .entry(uid?)
10+
Method functionality updated
11+
Pass entry `uid` that you'd want to find. If the uid is not provided, returns the 1st element found in that content type
12+
Returns `{ entry: { ... } }` OR `{ entry: null }`
13+
14+
- .entries()
15+
New method
16+
Used for querying all entries in the `.contentType({{ uid }})` specified
17+
Note: Call `.contentType({{ uid }})` before calling this method
18+
19+
- .asset(uid?)
20+
Method functionality updated
21+
Pass asset `uid` that you'd want to find. If the uid is not provided, returns the 1st element found in assets
22+
Returns `{ asset: { ... } }` OR `{ asssets: null }`
23+
Note: No need to call `.contentType({{ uid }})` while calling this
24+
25+
- .assets()
26+
New method
27+
Used for querying all assets
28+
Note: No need to call `.contentType({{ uid }})` while calling this
29+
30+
- .schema(uid?)
31+
New method
32+
Method functionality updated
33+
Pass asset `uid` that you'd want to find. If the uid is not provided, returns the 1st element found in content type schemas
34+
Returns `{ content_type: { ... } }` OR `{ content_type: null }`
35+
Note: No need to call `.contentType({{ uid }})` while calling this
36+
37+
- .schemas()
38+
New method
39+
Used for querying all the content type schemas
40+
Note: No need to call `.contentType({{ uid }})` while calling this
41+
42+
- .regex(field, pattern, options?)
43+
Implementation updated
44+
Options field is now optional, by default, it will become `i` i.e. case insensitivity match
45+
Ref. https://docs.mongodb.com/manual/reference/operator/query/regex/
46+
47+
- .where(expr)
48+
Implementation updated
49+
Accepts an expression, instead of 'field' and 'value'
50+
Ref. https://docs.mongodb.com/manual/reference/operator/query/where/index.html
51+
52+
- .count()
53+
New method
54+
Returns only the count of the query filters fired
55+
56+
- .includeReferences()
57+
New method
58+
Returns all entries and their references
59+
60+
- .excludeReferences()
61+
New method
62+
Returns all entries, without any references
63+
Note: Since assets are considered as references, they'll not be evaluated
64+
65+
- .queryReferences(query)
66+
New method
67+
Accepts a query object, which can be used to query on the reference fields
68+
Note: This is a slow method, since it scans all documents and fires the `reference` query on them. Use `.query()` filters to reduce the total no of documents being scanned
69+
70+
- .find()
71+
Implementation updated
72+
Can be used for `.assets()`, `.assets()`, `.entry()`, `.entries()`, `.schema()` and `.schemas()`
73+
By default, if assets are in 'published' state, their json will be attached in entries. Else, the file fields will be either `null` OR `[]`
74+
75+
- .findOne()
76+
Implementation updated
77+
Can be used for `.assets()`, `.assets()`, `.entry()`, `.entries()`, `.schema()` and `.schemas()`
78+
By default, if assets are in 'published' state, their json will be attached in entries. Else, the file fields will be either `null` OR `{}`

0 commit comments

Comments
 (0)