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) {
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 limit = 200;
var skip = 0;
if (filter != null) {
limit = filter.limit || limit;
skip = filter.skip ||skip;
}
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(this))
);
}.bind(self)));
});
};
CradleAdapter.prototype.all = function(model, filter, callback) {

View File

@ -175,11 +175,25 @@ 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) {
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]);
}
}
@ -187,22 +201,33 @@ BridgeToRedis.prototype.forDb = 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) {
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);
if (!p[i]) continue;
if (!data[i]) {
data[i] = "";
continue;
}
} else if (p[i] && [
'String', 'Text', 'Number', 'Boolean', 'Date'
].indexOf(p[i].type.name) === -1) {
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) {}
}
catch(e) {
data[i] = d;
}
}
}
return data;