Permalink
Browse files

feat: query feature started

  • Loading branch information...
XadillaX committed Aug 11, 2016
1 parent 3cd305b commit d1ddc9c7e4b4fd5bf050c74a55302c7dbc940b4d
Showing with 165 additions and 2 deletions.
  1. +34 −0 lib/model.js
  2. +127 −0 lib/query.js
  3. +4 −2 lib/toshihiko.js
@@ -6,12 +6,16 @@
*/
"use strict";

const debug = require("debug")("toshihiko:model");
const EventEmitter = require("eventemitter2").EventEmitter2;

const Field = require("./field");
const Query = require("./query");

class ToshihikoModel extends EventEmitter {
constructor(collectionName, toshihiko, schema, options) {
super();

this.ai = null;

Object.defineProperties(this, {
@@ -72,6 +76,8 @@ class ToshihikoModel extends EventEmitter {
value: null
});
}

debug(`"${this.name}" created.`, this);
}

/**
@@ -80,6 +86,34 @@ class ToshihikoModel extends EventEmitter {
get toshihiko() {
return this.parent;
}

where(condition) {
return (new Query(this)).where(condition);
}

field(fields) {
return (new Query(this)).fields(fields);
}

fields(fields) {
return (new Query(this)).fields(fields);
}

limit(limit) {
return (new Query(this)).limit(limit);
}

order(order) {
return (new Query(this)).order(order);
}

orderBy(order) {
return (new Query(this)).orderBy(order);
}

find(callback, toJSON, single) {
return (new Query(this)).find(callback, toJSON, single);
}
}

module.exports = ToshihikoModel;
@@ -0,0 +1,127 @@
/**
* XadillaX created at 2016-08-11 14:23:10 With ♥
*
* Copyright (c) 2016 Souche.com, all rights
* reserved.
*/
"use strict";

const _ = require("lodash");

class ToshihikoQuery {
constructor(model) {
Object.defineProperties(this, {
toshihiko: { value: model.parent },
adapter: { value: model.parent.adapter },
model: { value: model },
cache: { value: model.cache }
});

this._fields = this.model.schema.map(field => field.name);
this._limit = [];
this._order = [];
this._updateData = {};
this._where = {};

Object.defineProperty(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 }
});
}

where(condition) {
this._where = condition;
return this;
}

fields(fields) {
if(typeof fields === "string") {
// 'foo,bar, baz, ...'
fields = _.compact(fields.split(",").map(field => field.trim()));
}

this._fields = fields;
return this;
}

limit(limit) {
/**
* limit
* - []: no limit
* - [ foo ]: `foo` rows
* - [ 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] ];
}

this._limit = limit;
return this;
}

order(order) {
/**
* order
* - object
* - { foo: 1, bar: -1 }
* - { foo: "asc", bar: "desc" }
* - array
* - [ "foo asc", "bar desc" ]
* - [ { foo: 1 }, { bar: "desc" } ]
* - string
* - "foo, bar desc"
*/

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

order = order.split(" ");
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 = {};

if(typeof order === "string") {
order = order.split(" ");
result[order[0].trim()] = ((order[1] || "ASC").trim().toUpperCase() === "ASC") ? 1 : -1;
return result;
}

Object.keys(order).forEach(key => {
result[key.trim()] = (typeof order[key] === "number") ?
order[key] :
(order[key].toUpperCase() === "ASC" ? 1 : -1);
});
return result;
});
} else {
order = Object.keys(order).map(key => {
const result = {};
result[key.trim()] = (typeof order[key] === "number") ?
order[key] :
(order[key].toUpperCase() === "ASC" ? 1 : -1);
return result;
});
}

this._order = order;
return this;
}

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

module.exports = ToshihikoQuery;
@@ -48,15 +48,17 @@ Toshihiko.createCache = function(param) {
}

let path;
if(param.path) {
if(param.module) {
path = "$$$";
} else if(param.path) {
path = param.path;
} else if(param.name) {
path = `toshihiko-${param.name}`;
} else {
return null;
}

const m = require(path);
const m = param.module ? param.module : require(path);
const func = m.create;
const keys = common.getParamNames(func);

0 comments on commit d1ddc9c

Please sign in to comment.