add async support to database.query()

This commit is contained in:
Girish Ramakrishnan
2021-05-02 21:12:38 -07:00
parent 199eda82d1
commit 035f356dff
4 changed files with 45 additions and 93 deletions

View File

@@ -12,7 +12,7 @@ exports = module.exports = {
_clear: clear
};
var assert = require('assert'),
const assert = require('assert'),
async = require('async'),
BoxError = require('./boxerror.js'),
child_process = require('child_process'),
@@ -86,13 +86,20 @@ function clear(callback) {
}
function query() {
const args = Array.prototype.slice.call(arguments);
const callback = args[args.length - 1];
assert.strictEqual(typeof callback, 'function');
assert.notStrictEqual(gConnectionPool, null);
if (constants.TEST && !gConnectionPool) return callback(new BoxError(BoxError.DATABASE_ERROR, 'database.js not initialized'));
return new Promise((resolve, reject) => {
let args = Array.prototype.slice.call(arguments);
const callback = typeof args[args.length - 1] === 'function' ? args.pop() : null;
gConnectionPool.query.apply(gConnectionPool, args); // this is same as getConnection/query/release
args.push(function queryCallback(error, result) {
if (error) return callback ? callback(error) : reject(new BoxError(BoxError.DATABASE_ERROR, error));
callback ? callback(null, result) : resolve(result);
});
gConnectionPool.query.apply(gConnectionPool, args); // this is same as getConnection/query/release
});
}
function transaction(queries, callback) {