periodicjs.core.data
Advanced tools
Comparing version 0.8.3 to 0.8.4
@@ -105,37 +105,37 @@ 'use strict'; | ||
Promisie.parallel({ | ||
count: () => { | ||
count: () => { | ||
return new Promisie((resolve, reject) => { | ||
Model.count(query, (err, count) => { | ||
if (err) reject(err); | ||
else resolve(count); | ||
}); | ||
}); | ||
}, | ||
pagination: () => { | ||
return Promisie.doWhilst(() => { | ||
return new Promisie((resolve, reject) => { | ||
Model.count(query, (err, count) => { | ||
_QUERY.call(this, { query, sort, limit: (total + pagelength <= limit) ? pagelength : (limit - total), fields, skip, population, model: Model, }, (err, data) => { | ||
if (err) reject(err); | ||
else resolve(count); | ||
else { | ||
skip += data.length; | ||
total += data.length; | ||
pages.total += data.length; | ||
pages.total_pages++; | ||
pages[index++] = { | ||
documents: data, | ||
count: data.length, | ||
}; | ||
resolve(data.length); | ||
} | ||
}); | ||
}); | ||
}, | ||
pagination: () => { | ||
return Promisie.doWhilst(() => { | ||
return new Promisie((resolve, reject) => { | ||
_QUERY.call(this, { query, sort, limit: (total + pagelength <= limit) ? pagelength : (limit - total), fields, skip, population, model: Model, }, (err, data) => { | ||
if (err) reject(err); | ||
else { | ||
skip += data.length; | ||
total += data.length; | ||
pages.total += data.length; | ||
pages.total_pages++; | ||
pages[index++] = { | ||
documents: data, | ||
count: data.length, | ||
}; | ||
resolve(data.length); | ||
} | ||
}); | ||
}); | ||
}, current => (current === pagelength && total < limit)) | ||
.then(() => pages) | ||
.catch(e => Promisie.reject(e)); | ||
} | ||
}) | ||
}, current => (current === pagelength && total < limit)) | ||
.then(() => pages) | ||
.catch(e => Promisie.reject(e)); | ||
}, | ||
}) | ||
.then(result => { | ||
cb(null, Object.assign({}, result.pagination, { | ||
collection_count: result.count, | ||
collection_pages: Math.ceil(result.count / ((pagelength <= limit) ? pagelength : limit)) | ||
collection_pages: Math.ceil(result.count / ((pagelength <= limit) ? pagelength : limit)), | ||
})); | ||
@@ -202,3 +202,5 @@ }) | ||
let isObjectIds = (split.filter(utility.isObjectId).length === split.length); | ||
if (isObjectIds) { query[toplevel].push({ '_id': { $in: split, }, }); } else if (Array.isArray(docid)) { | ||
if (isObjectIds) { | ||
query[toplevel].push({ '_id': { $in: split, }, }); | ||
} else if (Array.isArray(docid)) { | ||
docid.forEach(d => { | ||
@@ -347,14 +349,31 @@ if (d === '_id') { | ||
let generateChanges = (callback) => { | ||
if (!options.track_changes || (options.track_changes && !options.ensure_changes)) callback(); | ||
if (!options.track_changes || (options.track_changes && !options.ensure_changes)) { | ||
callback(); | ||
} | ||
if (options.track_changes) { | ||
let changeset = (!options.isPatch) ? utility.diff(changesetData.original, changesetData.update, depopulate) : options.updatedoc; | ||
this.changeset.create({ | ||
parent_document: { id: options.id, }, | ||
changeset: changeset, | ||
}, (err, result) => { | ||
if (options.ensure_changes) { | ||
if (err) callback(err); | ||
else callback(null, result); | ||
} | ||
}); | ||
Promise.resolve(options.originalrevision) | ||
.then(originalDoc => { | ||
if (originalDoc) { | ||
return changesetData.original; | ||
} else { | ||
return this.load({ docid: options.id, }); | ||
} | ||
}) | ||
.then(originalDoc => { | ||
changesetData.original = (typeof originalDoc.toObject === 'function') | ||
? originalDoc.toObject() | ||
: originalDoc; | ||
// console.log({ originalDoc }); | ||
let changeset = (!options.isPatch) ? utility.diff(changesetData.original, changesetData.update, depopulate) : options.updatedoc; | ||
this.changeset.create({ | ||
parent_document: { id: options.id, }, | ||
changeset: changeset, | ||
}, (err, result) => { | ||
if (options.ensure_changes) { | ||
if (err) callback(err); | ||
else callback(null, result); | ||
} | ||
}); | ||
}) | ||
.catch(callback); | ||
} | ||
@@ -365,2 +384,3 @@ }; | ||
let originalId = options.updatedoc._id; | ||
options.updatedoc.updatedat = new Date(); | ||
options.updatedoc = (depopulate) ? utility.depopulate(options.updatedoc) : options.updatedoc; | ||
@@ -371,10 +391,26 @@ options.updatedoc._id = originalId; | ||
let Model = options.model || this.model; | ||
Promisie.parallel({ | ||
update: Promisie.promisify(Model.update, Model)({ _id: options.id, }, updateOperation), | ||
changes: Promisie.promisify(generateChanges)(), | ||
Promise.resolve(options.originalrevision) | ||
.then(originalDoc => { | ||
if (originalDoc) { | ||
return changesetData.original; | ||
} else if(options.track_changes) { | ||
return this.load({ docid: options.id, }); | ||
} else { | ||
return {}; | ||
} | ||
}) | ||
.then(result => { | ||
if (options.ensure_changes) cb(null, result); | ||
else cb(null, result.update); | ||
}, cb); | ||
.then(originalDoc => { | ||
changesetData.original = (typeof originalDoc.toObject === 'function') | ||
? originalDoc.toObject() | ||
: originalDoc; | ||
Promisie.parallel({ | ||
update: Promisie.promisify(Model.update, Model)({ _id: options.id, }, updateOperation), | ||
changes: Promisie.promisify(generateChanges)(), | ||
}) | ||
.then(result => { | ||
if (options.ensure_changes) cb(null, result); | ||
else cb(null, result.update); | ||
}, cb); | ||
}) | ||
.catch(cb); | ||
} catch (e) { | ||
@@ -451,4 +487,4 @@ cb(e); | ||
Promisie.map(newdoc, (doc) => { | ||
return Promisie.promisify(Model.create, Model)(utility.enforceXSSRules(doc, xss_whitelist, options)); | ||
}) | ||
return Promisie.promisify(Model.create, Model)(utility.enforceXSSRules(doc, xss_whitelist, options)); | ||
}) | ||
.then(created => cb(null, created)) | ||
@@ -526,24 +562,27 @@ .catch(cb); | ||
constructor(options = {}) { | ||
this.db_connection = options.db_connection || mongoose; | ||
this.docid = options.docid; //previously docnamelookup | ||
this.model = (typeof options.model === 'string') ? this.db_connection.model(options.model) : options.model; | ||
this.sort = options.sort || '-createdat'; | ||
this.limit = options.limit || 500; | ||
this.skip = options.skip || 0; | ||
if (Array.isArray(options.search)) this.searchfields = options.search; | ||
else if (typeof options.search === 'string') this.searchfields = options.search.split(','); | ||
else this.searchfields = []; | ||
if(Array.isArray(options.plugins)) { | ||
options.plugins.forEach(plugin => {this.model.schema.plugin(plugin.func, plugin.options)}); | ||
}; | ||
this.population = options.population; | ||
this.fields = options.fields; | ||
this.pagelength = options.pagelength || 15; | ||
this.cache = options.cache; | ||
this.track_changes = (options.track_changes === false) ? false : true; | ||
this.changeset = (options.db_connection) ? require(path.join(__dirname, '../changeset/index')).mongo(this.db_connection) : require(path.join(__dirname, '../changeset/index')).mongo_default; | ||
this.xss_whitelist = options.xss_whitelist || xss_default_whitelist; | ||
this._useCache = (options.useCache && options.cache) ? true : false; | ||
this.db_connection = options.db_connection || mongoose; | ||
this.docid = options.docid; //previously docnamelookup | ||
this.model = (typeof options.model === 'string') ? this.db_connection.model(options.model) : options.model; | ||
this.sort = options.sort || '-createdat'; | ||
this.limit = options.limit || 500; | ||
this.skip = options.skip || 0; | ||
if (Array.isArray(options.search)) this.searchfields = options.search; | ||
else if (typeof options.search === 'string') this.searchfields = options.search.split(','); | ||
else this.searchfields = []; | ||
if(Array.isArray(options.plugins)) { | ||
options.plugins.forEach(plugin => { | ||
this.model.schema.plugin(plugin.func, plugin.options) | ||
; | ||
}); | ||
} | ||
/** | ||
this.population = options.population; | ||
this.fields = options.fields; | ||
this.pagelength = options.pagelength || 15; | ||
this.cache = options.cache; | ||
this.track_changes = (options.track_changes === false) ? false : true; | ||
this.changeset = (options.db_connection) ? require(path.join(__dirname, '../changeset/index')).mongo(this.db_connection) : require(path.join(__dirname, '../changeset/index')).mongo_default; | ||
this.xss_whitelist = options.xss_whitelist || xss_default_whitelist; | ||
this._useCache = (options.useCache && options.cache) ? true : false; | ||
} | ||
/** | ||
* Query method for adapter see _QUERY and _QUERY_WITH_PAGINATION for more details | ||
@@ -556,7 +595,7 @@ * @param {Object} [options={}] Configurable options for query | ||
query(options = {}, cb = false) { | ||
let _query = (options && options.paginate) ? _QUERY_WITH_PAGINATION.bind(this) : _QUERY.bind(this); | ||
if (typeof cb === 'function') _query(options, cb); | ||
else return Promisie.promisify(_query)(options); | ||
} | ||
/** | ||
let _query = (options && options.paginate) ? _QUERY_WITH_PAGINATION.bind(this) : _QUERY.bind(this); | ||
if (typeof cb === 'function') _query(options, cb); | ||
else return Promisie.promisify(_query)(options); | ||
} | ||
/** | ||
* Search method for adapter see _SEARCH for more details | ||
@@ -568,7 +607,7 @@ * @param {Object} [options={}] Configurable options for query | ||
search(options = {}, cb = false) { | ||
let _search = _SEARCH.bind(this); | ||
if (typeof cb === 'function') _search(options, cb); | ||
else return Promisie.promisify(_search)(options); | ||
} | ||
/** | ||
let _search = _SEARCH.bind(this); | ||
if (typeof cb === 'function') _search(options, cb); | ||
else return Promisie.promisify(_search)(options); | ||
} | ||
/** | ||
* Stream method for adapter see _STREAM for more details | ||
@@ -580,7 +619,7 @@ * @param {Object} [options={}] Configurable options for stream | ||
stream(options = {}, cb = false) { | ||
let _stream = _STREAM.bind(this); | ||
if (typeof cb === 'function') _stream(options, cb); | ||
else return Promisie.promisify(_stream)(options); | ||
} | ||
/** | ||
let _stream = _STREAM.bind(this); | ||
if (typeof cb === 'function') _stream(options, cb); | ||
else return Promisie.promisify(_stream)(options); | ||
} | ||
/** | ||
* Load method for adapter see _LOAD for more details | ||
@@ -592,7 +631,7 @@ * @param {Object} [options={}] Configurable options for load | ||
load(options = {}, cb = false) { | ||
let _load = _LOAD.bind(this); | ||
if (typeof cb === 'function') _load(options, cb); | ||
else return Promisie.promisify(_load)(options); | ||
} | ||
/** | ||
let _load = _LOAD.bind(this); | ||
if (typeof cb === 'function') _load(options, cb); | ||
else return Promisie.promisify(_load)(options); | ||
} | ||
/** | ||
* Update method for adapter see _UPDATE, _UPDATED and _UPDATE_ALL for more details | ||
@@ -606,7 +645,7 @@ * @param {Object} [options={}] Configurable options for update | ||
update(options = {}, cb = false) { | ||
let _update = (options.multi) ? _UPDATE_ALL.bind(this) : ((options.return_updated) ? _UPDATED.bind(this) : _UPDATE.bind(this)); | ||
if (typeof cb === 'function') _update(options, cb); | ||
else return Promisie.promisify(_update)(options); | ||
} | ||
/** | ||
let _update = (options.multi) ? _UPDATE_ALL.bind(this) : ((options.return_updated) ? _UPDATED.bind(this) : _UPDATE.bind(this)); | ||
if (typeof cb === 'function') _update(options, cb); | ||
else return Promisie.promisify(_update)(options); | ||
} | ||
/** | ||
* Create method for adapter see _CREATE for more details | ||
@@ -618,7 +657,7 @@ * @param {Object} [options={}] Configurable options for create | ||
create(options = {}, cb = false) { | ||
let _create = _CREATE.bind(this); | ||
if (typeof cb === 'function') _create(options, cb); | ||
else return Promisie.promisify(_create)(options); | ||
} | ||
/** | ||
let _create = _CREATE.bind(this); | ||
if (typeof cb === 'function') _create(options, cb); | ||
else return Promisie.promisify(_create)(options); | ||
} | ||
/** | ||
* Delete method for adapter see _DELETE and _DELETED for more details | ||
@@ -625,0 +664,0 @@ * @param {Object} [options={}] Configurable options for create |
@@ -462,2 +462,3 @@ 'use strict'; | ||
let xss_whitelist = (options.xss_whitelist) ? options.xss_whitelist : this.xss_whitelist; | ||
options.updatedoc.updatedat = new Date(); | ||
options.updatedoc = utility.enforceXSSRules(options.updatedoc, xss_whitelist, options); | ||
@@ -485,16 +486,34 @@ let Model = options.model || this.model; | ||
// console.log({docid},'this.docid',this.docid,'where',JSON.stringify(where, null, 2)); | ||
Promisie.parallel({ | ||
update: () => Model.update(options.updatedoc, (options.query && typeof options.query === 'object') ? { | ||
limit: 1, | ||
where: options.query, | ||
} : { | ||
where, | ||
limit: 1, | ||
}), | ||
changes: () => Promisie.promisify(generateChanges)(), | ||
}) | ||
.then(result => { | ||
if (options.ensure_changes) cb(null, result); | ||
else cb(null, result.update); | ||
}, cb); | ||
Promise.resolve(options.originalrevision) | ||
.then(originalDoc => { | ||
if (originalDoc) { | ||
return changesetData.original; | ||
} else if (options.track_changes) { | ||
return this.load({ docid: options.id, }); | ||
} else { | ||
return {}; | ||
} | ||
}) | ||
.then(originalDoc => { | ||
changesetData.original = (typeof originalDoc.toObject === 'function') | ||
? originalDoc.toObject() | ||
: originalDoc; | ||
Promisie.parallel({ | ||
update: () => Model.update(options.updatedoc, (options.query && typeof options.query === 'object') ? { | ||
limit: 1, | ||
where: options.query, | ||
} : { | ||
where, | ||
limit: 1, | ||
}), | ||
changes: () => Promisie.promisify(generateChanges)(), | ||
}) | ||
.then(result => { | ||
if (options.ensure_changes) cb(null, result); | ||
else cb(null, result.update); | ||
}, cb); | ||
}) | ||
.catch(cb); | ||
} catch (e) { | ||
@@ -501,0 +520,0 @@ cb(e); |
@@ -97,4 +97,4 @@ { | ||
}, | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"license": "MIT" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1865872
5553