Skip to content

Commit

Permalink
fix: some bugs in query and base adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed Aug 11, 2016
1 parent ffd98da commit 2b03dc4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/adapters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
46 changes: 35 additions & 11 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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 = {};
Expand Down Expand Up @@ -118,8 +142,8 @@ class ToshihikoQuery {

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

0 comments on commit 2b03dc4

Please sign in to comment.