Merge branch 'master' of github.com:1602/jugglingdb

This commit is contained in:
Anatoliy Chakkaev 2012-11-06 19:53:04 +04:00
commit 3731447787
2 changed files with 71 additions and 32 deletions

View File

@ -221,7 +221,7 @@ CradleAdapter.prototype.updateOrCreate = function(model, data, callback) {
if(err) { if(err) {
this.create(model, data, callback); this.create(model, data, callback);
} else { } else {
this.updateAttributes(mode, data.id, data, callback); this.updateAttributes(model, data.id, data, callback);
} }
}.bind(this) }.bind(this)
) )
@ -260,17 +260,31 @@ CradleAdapter.prototype.count = function(model, callback, where) {
}; };
CradleAdapter.prototype.models = function(model, filter, callback, func) { CradleAdapter.prototype.models = function(model, filter, callback, func) {
this.client.all( var limit = 200;
{include_docs: true}, var skip = 0;
errorHandler(callback, function(res, cb) { if (filter != null) {
var docs = res.map(function(doc) { limit = filter.limit || limit;
return idealize(doc); skip = filter.skip ||skip;
}); }
var filtered = filtering(docs, model, filter, this._models)
func ? func(filtered, cb) : cb(filtered); var self = this;
}.bind(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) { CradleAdapter.prototype.all = function(model, filter, callback) {

View File

@ -175,34 +175,59 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) {
BridgeToRedis.prototype.forDb = function (model, data) { BridgeToRedis.prototype.forDb = function (model, data) {
var p = this._models[model].properties; var p = this._models[model].properties;
for (var i in data) { for (var i in data) {
if (p[i] && p[i].type.name === 'Date') { if (!p[i]) continue;
data[i] = data[i] && data[i].getTime ? data[i].getTime() : 0; if (!data[i]) {
} else if (p[i] && [ data[i] = "";
'String', 'Text', 'Number', 'Boolean', 'Date' continue;
].indexOf(p[i].type.name) === -1) { }
data[i] = JSON.stringify(data[i]); 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; return data;
}; };
BridgeToRedis.prototype.fromDb = function (model, 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) { for (var i in data) {
if (p[i] && p[i].type.name === 'Date') { if (!p[i]) continue;
if (isNaN(parseInt(data[i]))) { if (!data[i]) {
data[i] = new Date(data[i]); data[i] = "";
} else { continue;
var ms = data[i]; }
data[i] = new Date(); switch (p[i].type.name) {
data[i].setTime(ms); case "Date":
} d = new Date(data[i]);
} else if (p[i] && [ d.setTime(data[i]);
'String', 'Text', 'Number', 'Boolean', 'Date' data[i] = d;
].indexOf(p[i].type.name) === -1) { break;
try { case "Number":
data[i] = JSON.parse(data[i]); data[i] = Number(data[i]);
} catch (e) {} 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; return data;