fixed type check strictly

This commit is contained in:
taiyoh 2012-11-05 22:22:47 +09:00
parent e10c42ed1b
commit 885e9188f8
1 changed files with 82 additions and 21 deletions

View File

@ -172,37 +172,98 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) {
cb(null, Number); cb(null, Number);
}; };
// 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]);
// }
// }
// return data;
// };
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) {
// var p = this._models[model].properties;
// 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) {}
// }
// }
// 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;