From 885e9188f874ea6d5c16d820b7867646f1468f33 Mon Sep 17 00:00:00 2001 From: taiyoh Date: Mon, 5 Nov 2012 22:22:47 +0900 Subject: [PATCH 1/2] fixed type check strictly --- lib/adapters/redis2.js | 103 ++++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 21 deletions(-) diff --git a/lib/adapters/redis2.js b/lib/adapters/redis2.js index 2dd4893d..7c7fb4b0 100644 --- a/lib/adapters/redis2.js +++ b/lib/adapters/redis2.js @@ -172,37 +172,98 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) { 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) { 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; +// 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) { - 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; From 30e9983abff4374623fe7844439118d0712fd79c Mon Sep 17 00:00:00 2001 From: taiyoh Date: Mon, 5 Nov 2012 22:30:57 +0900 Subject: [PATCH 2/2] remove commentouted source --- lib/adapters/redis2.js | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/lib/adapters/redis2.js b/lib/adapters/redis2.js index 7c7fb4b0..53215b0f 100644 --- a/lib/adapters/redis2.js +++ b/lib/adapters/redis2.js @@ -172,20 +172,6 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) { 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) { var p = this._models[model].properties; for (var i in data) { @@ -214,28 +200,6 @@ BridgeToRedis.prototype.forDb = function (model, 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) { var p = this._models[model].properties, d; for (var i in data) {