Skip to content

Commit 8910991

Browse files
committed
test cases updated
1 parent d3eda00 commit 8910991

22 files changed

+1681
-316
lines changed

dist/stack.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ class Stack {
5252
}
5353
if (!(field) || typeof field !== 'string') {
5454
if (this.internal.sort && typeof this.internal.sort === 'object') {
55-
this.internal.sort[field] = -1;
55+
this.internal.sort.published_at = -1;
5656
}
5757
else {
5858
this.internal.sort = {
59-
[field]: -1,
59+
published_at: -1,
6060
};
6161
}
6262
}
@@ -101,7 +101,7 @@ class Stack {
101101
this.q.locale = code;
102102
return this;
103103
}
104-
and(...queries) {
104+
and(queries) {
105105
if (this.q.query && typeof this.q.query === 'object') {
106106
this.q.query = lodash_1.merge(this.q.query, {
107107
$and: queries,
@@ -114,7 +114,7 @@ class Stack {
114114
}
115115
return this;
116116
}
117-
or(...queries) {
117+
or(queries) {
118118
if (this.q.query && typeof this.q.query === 'object') {
119119
this.q.query = lodash_1.merge(this.q.query, {
120120
$or: queries,
@@ -376,10 +376,11 @@ class Stack {
376376
if (!fields || typeof fields !== 'object' || !(fields instanceof Array) || fields.length === 0) {
377377
throw new Error('Kindly provide valid \'field\' values for \'only()\'');
378378
}
379-
this.internal.projections = this.internal.projections || {};
379+
this.internal.only = this.internal.only || {};
380+
this.internal.only._id = 0;
380381
fields.forEach((field) => {
381382
if (typeof field === 'string') {
382-
this.internal.projections[field] = 1;
383+
this.internal.only[field] = 1;
383384
}
384385
});
385386
return this;
@@ -388,12 +389,13 @@ class Stack {
388389
if (!fields || typeof fields !== 'object' || !(fields instanceof Array) || fields.length === 0) {
389390
throw new Error('Kindly provide valid \'field\' values for \'except()\'');
390391
}
391-
this.internal.projections = this.internal.projections || {};
392+
this.internal.except = this.internal.except || {};
392393
fields.forEach((field) => {
393394
if (typeof field === 'string') {
394-
this.internal.projections[field] = 0;
395+
this.internal.except[field] = 0;
395396
}
396397
});
398+
this.internal.except = lodash_1.merge(this.config.projections, this.internal.except);
397399
return this;
398400
}
399401
regex(field, pattern, options = 'i') {
@@ -482,13 +484,18 @@ class Stack {
482484
find(query = {}) {
483485
return new Promise((resolve, reject) => {
484486
const queryFilters = this.preProcess(query);
487+
if (this.internal.sort) {
488+
this.collection = this.collection.find(queryFilters).sort(this.internal.sort);
489+
}
490+
else {
491+
this.collection = this.collection.find(queryFilters);
492+
}
485493
if (this.internal.queryReferences) {
486-
return this.queryOnReferences(queryFilters)
494+
return this.queryOnReferences()
487495
.then(resolve)
488496
.catch(reject);
489497
}
490498
return this.collection
491-
.find(queryFilters)
492499
.project(this.internal.projections)
493500
.limit(this.internal.limit)
494501
.skip(this.internal.skip)
@@ -573,10 +580,9 @@ class Stack {
573580
.catch(reject);
574581
});
575582
}
576-
queryOnReferences(query) {
583+
queryOnReferences() {
577584
return new Promise((resolve, reject) => {
578585
return this.collection
579-
.find(query)
580586
.project(this.internal.projections)
581587
.toArray()
582588
.then((result) => {
@@ -611,11 +617,11 @@ class Stack {
611617
else {
612618
this.q.query = {};
613619
}
614-
if (this.internal.projections) {
615-
this.internal.projections = lodash_1.merge(this.config.projections, this.internal.projections);
620+
if (this.internal.only) {
621+
this.internal.projections = this.internal.only;
616622
}
617623
else {
618-
this.internal.projections = this.config.projections;
624+
this.internal.projections = lodash_1.merge(this.config.projections, this.internal.except);
619625
}
620626
if (!(this.internal.limit)) {
621627
this.internal.limit = this.config.limit;
@@ -741,7 +747,7 @@ class Stack {
741747
},
742748
};
743749
referencesFound.push(new Promise((rs, rj) => {
744-
return self.db.collection('contents')
750+
return self.db.collection(this.config.collectionName)
745751
.find(query)
746752
.project(self.config.projections)
747753
.toArray()

dist/util.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,25 @@ const getParents = (child, mapping) => {
3737
}
3838
return parents;
3939
};
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/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Stack = Contentstack.Stack({
1919

2020
function connect () {
2121
return new Promise((resolve, reject) => {
22-
return Stack.connect()
22+
return Stack.connect({dbName: 'sync-test', collectionName: 'core'})
2323
.then(resolve)
2424
.catch(reject)
2525
})

example/random.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const Contentstack = require('../dist/contentstack').Contentstack
1+
const Contentstack = require('../dist').Contentstack
22

33
const Stack = Contentstack.Stack({
44
api_key: '',
@@ -13,13 +13,25 @@ const Stack = Contentstack.Stack({
1313
relative_url_prefix: '/es/'
1414
}
1515
],
16-
dbName: 'sync-test'
16+
dbName: 'sync-test',
17+
collectionName: 'references'
1718
})
1819

19-
Stack.connect().then(() => {
20-
Stack.asset()
21-
// .schemas()
22-
// .entries()
20+
Stack.connect({collectionName: 'logical'}).then(() => {
21+
// Stack.asset()
22+
// // .schemas()
23+
// // .entries()
24+
// .find()
25+
// .then(console.log)
26+
// .catch(console.error)
27+
Stack.contentType('blog')
28+
.entries()
29+
.includeReferences()
30+
// .only(['published_at'])
31+
// .except(['published_at'])
32+
// .ascending()
33+
// .descending()
34+
// .and([{uid: 'b1'}, {no: 1}])
2335
.find()
2436
.then(console.log)
2537
.catch(console.error)

json-mask.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var obj = {
2+
name: 'rk',
3+
person: {
4+
age: 1,
5+
no: 2,
6+
key: {
7+
k1: 1,
8+
k2: 2
9+
}
10+
},
11+
arr1: [{
12+
k: 0,
13+
a: 'a'
14+
},
15+
{
16+
k: 1,
17+
b: 'b'
18+
}
19+
],
20+
arr2: [{
21+
k: 0,
22+
k1: '00'
23+
},
24+
{
25+
k: 1,
26+
k1: '01'
27+
}
28+
],
29+
deep: {
30+
depth1: {
31+
no: 'no'
32+
},
33+
depth2: {
34+
depth: null
35+
},
36+
depth3: {
37+
depth2: [
38+
{
39+
k1: 'k1',
40+
k2: 'k2'
41+
}
42+
]
43+
}
44+
}
45+
}
46+
47+
const filter = ['arr1.k', 'arr2.k1', 'deep.depth3.depth2.k2']
48+
49+
function mask (json, filter) {
50+
filter.forEach((field) => {
51+
maskKeys(json, field.split('.'), 0)
52+
})
53+
}
54+
55+
function maskKeys(json, arr, pos) {
56+
const key = arr[pos]
57+
if (json.hasOwnProperty(key)) {
58+
if (pos === arr.length - 1) {
59+
delete json[key]
60+
} else {
61+
pos++
62+
maskKeys(json[key], arr, pos)
63+
}
64+
} else if (typeof json === 'object' && json instanceof Array && json.length) {
65+
json.forEach((sub) => {
66+
maskKeys(sub, arr, pos)
67+
})
68+
}
69+
}
70+
mask(obj, filter)
71+
console.log(JSON.stringify(obj, null, 1))

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
"clean": "rimraf dist typings coverage",
88
"build-ts": "npm run clean && tsc",
99
"watch-ts": "npm run clean && tsc -w",
10-
"compile": "tsc",
11-
"prepare": "npm run compile",
1210
"start": "dist/index.js",
1311
"tslint": "npx tslint -c tslint.json 'src/**/*.ts' --fix",
14-
"test": "jest --verbose --colors"
12+
"test": "jest"
1513
},
1614
"author": "Contentstack Ecosystem",
1715
"license": "ISC",

0 commit comments

Comments
 (0)