Skip to content

Commit

Permalink
NPM integration
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Feb 17, 2011
1 parent 1f4efab commit 04545f7
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 111 deletions.
83 changes: 40 additions & 43 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
var Step = require('step');


var Model = module.exports = function(orm,name){
var Model = module.exports = function(ron,name){
// Public
this.name = name;
this.properties = {};
// Private
this._orm = orm;
this._ron = ron;
this._identify = 'id';
this._unique = [];
this._required = [];
Expand Down Expand Up @@ -40,53 +40,51 @@ Model.prototype.required = function(property){
}

Model.prototype.clear = function(callback){
var self = this;
this.list(function(err,ids){
if(err){ return callback(err); }
var multi = self._orm.client().multi();
var multi = this._ron.client().multi();
ids.forEach(function(id){
multi.del('obj:'+self.name+':'+id);
});
self._unique.forEach(function(property){
multi.del('index:'+self.name+':'+property);
});
multi.del('index:'+self.name);
multi.del('count:'+self.name);
multi.exec(function(err,values){
multi.del('obj:'+this.name+':'+id);
}.bind(this));
this._unique.forEach(function(property){
multi.del('index:'+this.name+':'+property);
}.bind(this));
multi.del('index:'+this.name);
multi.del('count:'+this.name);
multi.exec(function(err){
callback(err,ids);
})
})
}.bind(this));
};


Model.prototype.delete = function(id,callback){
var self = this;
if(self._unique.length){
var client = this._orm.client();
if(this._unique.length){
var client = this._ron.client();
var properties = [];
var args = ['obj:'+self.name+':'+id];
self._unique.forEach(function(property){
var args = ['obj:'+this.name+':'+id];
this._unique.forEach(function(property){
args.push(property);
properties.push(property);
});
args.push(function(err,values){
var multi = self._orm.client().multi();
var multi = this._ron.client().multi();
properties.forEach(function(property,i){
multi.del('index:'+self.name+':'+property,values[i]);
});
multi.del('obj:'+self.name+':'+id);
multi.srem('index:'+self.name,id);
multi.del('index:'+this.name+':'+property,values[i]);
}.bind(this));
multi.del('obj:'+this.name+':'+id);
multi.srem('index:'+this.name,id);
multi.exec(function(err,values){
callback(err,id);
})
});
}.bind(this));
client.hmget.apply(client,args);
}else{
var multi = this._orm.client().multi();
multi.del('obj:'+self.name+':'+id);
multi.srem('index:'+self.name,id);
// self._unique.forEach(function(property){
// multi.set('index:'+self.name+':'+property+':'+record.username,id);
var multi = this._ron.client().multi();
multi.del('obj:'+this.name+':'+id);
multi.srem('index:'+this.name,id);
// this._unique.forEach(function(property){
// multi.set('index:'+this.name+':'+property+':'+record.username,id);
// })
multi.exec(function(err,values){
callback(err,values.every(function(value){return value;}));
Expand All @@ -107,33 +105,32 @@ Model.prototype.delete = function(id,callback){
*/
Model.prototype.get = function(){
var args = Array.prototype.slice.call(arguments),
self = this,
callback = args.pop(),
properties = args[args.length-1] instanceof Array ? args.pop(): [];
// Using its identifier & multi passed as argument
if( args.length === 1 && typeof callback === 'object' ){
var multi = callback;
args[0].forEach(function(id){
if(properties.length){
properties.unshift('obj:'+self.name+':'+id);
properties.unshift('obj:'+this.name+':'+id);
multi.hmget.apply(multi,properties);
}else{
multi.hgetall('obj:'+self.name+':'+id);
multi.hgetall('obj:'+this.name+':'+id);
}
});
}.bind(this));
// Using its identifier
}else if( args.length === 1 ){
if(args[0] instanceof Array){
var multi = this._orm.client().multi();
var multi = this._ron.client().multi();
this.get(args[0],properties,multi);
multi.exec(function(err,records){
records[this._identify] = args[0];
callback(err,records);
}.bind(this));
}else{
var client = this._orm.client();
var client = this._ron.client();
if(properties.length){
properties.unshift('obj:'+self.name+':'+args[0]);
properties.unshift('obj:'+this.name+':'+args[0]);
properties.push(function(err,values){
var record = {};
record[this._identify] = args[0];
Expand All @@ -152,13 +149,13 @@ Model.prototype.get = function(){
}
// Using a unique property
}else{
this._orm.client().hget('index:'+this.name+':'+args[0],args[1],function(err,id){
this._ron.client().hget('index:'+this.name+':'+args[0],args[1],function(err,id){
if(err){ return callback(err); }
if(id === null) {
return callback(null, null);
}
self.get(id,properties,callback);
})
this.get(id,properties,callback);
}.bind(this));
}
return this;
};
Expand Down Expand Up @@ -200,7 +197,7 @@ Model.prototype.put = function(){
}
};
Model.prototype.create = function(record,callback){
var client = this._orm.client();
var client = this._ron.client();
client.incr('count:'+this.name,function(err,id){
if(err){ return callback(err); }
record[this._identify] = id;
Expand All @@ -209,7 +206,7 @@ Model.prototype.create = function(record,callback){
};

Model.prototype.update = function(record,callback){
var multi = this._orm.client().multi(),
var multi = this._ron.client().multi(),
id = record[this._identify];
this._unique.forEach(function(property){
multi.hset('index:'+this.name+':'+property,record[property],id);
Expand All @@ -223,13 +220,13 @@ Model.prototype.update = function(record,callback){
};

Model.prototype.list = function(callback){
this._orm.client().smembers('index:'+this.name,function(err,values){
this._ron.client().smembers('index:'+this.name,function(err,values){
callback(err,values);
});
};

Model.prototype.length = function(callback){
this._orm.client().scard('index:'+this.name,function(err,length){
this._ron.client().scard('index:'+this.name,function(err,length){
callback(err,length);
});
};
25 changes: 0 additions & 25 deletions lib/orm.js

This file was deleted.

27 changes: 27 additions & 0 deletions lib/ron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

var redis = require('redis'),
Model = require('./Model');

var ron = {};

ron.Model = Model;

ron.client = function(client){
if(!ron._client){
ron._client = client||redis.createClient();
}
return ron._client;
}

ron.create = function(name){
return ron[name] = new Model(this, name);
}

ron.quit = function(name){
if(ron._client){
ron._client.quit();
delete ron._client;
}
}

module.exports = ron;
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ "name": "ron"
, "version": "0.0.1"
, "description": "Redis ORM for NodeJs"
, "author": "David Worms <david@adaltas.com>"
, "contributors":
[ ]
, "main": "./lib/ron"
, "engines": { "node": ">= 0.3.0" }
, "keywords": ["redis", "orm", "database", "nosql"]
, "repository":
{ "type" : "git"
, "url" : "https://github.com/wdavidw/node-redis-orm.git"
}
}
34 changes: 17 additions & 17 deletions test/model.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@

var orm = require(__dirname+'/../lib/orm'),
Model = require(__dirname+'/../lib/Model'),
assert = require('assert');
var assert = require('assert'),
ron = require('ron'),
Model = ron.Model;

module.exports = {
'Test creation': function(callback){
// Test return instance
assert.ok(orm.create('TestCreation') instanceof Model);
assert.ok(ron.create('TestCreation') instanceof Model);
// Test registration
var model = orm.create('TestCreation');
assert.eql(orm.TestCreation, model);
var model = ron.create('TestCreation');
assert.eql(ron.TestCreation, model);
assert.eql('TestCreation', model.name);
},
'Test properties': function(callback){
var model = orm.create('TestProperties');
var model = ron.create('TestProperties');
['get','put','delete'].forEach(function(method){
assert.eql('function', typeof model[method]);
})
},
'Test CRUD': function(callback){
var model = orm.create('TestCRUD');
var model = ron.create('TestCRUD');
var data = { property: 'value' };
// Test creation
model.put(data, function(err,record){
Expand All @@ -41,14 +41,14 @@ module.exports = {
model.delete(id,function(err,success){
assert.ifError(err);
assert.ok(success);
orm.quit();
ron.quit();
});
});
});
});
},
'Test delete': function(callback){
var model = orm.create('TestDelete');
var model = ron.create('TestDelete');
var data = { property: 'value' };
// Test creation
model.put(data, function(err,record){
Expand All @@ -60,13 +60,13 @@ module.exports = {
model.delete(record.id,function(err,success){
assert.ifError(err);
assert.ok(!success);
orm.quit();
ron.quit();
});
});
})
},
'Test list and clear': function(callback){
var model = orm.create('TestDelete');
var model = ron.create('TestDelete');
var data = { property: 'value' };
// Create 2 records
model.put({ property: 'value 1' }, function(err,record){
Expand All @@ -82,15 +82,15 @@ module.exports = {
model.list(function(err,ids){
assert.ifError(err);
assert.eql(0,ids.length);
orm.quit();
ron.quit();
});
});
});
})
})
},
'Test put': function(callback){
var model = orm.create('TestPut');
var model = ron.create('TestPut');
var data = { property: 'value' };
// Create 2 records
model.put({ property_1: 'value 1' }, function(err,record){
Expand All @@ -106,14 +106,14 @@ module.exports = {
assert.eql('value 2',record.property_2);
// Clear all ids
model.clear(function(err,ids){
orm.quit();
ron.quit();
});
});
})
})
},
'Test get properties': function(callback){
var model = orm.create('TestGetProperties');
var model = ron.create('TestGetProperties');
var data = { property: 'value' };
// Create 2 records
model.put({
Expand All @@ -129,7 +129,7 @@ module.exports = {
assert.eql('value 3',record.property_3);
// Clear all ids
model.clear(function(err,ids){
orm.quit();
ron.quit();
});
});
})
Expand Down
Loading

0 comments on commit 04545f7

Please sign in to comment.