diff --git a/lib/adapters/cradle.js b/lib/adapters/cradle.js index d624652c..cd1b74a7 100644 --- a/lib/adapters/cradle.js +++ b/lib/adapters/cradle.js @@ -221,7 +221,7 @@ CradleAdapter.prototype.updateOrCreate = function(model, data, callback) { if(err) { this.create(model, data, callback); } else { - this.updateAttributes(mode, data.id, data, callback); + this.updateAttributes(model, data.id, data, callback); } }.bind(this) ) @@ -260,17 +260,31 @@ CradleAdapter.prototype.count = function(model, callback, where) { }; CradleAdapter.prototype.models = function(model, filter, callback, func) { - this.client.all( - {include_docs: true}, - errorHandler(callback, function(res, cb) { - var docs = res.map(function(doc) { - return idealize(doc); - }); - var filtered = filtering(docs, model, filter, this._models) + var limit = 200; + var skip = 0; + if (filter != null) { + limit = filter.limit || limit; + skip = filter.skip ||skip; + } - func ? func(filtered, cb) : cb(filtered); - }.bind(this)) - ); + var self = this; + + self.client.save('_design/'+model, { + views : { + all : { + map : 'function(doc) { if (doc.nature == "'+model+'") { emit(doc._id, doc); } }' + } + } + }, function() { + self.client.view(model+'/all', {include_docs:true, limit:limit, skip:skip}, errorHandler(callback, function(res, cb) { + var docs = res.map(function(doc) { + return idealize(doc); + }); + var filtered = filtering(docs, model, filter, this._models) + + func ? func(filtered, cb) : cb(filtered); + }.bind(self))); + }); }; CradleAdapter.prototype.all = function(model, filter, callback) { diff --git a/lib/adapters/redis2.js b/lib/adapters/redis2.js index 31d6a479..a6a344a6 100644 --- a/lib/adapters/redis2.js +++ b/lib/adapters/redis2.js @@ -175,34 +175,59 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) { BridgeToRedis.prototype.forDb = function (model, data) { var p = this._models[model].properties; for (var i in data) { - if (p[i] && p[i].type.name === 'Date') { - data[i] = data[i] && data[i].getTime ? data[i].getTime() : 0; - } else if (p[i] && [ - 'String', 'Text', 'Number', 'Boolean', 'Date' - ].indexOf(p[i].type.name) === -1) { - data[i] = JSON.stringify(data[i]); + if (!p[i]) continue; + if (!data[i]) { + data[i] = ""; + continue; + } + switch (p[i].type.name) { + case "Date": + data[i] = data[i].getTime ? data[i].getTime().toString() : "0"; + break; + case "Number": + data[i] = data[i].toString(); + break; + case "Boolean": + data[i] = !!data[i] ? "1" : "0"; + break; + case "String": + case "Text": + break; + default: + data[i] = JSON.stringify(data[i]); } } return data; }; BridgeToRedis.prototype.fromDb = function (model, data) { - var p = this._models[model].properties; + var p = this._models[model].properties, d; for (var i in data) { - if (p[i] && p[i].type.name === 'Date') { - if (isNaN(parseInt(data[i]))) { - data[i] = new Date(data[i]); - } else { - var ms = data[i]; - data[i] = new Date(); - data[i].setTime(ms); - } - } else if (p[i] && [ - 'String', 'Text', 'Number', 'Boolean', 'Date' - ].indexOf(p[i].type.name) === -1) { - try { - data[i] = JSON.parse(data[i]); - } catch (e) {} + if (!p[i]) continue; + if (!data[i]) { + data[i] = ""; + continue; + } + switch (p[i].type.name) { + case "Date": + d = new Date(data[i]); + d.setTime(data[i]); + data[i] = d; + break; + case "Number": + data[i] = Number(data[i]); + break; + case "Boolean": + data[i] = data[i] === "1"; + break; + default: + d = data[i]; + try { + data[i] = JSON.parse(data[i]); + } + catch(e) { + data[i] = d; + } } } return data;