Permalink
Browse files

feat: add promise support in query

  • Loading branch information...
XadillaX committed Oct 18, 2016
1 parent 699e72a commit adb99d5aa69ba7e3d1d057968ae540b3b8e8caaa
Showing with 93 additions and 1 deletion.
  1. +93 −1 lib/query.js
@@ -157,13 +157,19 @@ class ToshihikoQuery {
}

find(callback, toJSON, options) {
if(typeof callback !== "function") {
options = toJSON;
toJSON = callback;
callback = emptyFunc;
}
if(typeof toJSON === "object") {
options = toJSON;
toJSON = false;
}
options = options || {};
if(undefined === callback) callback = emptyFunc;

const promise = this._promisify();
const self = this;
this.adapter.find(this, function(err, row, extra) {
if(options.single && row) {
@@ -186,11 +192,20 @@ class ToshihikoQuery {
}
}

// promisify
if(err) {
promise.reject(err);
} else {
promise.resolve(row);
}

return callback(err, row, extra);
}, {
single: !!options.single,
noCache: !!options.noCache
});

return promise.q;
}

findById(_id, callback, toJSON) {
@@ -228,27 +243,104 @@ class ToshihikoQuery {
}

findOne(callback, toJSON) {
this.find(callback, toJSON, { single: true });
return this.find(callback, toJSON, { single: true });
}

update(data, callback) {
if(undefined === callback) callback = emptyFunc;
this._updateData = data;

const promise = this._promisify();
this.adapter.updateByQuery(this, function(err, result, extra) {
// promisify
if(err) {
promise.reject(err);
} else {
promise.resolve(result, extra);
}

return callback(err, result, extra);
});
return promise.q;
}

delete(callback) {
if(undefined === callback) callback = emptyFunc;

const promise = this._promisify();
this.adapter.deleteByQuery(this, function(err, result, extra) {
// promisify
if(err) {
promise.reject(err);
} else {
promise.resolve(result);
}

return callback(err, result, extra);
});
return promise.q;
}

execute() {
const promise = this._promisify();

let trueCallback = emptyFunc;
let cbIdx;
for(let i = 0; i < arguments.length; i++) {
if(typeof arguments[i] === "function") {
trueCallback = arguments[i];
cbIdx = i;
}
}
let callback = function(err, res, extra) {
if(err) {
promise.reject(err);
} else {
promise.resolve(res);
}

trueCallback(err, res, extra);
};
if(cbIdx === undefined){
arguments[arguments.length] = callback;
arguments.length++;
} else {
arguments[cbIdx] = callback;
}

this.adapter.execute.apply(this.adapter, arguments);

return promise.q;
}

_promisify() {
// not using Promise.promisify because I don't want to wrap code logic
// in a Promise
//
// I DO LIKE CALLBACK
let resolve;
let reject;
const q = new Promise(function(_resolve, _reject) {
// this function will be called synchronous
//
// see
//
// https://github.com/petkaantonov/bluebird/blob/v3.4.6/src/promise.js#L78
//
// and
//
// https://github.com/petkaantonov/bluebird/blob/v3.4.6/src/debuggability.js#L303-L309
//
// so we can assign them directly
resolve = _resolve;
reject = _reject;
});

return {
q: q,
resolve: resolve,
reject: reject
};
}
}

0 comments on commit adb99d5

Please sign in to comment.