Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added projections support and updated test cases for global fields #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.config = {
collection: {
asset: 'contents',
entry: 'contents',
schema: 'content_types',
schema: 'contents',
},
dbName: 'contentstack-db',
indexes: {
Expand Down
76 changes: 54 additions & 22 deletions dist/stack.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"use strict";
/*!
* Contentstack DataSync Mongodb SDK
* Copyright (c) 2019 Contentstack LLC
* MIT Licensed
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
Expand All @@ -16,6 +11,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*!
* Contentstack DataSync Mongodb SDK
* Copyright (c) 2019 Contentstack LLC
* MIT Licensed
*/
//import mask from 'json-mask'
const lodash_1 = require("lodash");
const mongodb_1 = require("mongodb");
const sift_1 = __importDefault(require("sift"));
Expand Down Expand Up @@ -1045,11 +1046,11 @@ class Stack {
if (!fields || typeof fields !== 'object' || !(fields instanceof Array) || fields.length === 0) {
throw new Error('Kindly provide valid \'field\' values for \'only()\'');
}
this.internal.only = this.internal.only || {};
this.internal.only._id = 0;
this.internal.only = [];
//this.internal.only._id = 0
fields.forEach((field) => {
if (typeof field === 'string') {
this.internal.only[field] = 1;
this.internal.only.push(field);
}
});
return this;
Expand Down Expand Up @@ -1080,13 +1081,12 @@ class Stack {
if (!fields || typeof fields !== 'object' || !(fields instanceof Array) || fields.length === 0) {
throw new Error('Kindly provide valid \'field\' values for \'except()\'');
}
this.internal.except = this.internal.except || {};
this.internal.except = [];
fields.forEach((field) => {
if (typeof field === 'string') {
this.internal.except[field] = 0;
this.internal.except.push(field);
}
});
this.internal.except = lodash_1.merge(this.contentStore.projections, this.internal.except);
return this;
}
/**
Expand Down Expand Up @@ -1160,7 +1160,6 @@ class Stack {
if (!values || typeof values !== 'object' || !(values instanceof Array)) {
throw new Error('Kindly provide valid \'field\' values for \'tags()\'');
}
// filter non-string keys
lodash_1.remove(values, (value) => {
return typeof value !== 'string';
});
Expand Down Expand Up @@ -1460,12 +1459,12 @@ class Stack {
}
if (this.internal.queryReferences) {
this.collection = this.collection
.project(this.internal.projections)
.project(this.contentStore.projections)
.toArray();
}
else {
this.collection = this.collection
.project(this.internal.projections)
.project(this.contentStore.projections)
.limit(this.internal.limit)
.skip(this.internal.skip)
.toArray();
Expand Down Expand Up @@ -1590,12 +1589,6 @@ class Stack {
}
// tslint:disable-next-line: max-line-length
this.q.referenceDepth = (typeof this.q.referenceDepth === 'number') ? this.q.referenceDepth : this.contentStore.referenceDepth;
if (this.internal.only) {
this.internal.projections = this.internal.only;
}
else {
this.internal.projections = lodash_1.merge(this.contentStore.projections, this.internal.except);
}
// set default limit, if .limit() hasn't been called
if (!(this.internal.limit)) {
this.internal.limit = this.contentStore.limit;
Expand Down Expand Up @@ -1657,7 +1650,7 @@ class Stack {
postProcess(result) {
return __awaiter(this, void 0, void 0, function* () {
const count = (result === null) ? 0 : result.length;
const output = {
let output = {
locale: this.q.locale,
};
if (this.internal.onlyCount) {
Expand All @@ -1666,35 +1659,74 @@ class Stack {
output.count = count;
return output;
}
let type;
switch (this.q.content_type_uid) {
case this.types.assets:
if (this.internal.single) {
output.asset = (result === null) ? result : result[0];
type = 'asset';
}
else {
output.assets = result;
type = 'assets';
}
output.content_type_uid = 'assets';
break;
case this.types.content_types:
if (this.internal.single) {
output.content_type = (result === null) ? result : result[0];
type = 'content_type';
}
else {
output.content_types = result;
type = 'content_types';
}
output.content_type_uid = 'content_types';
break;
default:
if (this.internal.single) {
output.entry = (result === null) ? result : result[0];
type = 'entry';
}
else {
output.entries = result;
type = 'entries';
}
output.content_type_uid = this.q.content_type_uid;
break;
}
if (this.internal.only) {
const bukcet = JSON.parse(JSON.stringify(output[type]));
this.internal.only.forEach(field => {
let splittedField = field.split('.');
bukcet.forEach(obj => {
if (obj.hasOwnProperty(field)) {
delete obj[field];
}
else {
let depth = 0;
let parent = '';
util_1.applyProjections(obj, splittedField, depth, parent);
}
});
});
output[type] = util_1.difference(output[type], bukcet);
}
else if (this.internal.except) {
this.internal.except.forEach(field => {
let splittedField = field.split('.');
output[type].forEach(obj => {
if (obj.hasOwnProperty(field)) {
delete obj[field];
}
else {
let depth = 0;
let parent = '';
util_1.applyProjections(obj, splittedField, depth, parent);
}
});
});
}
if (this.internal.includeCount) {
output.count = yield this.db.collection(util_1.getCollectionName({
content_type_uid: this.q.content_type_uid,
Expand Down Expand Up @@ -1736,7 +1768,7 @@ class Stack {
_assets: 1,
_id: 0,
});
if (schema === null || schema[this.types.assets] !== 'object') {
if (schema === null || typeof schema[this.types.assets] !== 'object') {
return;
}
const paths = Object.keys(schema[this.types.assets]);
Expand Down
35 changes: 35 additions & 0 deletions dist/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,38 @@ exports.getCollectionName = ({ locale, content_type_uid }, collection) => {
return `${locale}.${collection.entry}`;
}
};
exports.difference = (obj, baseObj) => {
const changes = (data, base) => {
return lodash_1.transform(data, (result, value, key) => {
if (!lodash_1.isEqual(value, base[key])) {
result[key] = (lodash_1.isObject(value) && lodash_1.isObject(base[key])) ? changes(value, base[key]) : value;
}
});
};
return changes(obj, baseObj);
};
exports.applyProjections = (data, keys, depth, parent) => {
for (let prop in data) {
if (prop === keys[depth] && keys.length - 1 === depth) {
let array = keys.slice(0);
let field = array.slice(-1).pop();
array.pop();
if ((array.join('.')) === parent)
delete data[field];
}
else if (typeof data[prop] === 'object') {
if (prop === keys[depth]) {
depth = depth + 1;
parent = parent !== '' ? parent + '.' + prop : prop;
if (data[prop] instanceof Array) {
data[prop].forEach(element => {
exports.applyProjections(element, keys, depth, parent);
});
}
else {
exports.applyProjections(data[prop], keys, depth, parent);
}
}
}
}
};
13 changes: 5 additions & 8 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const Contentstack = require('../dist').Contentstack

const Stack = Contentstack.Stack({
locale: 'en-us',
contentStore: {
dbName: 'references',
collection: {
entry: 'contents',
asset: 'contents',
schema: 'content_types'
}
dbName: 'contentstack-db',
}
})

Expand All @@ -20,9 +14,12 @@ function close () {
return Stack.close()
}

function find (contentType = 'blog') {
function find (contentType = 'authors') {
return Stack.contentType(contentType)
.entries()
//.includeReferences()
//.except(['image_thumbnails.title', 'image_thumbnails._internal_url'])
//.limit(3)
.find()
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Contentstack Ecosystem <[email protected]>",
"name": "datasync-mongodb-sdk",
"version": "1.0.2",
"version": "1.0.3",
"description": "Mongodb query wrapper around contents synced via @contentstack/content-store-mongodb",
"main": "dist/index.js",
"scripts": {
Expand All @@ -17,6 +17,7 @@
},
"license": "MIT",
"dependencies": {
"json-mask": "^0.3.8",
"lodash": "4.17.15",
"mongodb": "3.1.12",
"sift": "8.5.0"
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const config = {
collection: {
asset: 'contents',
entry: 'contents',
schema: 'content_types',
schema: 'contents',
},
dbName: 'contentstack-db',
indexes: {
Expand Down
Loading