Skip to content

Commit 2b4730f

Browse files
committed
support mongostore 0.1.0
1 parent 1cd0266 commit 2b4730f

File tree

10 files changed

+113
-14
lines changed

10 files changed

+113
-14
lines changed

dist/stack.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const util_1 = require("./util");
3131
class Stack {
3232
constructor(stackConfig, existingDB) {
3333
this.config = lodash_1.merge(config_1.config, stackConfig);
34+
// validates config.locales property
35+
util_1.validateConfig(this.config);
3436
this.contentStore = this.config.contentStore;
3537
this.types = this.contentStore.internalContentTypes;
3638
this.q = {};
@@ -199,7 +201,6 @@ class Stack {
199201
[index]: type
200202
})
201203
.then(() => {
202-
console.info(`Index '${index}' created successfully!`);
203204
return;
204205
});
205206
}
@@ -237,7 +238,7 @@ class Stack {
237238
if (!(code) || typeof code !== 'string' || !(lodash_1.find(this.config.locales, {
238239
code
239240
}))) {
240-
throw new Error(`Language queried is invalid ${code}`);
241+
throw new Error(`Language ${code} is invalid!`);
241242
}
242243
this.q.locale = code;
243244
return this;
@@ -1423,9 +1424,7 @@ class Stack {
14231424
let queryFilters = this.preProcess(query);
14241425
if (this.internal.includeSpecificReferences) {
14251426
const projections = yield this.excludeSpecificReferences(this.internal.includeSpecificReferences, this.q.content_type_uid, this.internal.hasOwnProperty('only'));
1426-
console.log('projections', projections);
14271427
this.internal.projections = lodash_1.merge(this.internal.projections, projections);
1428-
console.log('this.internal.projections', this.internal.projections);
14291428
}
14301429
if (this.internal.sort) {
14311430
this.collection = this.collection.find(queryFilters).sort(this.internal.sort);
@@ -1607,15 +1606,19 @@ class Stack {
16071606
else {
16081607
this.internal.projections = lodash_1.merge(this.contentStore.projections, this.internal.except);
16091608
}
1609+
// set default limit, if .limit() hasn't been called
16101610
if (!(this.internal.limit)) {
16111611
this.internal.limit = this.contentStore.limit;
16121612
}
1613+
// set default skip, if .skip() hasn't been called
16131614
if (!(this.internal.skip)) {
16141615
this.internal.skip = this.contentStore.skip;
16151616
}
1617+
// set default locale, if no locale has been passed
16161618
if (!(this.q.locale)) {
16171619
this.q.locale = this.config.locales[0].code;
16181620
}
1621+
// if querying for content types, remove if any locale has been set
16191622
if (this.q.content_type_uid === this.types.content_types) {
16201623
delete this.q.locale;
16211624
}
@@ -1632,6 +1635,19 @@ class Stack {
16321635
],
16331636
};
16341637
}
1638+
else if (this.q.content_type_uid === this.types.assets) {
1639+
// since, content type will take up 1 item-space
1640+
queryFilters = {
1641+
$and: [
1642+
filters,
1643+
{
1644+
_version: {
1645+
$exists: true
1646+
}
1647+
},
1648+
],
1649+
};
1650+
}
16351651
else {
16361652
queryFilters = filters;
16371653
}

dist/util.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,28 @@ const getParents = (child, mapping) => {
5050
}
5151
return parents;
5252
};
53+
const validateLocales = (config) => {
54+
if (!(config.locales) || typeof config.locales !== 'object' || !(config.locales instanceof Array)) {
55+
throw new Error(`Kindly provide 'config.locales: [ { code: '' }, ...]'`);
56+
}
57+
config.locales.forEach((locale) => {
58+
if (!(locale) || typeof locale.code !== 'string' || locale.code.length === 0) {
59+
throw new Error(`Locale ${locale} is invalid!`);
60+
}
61+
});
62+
return;
63+
};
64+
const validateContentStore = (contentStore) => {
65+
if (typeof contentStore.dbName !== 'string' || contentStore.dbName.length === 0) {
66+
throw new Error(`Contentstore dbName should be of type string and not empty!`);
67+
}
68+
if (typeof contentStore.collectionName !== 'string' || contentStore.collectionName.length === 0) {
69+
throw new Error(`Contentstore collectionName should be of type string and not empty!`);
70+
}
71+
return;
72+
};
73+
exports.validateConfig = (config) => {
74+
validateLocales(config);
75+
validateContentStore(config.contentStore);
76+
return;
77+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "datasync-mongodb-sdk",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Contentstack sync utility's Mongodb SDK",
55
"main": "dist/index.js",
66
"scripts": {
@@ -14,7 +14,7 @@
1414
"test": "jest"
1515
},
1616
"author": "Contentstack Ecosystem",
17-
"license": "ISC",
17+
"license": "MIT",
1818
"dependencies": {
1919
"debug": "4.1.1",
2020
"lodash": "4.17.11",

src/stack.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { filter, find, map, merge, remove, uniq } from 'lodash'
88
import { Db, MongoClient } from 'mongodb'
99
import sift from 'sift'
1010
import { config } from './config'
11-
import { checkCyclic, validateURI } from './util'
11+
import { checkCyclic, validateConfig, validateURI } from './util'
1212

1313
/**
1414
* @class Stack
@@ -29,6 +29,8 @@ export class Stack {
2929

3030
constructor(stackConfig, existingDB ? ) {
3131
this.config = merge(config, stackConfig)
32+
// validates config.locales property
33+
validateConfig(this.config)
3234
this.contentStore = this.config.contentStore
3335
this.types = this.contentStore.internalContentTypes
3436
this.q = {}
@@ -204,7 +206,6 @@ export class Stack {
204206
[index]: type
205207
})
206208
.then(() => {
207-
console.info(`Index '${index}' created successfully!`)
208209
return
209210
})
210211
}
@@ -244,7 +245,7 @@ export class Stack {
244245
if (!(code) || typeof code !== 'string' || !(find(this.config.locales, {
245246
code
246247
}))) {
247-
throw new Error(`Language queried is invalid ${code}`)
248+
throw new Error(`Language ${code} is invalid!`)
248249
}
249250
this.q.locale = code
250251

@@ -1482,9 +1483,7 @@ export class Stack {
14821483

14831484
if (this.internal.includeSpecificReferences) {
14841485
const projections = await this.excludeSpecificReferences(this.internal.includeSpecificReferences, this.q.content_type_uid, this.internal.hasOwnProperty('only'))
1485-
console.log('projections', projections)
14861486
this.internal.projections = merge(this.internal.projections, projections)
1487-
console.log('this.internal.projections', this.internal.projections)
14881487
}
14891488

14901489
if (this.internal.sort) {
@@ -1681,22 +1680,27 @@ export class Stack {
16811680
this.internal.projections = merge(this.contentStore.projections, this.internal.except)
16821681
}
16831682

1683+
// set default limit, if .limit() hasn't been called
16841684
if (!(this.internal.limit)) {
16851685
this.internal.limit = this.contentStore.limit
16861686
}
16871687

1688+
// set default skip, if .skip() hasn't been called
16881689
if (!(this.internal.skip)) {
16891690
this.internal.skip = this.contentStore.skip
16901691
}
16911692

1693+
// set default locale, if no locale has been passed
16921694
if (!(this.q.locale)) {
16931695
this.q.locale = this.config.locales[0].code
16941696
}
16951697

1698+
// if querying for content types, remove if any locale has been set
16961699
if (this.q.content_type_uid === this.types.content_types) {
16971700
delete this.q.locale
16981701
}
16991702

1703+
17001704
const filters = {
17011705
content_type_uid: this.q.content_type_uid,
17021706
locale: this.q.locale,
@@ -1714,6 +1718,18 @@ export class Stack {
17141718
},
17151719
],
17161720
}
1721+
} else if (this.q.content_type_uid === this.types.assets) {
1722+
// since, content type will take up 1 item-space
1723+
queryFilters = {
1724+
$and: [
1725+
filters,
1726+
{
1727+
_version: {
1728+
$exists: true
1729+
}
1730+
},
1731+
],
1732+
}
17171733
} else {
17181734
queryFilters = filters
17191735
}

src/util.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,36 @@ const getParents = (child, mapping) => {
5555

5656
return parents
5757
}
58+
59+
const validateLocales = (config) => {
60+
if (!(config.locales) || typeof config.locales !== 'object' || !(config.locales instanceof Array)) {
61+
throw new Error(`Kindly provide 'config.locales: [ { code: '' }, ...]'`)
62+
}
63+
64+
config.locales.forEach((locale) => {
65+
if (!(locale) || typeof locale.code !== 'string' || locale.code.length === 0) {
66+
throw new Error(`Locale ${locale} is invalid!`)
67+
}
68+
})
69+
70+
return
71+
}
72+
73+
const validateContentStore = (contentStore) => {
74+
if (typeof contentStore.dbName !== 'string' || contentStore.dbName.length === 0) {
75+
throw new Error(`Contentstore dbName should be of type string and not empty!`)
76+
}
77+
78+
if (typeof contentStore.collectionName !== 'string' || contentStore.collectionName.length === 0) {
79+
throw new Error(`Contentstore collectionName should be of type string and not empty!`)
80+
}
81+
82+
return
83+
}
84+
85+
export const validateConfig = (config) => {
86+
validateLocales(config)
87+
validateContentStore(config.contentStore)
88+
89+
return
90+
}

test/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ describe('# Core', () => {
174174
return Stack.assets()
175175
.findOne()
176176
.then((result) => {
177+
console.error('@result', JSON.stringify(result))
177178
expect(result).toHaveProperty('asset')
178179
expect(result).toHaveProperty('content_type_uid')
179180
expect(result).toHaveProperty('locale')
@@ -186,6 +187,7 @@ describe('# Core', () => {
186187
expect((result as any).asset).not.toHaveProperty('created_at')
187188
expect((result as any).asset).not.toHaveProperty('updated_at')
188189
}).catch((error) => {
190+
console.error(error)
189191
expect(error).toBeNull()
190192
})
191193
})

test/data/assets.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const assets = [
22
{
3+
_version: 1,
34
content_type_uid: '_assets',
45
locale: 'en-us',
56
sys_keys: {
@@ -14,6 +15,7 @@ export const assets = [
1415
'https://images.contentstack.io/v3/assets/***REMOVED***/blt6788d40f2aaaab9a/5c5aae49005d89b20bb85c21/cnn.png',
1516
},
1617
{
18+
_version: 1,
1719
content_type_uid: '_assets',
1820
locale: 'en-us',
1921
sys_keys: {
@@ -28,6 +30,7 @@ export const assets = [
2830
'https://images.contentstack.io/v3/assets/***REMOVED***/blt6788d40f2aaaab9a/5c5aae49005d89b20bb85c21/cnn.png',
2931
},
3032
{
33+
_version: 2,
3134
content_type_uid: '_assets',
3235
locale: 'en-us', sys_keys: {
3336
content_type_uid : '_assets',

test/data/content_types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const content_types = [
3232
},
3333
title: 'Blog',
3434
uid: 'blog',
35-
reference: {
35+
references: {
3636
authors: 'author',
3737
self_reference: 'blog'
3838
}
@@ -70,7 +70,7 @@ export const content_types = [
7070
},
7171
title: 'Author',
7272
uid: 'author',
73-
reference: {
73+
references: {
7474
blogs: 'blog',
7575
self_reference: 'author'
7676
}

test/references.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ describe('# References', () => {
152152
expect(result).toHaveProperty('entries')
153153
expect((result as any).content_type_uid).toEqual('blog')
154154
expect((result as any).entries).toHaveLength(5)
155-
expect(entry).not.toHaveProperty('self_reference')
155+
if (entry.hasOwnProperty('self_reference')) {
156+
expect(entry).toHaveProperty('self_reference')
157+
expect(entry.self_reference).toEqual({})
158+
}
156159
})
157160
}).catch((error) => {
158161
expect(error).toBeNull()

typings/util.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ export declare const validateURI: (uri: any) => string;
2121
* @returns {boolean} Returns `true` if the `uid` is part of the map (i.e. cyclic)
2222
*/
2323
export declare const checkCyclic: (uid: any, mapping: any) => boolean;
24+
export declare const validateConfig: (config: any) => void;

0 commit comments

Comments
 (0)