Merge pull request #146 from taiyoh/master

BridgeToRedis#forDb and BridgeToRedis#fromDb methods fixed
This commit is contained in:
Anatoliy Chakkaev 2012-11-05 07:31:09 -08:00
commit 64d46ecc77
1 changed files with 46 additions and 21 deletions

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;