periodicjs.core.data
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -48,5 +48,8 @@ 'use strict'; | ||
}; | ||
if (Object.keys(queryOptions.where).length === 0) delete queryOptions.where; | ||
if (fields) queryOptions.attributes = GENERATE_SELECT(fields); | ||
if (sort) queryOptions.order = sort; | ||
if (sort) queryOptions.order = (Array.isArray(sort) === false) ? | ||
convertSortObjToOrderArray(sort) : | ||
sort; | ||
if (skip) queryOptions.offset = skip; | ||
@@ -67,8 +70,41 @@ if (limit) queryOptions.limit = limit; | ||
/** | ||
* converts mongo-like sort property to sequelize order value | ||
* | ||
* @param {any} sortVal | ||
* @returns {String} [ASC|DESC] | ||
*/ | ||
function getOrderFromSortObj(sortVal) { | ||
if (sortVal >= 0) return 'ASC'; | ||
else if (sortVal < 0) return 'DESC'; | ||
else return 'ASC'; | ||
} | ||
/** | ||
* this converts a mongo like sort object to a sequelize order argument. { date:1, title:1} => [ ['date','ASC'], ['title','ASC'] ] | ||
* | ||
* @param {Object} sort mongo like sort argument | ||
* @returns {Array} order argument | ||
*/ | ||
function convertSortObjToOrderArray(sort) { | ||
return Object.keys(sort).map(key => [key, getOrderFromSortObj(sort[key]), ]); | ||
} | ||
/** | ||
* returns plain json object instead of sequelize row instance | ||
* | ||
* @param {Object} result | ||
* @returns {Object} | ||
*/ | ||
function getPlainResult(result) { | ||
return (result && typeof result.get === 'function') ? | ||
result.get({ plain: true }) : | ||
result.get({ plain: true, }) : | ||
result; | ||
} | ||
/** | ||
* returns rows of sequelize instances to plain objects | ||
* | ||
* @param {any} results | ||
* @returns | ||
*/ | ||
function getJSONResults(results) { | ||
@@ -151,11 +187,11 @@ return (results && results.length) ? | ||
Promisie.doWhilst(() => { | ||
return new Promisie((resolve, reject) => { | ||
_QUERY.call(this, { 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++] = { | ||
return new Promisie((resolve, reject) => { | ||
_QUERY.call(this, { 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: (this.jsonify_results) ? | ||
@@ -165,7 +201,7 @@ getJSONResults(data) : data, | ||
}; | ||
resolve(data.length); | ||
} | ||
}); | ||
resolve(data.length); | ||
} | ||
}); | ||
}, current => (current === pagelength && total < limit)) | ||
}); | ||
}, current => (current === pagelength && total < limit)) | ||
.then(() => cb(null, pages)) | ||
@@ -318,11 +354,11 @@ .catch(cb); | ||
.then(() => { | ||
return Promisie.map(Object.keys(changeset), (key) => { | ||
return this.changeset.create({ | ||
parent_document_id: options.id, | ||
field_name: key, | ||
original: (changeset[key].length > 1) ? changeset[key][0] : 'new value', | ||
update: (changeset[key].length < 2) ? changeset[0] : ((changeset[key].length === 2) ? changeset[key][1] : 'deleted value'), | ||
}); | ||
return Promisie.map(Object.keys(changeset), (key) => { | ||
return this.changeset.create({ | ||
parent_document_id: options.id, | ||
field_name: key, | ||
original: (changeset[key].length > 1) ? changeset[key][0] : 'new value', | ||
update: (changeset[key].length < 2) ? changeset[0] : ((changeset[key].length === 2) ? changeset[key][1] : 'deleted value'), | ||
}); | ||
}) | ||
}); | ||
}) | ||
.then(result => { | ||
@@ -346,11 +382,11 @@ if (options.ensure_changes) callback(null, (this.jsonify_results) ? | ||
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)(), | ||
}) | ||
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 => { | ||
@@ -460,10 +496,10 @@ if (options.ensure_changes) cb(null, result); | ||
Model.destroy({ | ||
where: [{ | ||
id: deleteid, | ||
}, { | ||
[options.docid || this.docid]: deleteid, | ||
}, ], | ||
force: options.force, | ||
limit: 1, | ||
}) | ||
where: [{ | ||
id: deleteid, | ||
}, { | ||
[options.docid || this.docid]: deleteid, | ||
}, ], | ||
force: options.force, | ||
limit: 1, | ||
}) | ||
.then(result => cb(null, result)) | ||
@@ -552,34 +588,34 @@ .catch(cb); | ||
constructor(options = {}) { | ||
if (options.db_connection && typeof options.db_connection === 'object') { | ||
if (options.db_connection.models && options.db_connection.define) this.db_connection = options.db_connection; | ||
else if (Array.isArray(options.db_connection)) { | ||
let connectionOptions = options.db_connection; | ||
this.db_connection = new Sequelize(...connectionOptions); | ||
} else if (options.db_connection.db_name && options.db_connection.db_user && options.db_connection.db_password) { | ||
let { db_name, db_user, db_password, db_options, } = options.db_connection; | ||
this.db_connection = new Sequelize(db_name, db_user, db_password, db_options); | ||
} | ||
if (options.db_connection && typeof options.db_connection === 'object') { | ||
if (options.db_connection.models && options.db_connection.define) this.db_connection = options.db_connection; | ||
else if (Array.isArray(options.db_connection)) { | ||
let connectionOptions = options.db_connection; | ||
this.db_connection = new Sequelize(...connectionOptions); | ||
} else if (options.db_connection.db_name && options.db_connection.db_user && options.db_connection.db_password) { | ||
let { db_name, db_user, db_password, db_options, } = options.db_connection; | ||
this.db_connection = new Sequelize(db_name, db_user, db_password, db_options); | ||
} | ||
this.db_connection = (options.db_connection && typeof options.db_connection === 'object' && options.db_connection.models && options.db_connection.define) ? options.db_connection : new Sequelize(options.db_connection); | ||
this.docid = options.docid || 'id'; | ||
this.jsonify_results = (typeof options.jsonify_results === 'boolean') ? options.jsonify_results : true; | ||
if (options.model && typeof options.model === 'object') { | ||
if (Array.isArray(options.model)) this.model = this.db_connection.define(...options.model); | ||
else this.model = options.model; | ||
} else this.model = this.db_connection.models[options.model]; | ||
this.sort = options.sort || 'createdat DESC'; | ||
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 = []; | ||
this.population = options.population || []; | ||
this.fields = options.fields; | ||
this.pagelength = options.pagelength || 15; | ||
this.cache = options.cache; | ||
this.changeset = (options.db_connection) ? require(path.join(__dirname, '../changeset/index')).sql(this.db_connection) : false; | ||
this.track_changes = (options.track_changes === false || this.changeset === false) ? false : true; | ||
this.xss_whitelist = options.xss_whitelist || xss_default_whitelist; | ||
this._useCache = (options.useCache && options.cache) ? true : false; | ||
} | ||
this.db_connection = (options.db_connection && typeof options.db_connection === 'object' && options.db_connection.models && options.db_connection.define) ? options.db_connection : new Sequelize(options.db_connection); | ||
this.docid = options.docid || 'id'; | ||
this.jsonify_results = (typeof options.jsonify_results === 'boolean') ? options.jsonify_results : true; | ||
if (options.model && typeof options.model === 'object') { | ||
if (Array.isArray(options.model)) this.model = this.db_connection.define(...options.model); | ||
else this.model = options.model; | ||
} else this.model = this.db_connection.models[options.model]; | ||
this.sort = options.sort || 'createdat DESC'; | ||
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 = []; | ||
this.population = options.population || []; | ||
this.fields = options.fields; | ||
this.pagelength = options.pagelength || 15; | ||
this.cache = options.cache; | ||
this.changeset = (options.db_connection) ? require(path.join(__dirname, '../changeset/index')).sql(this.db_connection) : false; | ||
this.track_changes = (options.track_changes === false || this.changeset === false) ? false : true; | ||
this.xss_whitelist = options.xss_whitelist || xss_default_whitelist; | ||
this._useCache = (options.useCache && options.cache) ? true : false; | ||
} | ||
/** | ||
@@ -592,6 +628,6 @@ * Sync defined sequelize models with SQL db | ||
sync(options = {}, cb = false) { | ||
if (typeof options === 'function') cb = options; | ||
let _sync = function(callback) { | ||
try { | ||
this.db_connection.sync(options) | ||
if (typeof options === 'function') cb = options; | ||
let _sync = function(callback) { | ||
try { | ||
this.db_connection.sync(options) | ||
.then(connection => connection.authenticate()) | ||
@@ -608,9 +644,9 @@ .then(() => { | ||
.catch(callback); | ||
} catch (e) { | ||
callback(e); | ||
} | ||
}.bind(this); | ||
if (typeof cb === 'function') _sync(cb); | ||
else return Promisie.promisify(_sync)(); | ||
} | ||
} catch (e) { | ||
callback(e); | ||
} | ||
}.bind(this); | ||
if (typeof cb === 'function') _sync(cb); | ||
else return Promisie.promisify(_sync)(); | ||
} | ||
/** | ||
@@ -624,6 +660,6 @@ * Query method for adapter see _QUERY and _QUERY_WITH_PAGINATION for more details | ||
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); | ||
} | ||
/** | ||
@@ -636,6 +672,6 @@ * Search method for adapter see _SEARCH for more details | ||
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); | ||
} | ||
/** | ||
@@ -648,6 +684,6 @@ * Stream method for adapter see _STREAM for more details | ||
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); | ||
} | ||
/** | ||
@@ -660,6 +696,6 @@ * Load method for adapter see _LOAD for more details | ||
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); | ||
} | ||
/** | ||
@@ -674,6 +710,6 @@ * Update method for adapter see _UPDATE, _UPDATED and _UPDATE_ALL for more details | ||
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); | ||
} | ||
/** | ||
@@ -686,6 +722,6 @@ * Create method for adapter see _CREATE for more details | ||
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); | ||
} | ||
/** | ||
@@ -699,6 +735,6 @@ * Delete method for adapter see _DELETE and _DELETED for more details | ||
delete(options = {}, cb = false) { | ||
let _delete = (options.return_deleted) ? _DELETED.bind(this) : _DELETE.bind(this); | ||
if (typeof cb === 'function') _delete(options, cb); | ||
else return Promisie.promisify(_delete)(options); | ||
} | ||
let _delete = (options.return_deleted) ? _DELETED.bind(this) : _DELETE.bind(this); | ||
if (typeof cb === 'function') _delete(options, cb); | ||
else return Promisie.promisify(_delete)(options); | ||
} | ||
/** | ||
@@ -705,0 +741,0 @@ * Raw query method for adapter see _RAW for more details |
@@ -97,4 +97,4 @@ { | ||
}, | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"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
1849891
5125