Permalink
Browse files

fix: some bugs in query and base adapter

  • Loading branch information...
XadillaX committed Aug 11, 2016
1 parent ffd98da commit 2b03dc43ad5f79fe0ae297fcef5fe4bf57a546d6
Showing with 42 additions and 11 deletions.
  1. +7 −0 lib/adapters/base.js
  2. +35 −11 lib/query.js
@@ -36,6 +36,13 @@ class Adapter extends EventEmitter {
debug("this adapter instance's execute is not implemented.");
process.nextTick(callback);
}

find(query, callback, options) {
options = null;
process.nextTick(function() {
callback(new Error("this adapter's find function is not implemented yet."));
});
}
}

module.exports = Adapter;
@@ -23,16 +23,20 @@ class ToshihikoQuery {
this._updateData = {};
this._where = {};

Object.defineProperty(this, {
Object.defineProperties(this, {
// alias for fields, to be compatible
field: { value: this.fields, writable: true },

// alias for order, to be compatible
orderBy: { value: this.fields, writable: true }
orderBy: { value: this.order, writable: true }
});
}

where(condition) {
if(typeof condition !== "object") {
throw new Error(`query condition expected to be an object but got ${typeof condition} ${condition}.`);
}

this._where = condition;
return this;
}
@@ -43,6 +47,10 @@ class ToshihikoQuery {
fields = _.compact(fields.split(",").map(field => field.trim()));
}

if(!Array.isArray(fields)) {
throw new Error(`query fields expected to be an array or string but got ${typeof fields} ${fields}.`);
}

this._fields = fields;
return this;
}
@@ -55,12 +63,27 @@ class ToshihikoQuery {
* - [ foo, bar ]: `bar` rows with offset `foo`
*/

if(typeof limit === "string") {
limit = limit.split(",").map(limit => (parseInt(limit) || 0));
if(limit.length > 2) limit = [ limit[0], limit[1] ];
if(arguments.length >= 2) {
this._limit = [ parseInt(arguments[0]) || 0, parseInt(arguments[1]) || 0 ];
return this;
} else if(typeof limit === "number") {
this._limit = [ limit ];
return this;
} else if(typeof limit === "string") {
if(limit.trim() !== "") {
limit = limit.split(",");
} else {
limit = [];
}
}

this._limit = limit;
if(!Array.isArray(limit)) {
throw new Error(`query limit expected to be an array, number or string but got ${typeof limit} ${limit}.`);
}

if(limit.length > 2) limit = [ limit[0], limit[1] ];

this._limit = limit.map(limit => parseInt(limit) || 0);
return this;
}

@@ -78,13 +101,14 @@ class ToshihikoQuery {
*/

if(typeof order === "string") {
order = order.split(",").map(order => {
order = _.compact(order.split(",").map(order => {
const result = {};

order = order.split(" ");
order = _.compact(order.split(" "));
if(!order.length) return null;
result[order[0].trim()] = ((order[1] || "ASC").trim().toUpperCase() === "ASC") ? 1 : -1;
return result;
});
}));
} else if(Array.isArray(order)) {
order = order.map(order => {
const result = {};
@@ -118,8 +142,8 @@ class ToshihikoQuery {

find(callback, toJSON, single) {
this.adapter.find(this, callback, {
toJSON: toJSON,
single: single
toJSON: !!toJSON,
single: !!single
});
}
}

0 comments on commit 2b03dc4

Please sign in to comment.