node-querybuilder
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -33,17 +33,41 @@ // Load Tedious connection pool library | ||
this._pool.acquire((err, connection) => { | ||
if (err) throw err; | ||
const adapter = new Single(this._original_settings, { | ||
pool: { | ||
pool: this._pool, | ||
connection: connection, | ||
} | ||
const self = this; | ||
const handler = (reject, resolve) => { | ||
self._pool.acquire((err, connection) => { | ||
if (err) throw err; | ||
const adapter = new Single(self._original_settings, { | ||
pool: { | ||
pool: self._pool, | ||
connection, | ||
} | ||
}); | ||
if ((!cb || typeof cb !== 'function') && (typeof resolve === 'function' && typeof reject === 'function')) return resolve(adapter) | ||
else if (cb && typeof cb === 'function') return cb(adapter); | ||
throw ERRORS.NO_VALID_RESULTS_HANDLER; | ||
}); | ||
} | ||
cb(adapter); | ||
}); | ||
if (!cb || (cb && typeof cb !== 'function')) { | ||
return new Promise(handler); | ||
} else { | ||
handler(); | ||
} | ||
} | ||
disconnect(cb) { | ||
this._pool.drain(cb); | ||
if (!cb || (cb && typeof cb !== 'function')) { | ||
return new Promise((resolve, reject) => { | ||
this._pool.drain((err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} else { | ||
this._pool.drain(cb); | ||
} | ||
} | ||
@@ -50,0 +74,0 @@ } |
@@ -38,4 +38,14 @@ // Load Tedious connection library | ||
connect(cb) { | ||
if (this._connection) return this._connection; | ||
this._connection = this.sql_connection.connect(cb); | ||
if (!cb || (cb && typeof cb !== 'function')) { | ||
return new Promise((resolve, reject) => { | ||
if (this._connection) return resolve(); | ||
this._connection = this.sql_connection.connect((err) => { | ||
if (err) return reject(err); | ||
resolve(); | ||
}); | ||
}); | ||
} else { | ||
if (this._connection) return cb(); | ||
this._connection = this.sql_connection.connect(cb); | ||
} | ||
} | ||
@@ -51,3 +61,3 @@ | ||
disconnect(callback) { | ||
disconnect(cb) { | ||
if (this.pool) { | ||
@@ -58,3 +68,10 @@ this.pool.drain(); | ||
} | ||
if (callback && typeof callback === 'function') callback(null); | ||
if (cb && typeof cb === 'function') { | ||
cb(null); | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
resolve(); | ||
}); | ||
} | ||
} | ||
@@ -61,0 +78,0 @@ |
const Request = require('tedious').Request; | ||
const QueryBuilder = require('./query_builder.js'); | ||
const ERROR = require("../QueryExecError"); | ||
const WrapperPromise = require("../WrapperPromise"); | ||
@@ -54,2 +56,3 @@ // **************************************************************************** | ||
query(sql, cb) { | ||
if (!cb || typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
@@ -66,11 +69,22 @@ } | ||
this.reset_query(sql); | ||
this._exec(sql, (err, row) => { | ||
const handler = (err, row) => { | ||
if (!err) { | ||
//console.dir(row[0].numrows); | ||
if (typeof callback !== "function") { | ||
this.resolve(row[0].numrows); | ||
return; | ||
} | ||
cb(err, row[0].numrows); | ||
} | ||
else { | ||
if (typeof callback !== "function") { | ||
this.reject(err); | ||
return; | ||
} | ||
cb(err, row); | ||
} | ||
}); | ||
} | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this), handler).promisify(); | ||
this._exec(sql, handler); | ||
} | ||
@@ -83,9 +97,8 @@ | ||
} | ||
else if (typeof table === 'undefined' && typeof cb !== 'function') { | ||
throw new Error("No cb function has been provided in your 'get' call!"); | ||
} | ||
const sql = this._get(table); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -95,10 +108,12 @@ | ||
if (typeof table !== 'string' && !Array.isArray(table)) { | ||
throw new Error("First parameter of get_where() must be a string or an array of strings."); | ||
throw ERROR.FIRST_PARAM_OF_GET_WHERE_ERR; | ||
} | ||
if (Object.prototype.toString.call(where) !== Object.prototype.toString.call({})) { | ||
throw new Error("Second parameter of get_where() must be an object with key:value pairs."); | ||
throw ERROR.SECOND_PARAM_OF_GET_WHERE_ERR; | ||
} | ||
const sql = this._get_where(table,where); | ||
const sql = this._get_where(table, where); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -109,3 +124,5 @@ | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -139,2 +156,4 @@ | ||
this.reset_query(sql); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
@@ -149,6 +168,3 @@ } | ||
} | ||
else if (typeof where === 'undefined' && typeof cb !== 'function') { | ||
throw new Error("No cb function has been provided in your update call!"); | ||
} | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && where.length == 0)) { | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && Object.keys(where).length === 0)) { | ||
where = null; | ||
@@ -159,3 +175,5 @@ } | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -170,10 +188,7 @@ | ||
} | ||
else if (typeof where === 'undefined' && typeof cb !== 'function') { | ||
throw new Error("No cb function has been provided in your update_batch call!"); | ||
} | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && where.length == 0)) { | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && Object.keys(where).length === 0)) { | ||
where = null; | ||
} | ||
const sqls = this._update_batch(table,set,index,where); | ||
const sqls = this._update_batch(table, set, index, where); | ||
const results = null; | ||
@@ -183,25 +198,33 @@ const errors = []; | ||
// Execute each batch of (at least) 100 | ||
(function next_batch() { | ||
const sql = sqls.shift(); | ||
this.reset_query(sql); | ||
const handler = (resolve, reject) => { | ||
(function next_batch() { | ||
const sql = sqls.shift(); | ||
this.reset_query(sql); | ||
this._exec(sql, (err, res) => { | ||
if (!err) { | ||
if (null === results) { | ||
results = res; | ||
this._exec(sql, (err, res) => { | ||
if (!err) { | ||
if (null === results) { | ||
results = res; | ||
} else { | ||
results.affected_rows += res.affected_rows; | ||
results.changed_rows += res.changed_rows; | ||
} | ||
} else { | ||
results.affected_rows += res.affected_rows; | ||
results.changed_rows += res.changed_rows; | ||
errors.push(err); | ||
} | ||
} else { | ||
errors.push(err); | ||
} | ||
if (sqls.length > 0) { | ||
setTimeout(next_batch,0); | ||
} else { | ||
return cb(errors, results); | ||
} | ||
}); | ||
})(); | ||
if (sqls.length > 0) { | ||
setTimeout(next_batch,0); | ||
} else { | ||
return cb(errors, results); | ||
} | ||
}); | ||
})(); | ||
}; | ||
if (!cb || cb !== 'function') { | ||
return new Promise(handler); | ||
} else { | ||
handler(); | ||
} | ||
} | ||
@@ -221,10 +244,7 @@ | ||
if (typeof cb !== 'function') { | ||
throw new Error("delete(): No callback function has been provided!"); | ||
} | ||
const sql = this._delete(table, where); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -235,3 +255,5 @@ | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -242,3 +264,5 @@ | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -245,0 +269,0 @@ } |
@@ -41,17 +41,41 @@ const mysql = require('mysql'); | ||
this._pool.getConnection((err, connection) => { | ||
if (err) throw err; | ||
const adapter = new Single(this._original_settings, { | ||
pool: { | ||
pool: this._pool, | ||
connection: connection | ||
} | ||
const self = this; | ||
const handler = (resolve, reject) => { | ||
self._pool.getConnection((err, connection) => { | ||
if (err) throw err; | ||
const adapter = new Single(self._original_settings, { | ||
pool: { | ||
pool: self._pool, | ||
connection | ||
} | ||
}); | ||
if ((!cb || typeof cb !== 'function') && (typeof resolve === 'function' && typeof reject === 'function')) return resolve(adapter); | ||
else if (cb && typeof cb === 'function') return cb(adapter); | ||
throw ERRORS.NO_VALID_RESULTS_HANDLER; | ||
}); | ||
} | ||
cb(adapter); | ||
}); | ||
if (!cb || (cb && typeof cb !== 'function')) { | ||
return new Promise(handler); | ||
} else { | ||
handler(); | ||
} | ||
} | ||
disconnect(cb) { | ||
this._pool.end(cb); | ||
if (!cb || (cb && typeof cb !== 'function')) { | ||
return new Promise((resolve, reject) => { | ||
this._pool.end((err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} else { | ||
this._pool.end(cb); | ||
} | ||
} | ||
@@ -58,0 +82,0 @@ } |
@@ -34,3 +34,15 @@ // Load MySQL Driver | ||
connect(cb) { | ||
return this._connection.connect(cb); | ||
if (!cb || (cb && typeof cb !== 'function')) { | ||
return new Promise((resolve, reject) => { | ||
return this._connection.connect((err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} else { | ||
this._connection.connect(cb); | ||
} | ||
} | ||
@@ -46,4 +58,16 @@ | ||
disconnect(callback) { | ||
return this._connection.end(callback); | ||
disconnect(cb) { | ||
if (!cb || (cb && typeof cb !== 'function')) { | ||
return new Promise((resolve, reject) => { | ||
this._connection.end((err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} else { | ||
this._connection.end(cb); | ||
} | ||
} | ||
@@ -50,0 +74,0 @@ |
const QueryBuilder = require('./query_builder.js'); | ||
const ERROR = require("../QueryExecError"); | ||
const WrapperPromise = require("../WrapperPromise"); | ||
@@ -36,3 +38,3 @@ // **************************************************************************** | ||
} else { | ||
throw new Error("No connection object available to the Query Exec Library!"); | ||
throw ERROR.NO_CONN_OBJ_ERR; | ||
} | ||
@@ -42,2 +44,3 @@ } | ||
query(sql, cb) { | ||
if (!cb || typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
@@ -54,14 +57,25 @@ } | ||
this.reset_query(sql); | ||
this._exec(sql, (err, row) => { | ||
const handler = (err, row) => { | ||
if (!err) { | ||
//console.dir(row[0].numrows); | ||
if (typeof callback !== "function") { | ||
this.resolve(row[0].numrows); | ||
return; | ||
} | ||
cb(err, row[0].numrows); | ||
} | ||
else { | ||
if (typeof callback !== "function") { | ||
this.reject(err); | ||
return; | ||
} | ||
cb(err, row); | ||
} | ||
}); | ||
} | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this), handler).promisify(); | ||
this._exec(sql, handler); | ||
} | ||
get(table,cb,conn) { | ||
get(table, cb, conn) { | ||
// The table parameter is optional, it could be the cb... | ||
@@ -71,26 +85,29 @@ if (typeof table === 'function' && typeof cb !== 'function') { | ||
} | ||
else if (typeof table === 'undefined' && typeof cb !== 'function') { | ||
throw new Error("No cb function has been provided in your 'get' call!"); | ||
} | ||
const sql = this._get(table); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
get_where(table,where,cb) { | ||
get_where(table, where, cb) { | ||
if (typeof table !== 'string' && !Array.isArray(table)) { | ||
throw new Error("First parameter of get_where() must be a string or an array of strings."); | ||
throw ERROR.FIRST_PARAM_OF_GET_WHERE_ERR; | ||
} | ||
if (Object.prototype.toString.call(where) !== Object.prototype.toString.call({})) { | ||
throw new Error("Second parameter of get_where() must be an object with key:value pairs."); | ||
throw ERROR.SECOND_PARAM_OF_GET_WHERE_ERR; | ||
} | ||
const sql = this._get_where(table,where); | ||
const sql = this._get_where(table, where); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
insert(table,set,cb,ignore,suffix) { | ||
insert(table, set, cb, ignore, suffix) { | ||
const sql = this._insert(table, set, ignore, suffix); | ||
this.reset_query(sql); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
@@ -104,5 +121,8 @@ } | ||
} | ||
const sql = this._insert_ignore(table, set, on_dupe); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -114,13 +134,16 @@ | ||
ignore = null; | ||
} | ||
else if (typeof on_dupe === 'function') { | ||
cb = on_dupe; | ||
on_dupe = null; | ||
} | ||
else if (typeof on_dupe === 'function') { | ||
cb = on_dupe; | ||
on_dupe = null; | ||
} | ||
const sql = this._insert_batch(table, set, ignore, on_dupe); | ||
this.reset_query(sql); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
update(table,set,where,cb) { | ||
update(table, set, where, cb) { | ||
// The where parameter is optional, it could be the cb... | ||
@@ -131,6 +154,3 @@ if (typeof where === 'function' && typeof cb !== 'function') { | ||
} | ||
else if (typeof where === 'undefined' && typeof cb !== 'function') { | ||
throw new Error("No cb function has been provided in your update call!"); | ||
} | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && where.length == 0)) { | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && Object.keys(where).length === 0)) { | ||
where = null; | ||
@@ -141,2 +161,4 @@ } | ||
this.reset_query(sql); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
@@ -146,3 +168,3 @@ } | ||
// TODO: Write this complicated-ass function | ||
update_batch(table,set,index,where,cb) { | ||
update_batch(table, set, index, where, cb) { | ||
// The where parameter is optional, it could be the cb... | ||
@@ -153,6 +175,3 @@ if (typeof where === 'function' && typeof cb !== 'function') { | ||
} | ||
else if (typeof where === 'undefined' && typeof cb !== 'function') { | ||
throw new Error("No cb function has been provided in your update_batch call!"); | ||
} | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && where.length == 0)) { | ||
else if (typeof where === 'undefined' || where === false || (where !== null && typeof where === 'object' && Object.keys(where).length === 0)) { | ||
where = null; | ||
@@ -166,25 +185,40 @@ } | ||
// Execute each batch of (at least) 100 | ||
(function next_batch() { | ||
const sql = sqls.shift(); | ||
this.reset_query(sql); | ||
const handler = (resolve, reject) => { | ||
(function next_batch() { | ||
const sql = sqls.shift(); | ||
this.reset_query(sql); | ||
this._exec(sql, (err, res) => { | ||
if (!err) { | ||
if (null === results) { | ||
results = res; | ||
this._exec(sql, (err, res) => { | ||
if (!err) { | ||
if (null === results) { | ||
results = res; | ||
} else { | ||
results.affected_rows += res.affected_rows; | ||
results.changed_rows += res.changed_rows; | ||
} | ||
} else { | ||
results.affected_rows += res.affected_rows; | ||
results.changed_rows += res.changed_rows; | ||
errors.push(err); | ||
} | ||
} else { | ||
errors.push(err); | ||
} | ||
if (sqls.length > 0) { | ||
setTimeout(next_batch,0); | ||
} else { | ||
return cb(errors, results); | ||
} | ||
}); | ||
})(); | ||
if (sqls.length > 0) { | ||
setTimeout(next_batch, 0); | ||
} else { | ||
if ((!cb || typeof cb !== 'function') && (typeof resolve === 'function' && typeof reject === 'function')) { | ||
if (Array.isArray(errors) && errors.length > 0) return reject(new Error(errors.join("\n\n"))); | ||
return resolve(results); | ||
} else if (cb && typeof cb === 'function') { | ||
return cb(errors, results); | ||
} else { | ||
throw ERRORS.NO_VALID_RESULTS_HANDLER; | ||
} | ||
} | ||
}); | ||
})(); | ||
}; | ||
if (!cb || cb !== 'function') { | ||
return new Promise(handler); | ||
} else { | ||
handler(); | ||
} | ||
} | ||
@@ -204,22 +238,23 @@ | ||
if (typeof cb !== 'function') { | ||
throw new Error("delete(): No callback function has been provided!"); | ||
} | ||
const sql = this._delete(table, where); | ||
this.reset_query(sql); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
empty_table(table, cb) { | ||
const sql = this._empty_table(table,cb); | ||
const sql = this._empty_table(table, cb); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
truncate(table, cb) { | ||
const sql = this._truncate(table,cb); | ||
const sql = this._truncate(table, cb); | ||
this.reset_query(sql); | ||
this._exec(sql,cb); | ||
if (typeof cb !== "function") return new WrapperPromise(sql, this._exec.bind(this)).promisify(); | ||
this._exec(sql, cb); | ||
} | ||
@@ -226,0 +261,0 @@ } |
{ | ||
"name": "node-querybuilder", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"author": "Kyle Farris <kyle@chomponllc.com>", | ||
@@ -5,0 +5,0 @@ "description": "Modeled after Codeigniter's QueryBuilder. Build and execute queries in a safe and database-agnostic way.", |
1668090
94
8802