Skip to content

Commit

Permalink
added new api methods gt,lt,...
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed May 8, 2013
1 parent 7b28aeb commit fd0975b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 12 deletions.
93 changes: 82 additions & 11 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ function AbstractClass(data) {

AbstractClass.prototype._initProperties = function (data, applySetters) {
var self = this;
var ctor = self.constructor;
var ctor = this.constructor;
var ds = ctor.schema.definitions[ctor.modelName];
var properties = ds.properties;
data = data || {};

Object.defineProperty(this, '__sql', {
Object.defineProperty(this, '__cachedRelations', {
writable: true,
enumerable: false,
configurable: true,
value: {}
});

Object.defineProperty(this, '__cachedRelations', {
Object.defineProperty(this, '__data', {
writable: true,
enumerable: false,
configurable: true,
value: {}
});

Object.defineProperty(this, '__data', {
Object.defineProperty(this, '__query', {
writable: true,
enumerable: false,
configurable: true,
Expand Down Expand Up @@ -150,6 +150,10 @@ AbstractClass._forDB = function (data) {
return res;
};

AbstractClass._conditions = {};
AbstractClass._params = {};
AbstractClass._fields = undefined;

AbstractClass.prototype.whatTypeName = function (propName) {
return this.constructor.whatTypeName(propName);
};
Expand Down Expand Up @@ -353,13 +357,16 @@ AbstractClass.find = function find(id, callback) {
* - err (null or Error)
* - Array of instances
*/
AbstractClass.all = function all(params, callback) {
AbstractClass.run = AbstractClass.all = function all(params, callback) {
if (stillConnecting(this.schema, this, arguments)) return;

if (arguments.length === 1) {
callback = params;
params = null;
params = {};
}

params = buildQuery(params, this);

var constr = this;
this.schema.adapter.all(this.modelName, params, function (err, data) {
if (data && data.map) {
Expand Down Expand Up @@ -391,6 +398,7 @@ AbstractClass.findOne = function findOne(params, callback) {
callback = params;
params = {};
}
params = buildQuery(params, this);
params.limit = 1;
this.all(params, function (err, collection) {
if (err || !collection || !collection.length > 0) return callback(err, null);
Expand Down Expand Up @@ -424,14 +432,15 @@ AbstractClass.destroyAll = function destroyAll(callback) {
* @param {Object} where - search conditions (optional)
* @param {Function} callback - callback, called with (err, count)
*/
AbstractClass.count = function (where, callback) {
AbstractClass.count = function (params, callback) {
if (stillConnecting(this.schema, this, arguments)) return;

if (typeof where === 'function') {
callback = where;
where = null;
if (typeof params === 'function') {
callback = params;
params = null;
}
this.schema.adapter.count(this.modelName, callback, where);
params = buildQuery(params, this);
this.schema.adapter.count(this.modelName, callback, params);
};

/**
Expand Down Expand Up @@ -1181,6 +1190,68 @@ AbstractClass.prototype.inspect = function () {
return util.inspect(this.__data, false, 4, true);
};

AbstractClass.select = function (fields) {
this._fields = fields;
return this;
};

['skip','limit','order','sort'].forEach(function(method){
AbstractClass[method] = function (value) {
if(method == 'sort') {
method = 'order';
}
this._params[method] = value;
return this;
};
});

AbstractClass.asc = function (value) {
this._params['order'] = value + ' ASC';
return this;
};

AbstractClass.desc = function (value) {
this._params['order'] = value + ' DESC';
return this;
};

AbstractClass.where = function (key, value) {
this._conditions[key] = value;
return this;
};

['gt','gte','lt','lte','in','inq','ne','neq','nin','regex','like','nlike','between'].forEach(function(method){
AbstractClass[method] = function (key, value) {
if(typeof this._conditions[key] == 'undefined') {
this._conditions[key] = {};
}
this._conditions[key][method] = value;
return this;
};
});

AbstractClass.or = function (params) {
/* implementation here */
return this;
};

function buildQuery(opts, model) {
for (var okey in model._conditions) {
if(typeof opts.where == 'undefined') {
opts.where = {};
}
opts.where[okey] = model._conditions[okey];
}

for (var pkey in model._params) {
if(typeof opts[pkey] == 'undefined') {
opts[pkey] = {};
}
opts[pkey] = model._params[pkey];
}

return opts;
}

/**
* Check whether `s` is not undefined
Expand Down
44 changes: 43 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
{
"name": "caminte",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
"version": "0.0.3-3",
"version": "0.0.4",
"author": "Aleksej Gordejev <[email protected]> (http://www.gordejev.lv)",
"contributors": [
{
"name": "Anatoliy Chakkaev",
"email": "[email protected]"
},
{
"name": "Julien Guimont",
"email": "[email protected]"
},
{
"name": "Joseph Junker",
"email": "[email protected]"
},
{
"name": "Henri Bergius",
"email": "[email protected]"
},
{
"name": "redvulps",
"email": "[email protected]"
},
{
"name": "Felipe Sateler",
"email": "[email protected]"
},
{
"name": "Amir M. Mahmoudi",
"email": "[email protected]"
},
{
"name": "Justinas Stankevičius",
"email": "[email protected]"
},
{
"name": "Rick O'Toole",
"email": "[email protected]"
},
{
"name": "Nicholas Westlake",
"email": "[email protected]"
}
],
"keywords": [
"caminte",
"database",
Expand Down

0 comments on commit fd0975b

Please sign in to comment.