Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

periodicjs.core.data

Package Overview
Dependencies
Maintainers
2
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

periodicjs.core.data - npm Package Compare versions

Comparing version 0.2.61 to 0.3.0

477

adapters/sql.js

@@ -13,3 +13,3 @@ 'use strict';

*/
const GENERATE_SELECT = function (fields) {
const GENERATE_SELECT = function(fields) {
if (typeof fields === 'string') return fields.split(',');

@@ -20,3 +20,3 @@ if (Array.isArray(fields)) return fields;

if (typeof fields[key] !== 'string') result.push(key);
else result.push([key, fields[key],]);
else result.push([key, fields[key], ]);
}

@@ -39,7 +39,7 @@ return result;

*/
const _QUERY = function (options, cb) {
const _QUERY = function(options, cb) {
try {
let Model = options.model || this.model;
//Iteratively checks if value was passed in options argument and conditionally assigns the default value if not passed in options
let { sort, limit, population, fields, skip, } = ['sort', 'limit', 'population', 'fields', 'skip',].reduce((result, key) => {
let { sort, limit, population, fields, skip, } = ['sort', 'limit', 'population', 'fields', 'skip', ].reduce((result, key) => {
result[key] = options[key] || this[key];

@@ -60,6 +60,7 @@ return result;

}
// queryOptions.raw = true;
Model.findAll(queryOptions)
.then(result => cb(null, result))
.then(results => cb(null, (this.jsonify_results) ? getJSONResults(results) : results))
.catch(cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -69,2 +70,13 @@ }

function getPlainResult(result) {
return (typeof result.get === 'function') ?
result.get({ plain: true }) :
result;
}
function getJSONResults(results) {
return (results && results.length) ?
results.map(getPlainResult) :
results;
}
/**

@@ -82,3 +94,3 @@ * Convenience method for returning a stream of sql data. Since sequelize does not expose a cursor or stream method this is an implementation of a cursor on top of a normal SQL query

*/
const _STREAM = function (options, cb) {
const _STREAM = function(options, cb) {
try {

@@ -93,4 +105,12 @@ _QUERY.call(this, options, (err, documents) => {

let task = setImmediate(() => {
if (i === documents.length - 1) querystream.end(documents[i]);
else querystream.write(documents[i]);
if (i === documents.length - 1) querystream.end(
(this.jsonify_results) ?
getPlainResult(documents[i]) :
documents[i]
);
else querystream.write(
(this.jsonify_results) ?
getPlainResult(documents[i]) :
documents[i]
);
clearImmediate(task);

@@ -102,3 +122,3 @@ });

});
} catch (e) {
} catch (e) {
cb(e);

@@ -121,7 +141,7 @@ }

*/
const _QUERY_WITH_PAGINATION = function (options, cb) {
const _QUERY_WITH_PAGINATION = function(options, cb) {
try {
let Model = options.model || this.model;
//Iteratively checks if value was passed in options argument and conditionally assigns the default value if not passed in options
let { sort, limit, population, fields, skip, pagelength, } = ['sort', 'limit', 'population', 'fields', 'skip', 'pagelength',].reduce((result, key) => {
let { sort, limit, population, fields, skip, pagelength, } = ['sort', 'limit', 'population', 'fields', 'skip', 'pagelength', ].reduce((result, key) => {
result[key] = options[key] || this[key];

@@ -138,22 +158,24 @@ return result;

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++] = {
documents: data,
count: data.length,
};
resolve(data.length);
}
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) ?
getJSONResults(data) :
data,
count: data.length,
};
resolve(data.length);
}
});
});
});
}, current => (current === pagelength && total < limit))
}, current => (current === pagelength && total < limit))
.then(() => cb(null, pages))
.catch(cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -181,3 +203,3 @@ }

*/
const _SEARCH = function (options, cb) {
const _SEARCH = function(options, cb) {
try {

@@ -190,3 +212,5 @@ let query;

let toplevel = (options.inclusive) ? '$or' : '$and';
query = { [toplevel]: [], };
query = {
[toplevel]: [],
};
//Pushes options.query if it already a composed query object

@@ -203,3 +227,5 @@ if (options.query && typeof options.query === 'object') query[toplevel].push(options.query);

for (let i = 0; i < searchfields.length; i++) {
block.$or.push({ [searchfields[i]]: value, });
block.$or.push({
[searchfields[i]]: value,
});
}

@@ -215,3 +241,5 @@ return result.concat(block);

if (isObjectIds) query[toplevel].push({ 'id': { $in: split, }, });
else query[toplevel].push({ [(options.docid || this.docid) ? (options.docid || this.docid) : 'id']: { $in: split, }, });
else query[toplevel].push({
[(options.docid || this.docid) ? (options.docid || this.docid) : 'id']: { $in: split, },
});
}

@@ -221,3 +249,3 @@ options.query = query;

else _QUERY.call(this, options, cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -238,7 +266,7 @@ }

*/
const _LOAD = function (options, cb) {
const _LOAD = function(options, cb) {
try {
let Model = options.model || this.model;
//Iteratively checks if value was passed in options argument and conditionally assigns the default value if not passed in options
let { sort, population, fields, docid, } = ['sort', 'population', 'fields', 'docid',].reduce((result, key) => {
let { sort, population, fields, docid, } = ['sort', 'population', 'fields', 'docid', ].reduce((result, key) => {
result[key] = options[key] || this[key];

@@ -250,3 +278,3 @@ return result;

[docid || 'id']: options.query,
},],
}, ],
};

@@ -263,5 +291,7 @@ let queryOptions = {

Model.findOne(queryOptions)
.then(result => cb(null, result))
.then(result => cb(null, (this.jsonify_results) ?
getPlainResult(result) :
result))
.catch(cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -284,3 +314,3 @@ }

*/
const _UPDATE = function (options, cb) {
const _UPDATE = function(options, cb) {
try {

@@ -303,3 +333,3 @@ options.track_changes = (typeof options.track_changes === 'boolean') ? options.track_changes : this.track_changes;

})()
.then(() => {
.then(() => {
return Promisie.map(Object.keys(changeset), (key) => {

@@ -315,3 +345,5 @@ return this.changeset.create({

.then(result => {
if (options.ensure_changes) callback(null, result);
if (options.ensure_changes) callback(null, (this.jsonify_results) ?
getPlainResult(result) :
result);
}, e => {

@@ -328,14 +360,14 @@ if (options.ensure_changes) callback(e);

[options.docid || this.docid]: options.id,
},],
}, ],
};
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 => {

@@ -345,3 +377,3 @@ if (options.ensure_changes) cb(null, result);

}, cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -364,3 +396,3 @@ }

*/
const _UPDATED = function (options, cb) {
const _UPDATED = function(options, cb) {
try {

@@ -372,3 +404,3 @@ if (!options.id) throw new Error('Can\'t retrieve document after update if options.id is not defined');

});
} catch (e) {
} catch (e) {
cb(e);

@@ -388,3 +420,3 @@ }

*/
const _UPDATE_ALL = function (options, cb) {
const _UPDATE_ALL = function(options, cb) {
try {

@@ -398,3 +430,3 @@ let Model = options.model || this.model;

.catch(cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -415,3 +447,3 @@ }

*/
const _CREATE = function (options, cb) {
const _CREATE = function(options, cb) {
try {

@@ -426,3 +458,3 @@ let Model = options.model || this.model;

.catch(cb);
} else {
} else {
Model.create(utility.enforceXSSRules(newdoc, xss_whitelist, (options.newdoc) ? options : undefined))

@@ -432,3 +464,3 @@ .then(result => cb(null, result))

}
} catch (e) {
} catch (e) {
cb(e);

@@ -447,3 +479,3 @@ }

*/
const _DELETE = function (options, cb) {
const _DELETE = function(options, cb) {
try {

@@ -454,13 +486,13 @@ let Model = options.model || this.model;

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))
.catch(cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -478,3 +510,3 @@ }

*/
const _DELETED = function (options, cb) {
const _DELETED = function(options, cb) {
try {

@@ -490,3 +522,3 @@ _LOAD.call(this, { model: options.model, query: options.deleteid || options.id, }, (err1, loaded) => {

});
} catch (e) {
} catch (e) {
cb(e);

@@ -506,3 +538,3 @@ }

*/
const _RAW = function (options, cb) {
const _RAW = function(options, cb) {
try {

@@ -519,3 +551,3 @@ let Model = options.model || this.model;

.catch(cb);
} catch (e) {
} catch (e) {
cb(e);

@@ -549,151 +581,152 @@ }

*/
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);
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);
}
}
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) ? 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';
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;
}
/**
* Sync defined sequelize models with SQL db
* @param {Object} [options={}] Configurable options for sequelize sync method
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
sync (options = {}, cb = false) {
if (typeof options === 'function') cb = options;
let _sync = function (callback) {
try {
this.db_connection.sync(options)
.then(connection => connection.authenticate())
.then(() => {
if (this.changeset && !this.changeset[IS_SYNCED]) {
Object.defineProperty(this.changeset, IS_SYNCED, {
value: true,
enumerable: false,
});
}
callback(null, { status: 'ok', });
})
.catch(callback);
} catch (e) {
callback(e);
}
}.bind(this);
if (typeof cb === 'function') _sync(cb);
else return Promisie.promisify(_sync)();
}
/**
* Query method for adapter see _QUERY and _QUERY_WITH_PAGINATION for more details
* @param {Object} [options={}] Configurable options for query
* @param {Boolean} options.paginate When true query will return data in a paginated form
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
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);
}
/**
* Search method for adapter see _SEARCH for more details
* @param {Object} [options={}] Configurable options for query
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
search (options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for stream
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
stream (options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for load
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
load (options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for update
* @param {Boolean} options.return_updated If true update method will return the updated document instead of an update status message
* @param {Boolean} options.multi If true a multiple document update will be perfomed
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
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);
}
/**
* Create method for adapter see _CREATE for more details
* @param {Object} [options={}] Configurable options for create
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
create (options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for create
* @param {Boolean} options.return_deleted If true delete method will return the deleted document
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
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);
}
/**
* Raw query method for adapter see _RAW for more details
* @param {Object} options Configurable options for raw query
* @param {Function} cb Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
raw (options = {}, cb = false) {
/**
* Sync defined sequelize models with SQL db
* @param {Object} [options={}] Configurable options for sequelize sync method
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
sync(options = {}, cb = false) {
if (typeof options === 'function') cb = options;
let _sync = function(callback) {
try {
this.db_connection.sync(options)
.then(connection => connection.authenticate())
.then(() => {
if (this.changeset && !this.changeset[IS_SYNCED]) {
Object.defineProperty(this.changeset, IS_SYNCED, {
value: true,
enumerable: false,
});
}
callback(null, { status: 'ok', });
})
.catch(callback);
} catch (e) {
callback(e);
}
}.bind(this);
if (typeof cb === 'function') _sync(cb);
else return Promisie.promisify(_sync)();
}
/**
* Query method for adapter see _QUERY and _QUERY_WITH_PAGINATION for more details
* @param {Object} [options={}] Configurable options for query
* @param {Boolean} options.paginate When true query will return data in a paginated form
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
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);
}
/**
* Search method for adapter see _SEARCH for more details
* @param {Object} [options={}] Configurable options for query
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
search(options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for stream
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
stream(options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for load
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
load(options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for update
* @param {Boolean} options.return_updated If true update method will return the updated document instead of an update status message
* @param {Boolean} options.multi If true a multiple document update will be perfomed
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
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);
}
/**
* Create method for adapter see _CREATE for more details
* @param {Object} [options={}] Configurable options for create
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
create(options = {}, cb = false) {
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
* @param {Object} [options={}] Configurable options for create
* @param {Boolean} options.return_deleted If true delete method will return the deleted document
* @param {Function} [cb=false] Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
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);
}
/**
* Raw query method for adapter see _RAW for more details
* @param {Object} options Configurable options for raw query
* @param {Function} cb Callback argument. When cb is not passed function returns a Promise
* @return {Object} Returns a Promise when cb argument is not passed
*/
raw(options = {}, cb = false) {
let _raw = _RAW.bind(this);

@@ -705,2 +738,2 @@ if (typeof cb === 'function') _raw(options, cb);

module.exports = SQL_ADAPTER;
module.exports = SQL_ADAPTER;

@@ -50,2 +50,3 @@ {

"mongoose": "~4.4.14",
"pg": "^6.2.2",
"promisie": "^1.6.1",

@@ -97,4 +98,4 @@ "sequelize": "~3.28.0"

},
"version": "0.2.61",
"version": "0.3.0",
"license": "MIT"
}

@@ -23,2 +23,13 @@ 'use strict';

}];
/**
"database": "travis_ci_test",
"username": "",
"password": "",
"connection_options": {
"dialect": "postgres",
"port": 5432,
"host": "127.0.0.1",
"logging": false
}
*/
var travisConnectionOptions = ['test_core_data', 'travis', '', {

@@ -589,2 +600,2 @@ dialect: 'mysql',

});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc