Comparing version 1.3.2 to 1.3.4
119
lib/patch.js
@@ -18,31 +18,66 @@ var util = require('util'); | ||
// hook find, patch result | ||
function patchResult(o, funcs) { | ||
funcs.forEach(function (func) { | ||
var old_func = o[func]; | ||
if (old_func) { | ||
var new_func = function () { | ||
var rs = old_func.apply(this, Array.prototype.slice.apply(arguments)); | ||
if (rs) | ||
patchSync(rs, [ | ||
"count", | ||
"first", | ||
"last", | ||
'all', | ||
'where', | ||
'find', | ||
'remove', | ||
'run' | ||
]); | ||
return rs; | ||
} | ||
function patchResult(o) { | ||
var old_func = o.find; | ||
var m = o.model || o; | ||
var comps = ['val', 'from', 'to']; | ||
if (!old_func.is_new) { | ||
new_func.is_new = true; | ||
Object.defineProperty(o, func, { | ||
value: new_func, | ||
writable: true | ||
}); | ||
if (old_func.is_new) | ||
return; | ||
function filter_date(opt) { | ||
for (var k in opt) { | ||
if (k === 'or') | ||
opt[k].forEach(filter_date); | ||
else { | ||
var p = m.allProperties[k]; | ||
if (p && p.type === 'date') { | ||
var v = opt[k]; | ||
if (!util.isDate(v)) { | ||
if (util.isNumber(v) || util.isString(v)) | ||
opt[k] = new Date(v); | ||
else if (util.isObject(v)) { | ||
comps.forEach(c => { | ||
var v1 = v[c]; | ||
if (util.isArray(v1)) { | ||
v1.forEach((v2, i) => { | ||
if (!util.isDate(v2)) | ||
v1[i] = new Date(v2); | ||
}); | ||
} else if (v1 !== undefined && !util.isDate(v1)) | ||
v[c] = new Date(v1); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
var new_func = function () { | ||
var opt = arguments[0]; | ||
if (util.isObject(opt) && !util.isFunction(opt)) | ||
filter_date(opt); | ||
var rs = old_func.apply(this, Array.prototype.slice.apply(arguments)); | ||
if (rs) { | ||
patchResult(rs); | ||
patchSync(rs, [ | ||
"count", | ||
"first", | ||
"last", | ||
'all', | ||
'where', | ||
'find', | ||
'remove', | ||
'run' | ||
]); | ||
} | ||
return rs; | ||
} | ||
new_func.is_new = true; | ||
o.where = o.all = o.find = new_func; | ||
} | ||
@@ -137,2 +172,4 @@ | ||
patchResult(m); | ||
patchSync(m, [ | ||
@@ -152,8 +189,2 @@ "clear", | ||
patchResult(m, [ | ||
'find', | ||
'all', | ||
'where' | ||
]); | ||
patchHas(m, [ | ||
@@ -194,2 +225,23 @@ 'hasOne', | ||
function patchDriver(driver) { | ||
if (driver.dialect === 'sqlite') | ||
driver.insert = patchInsert; | ||
var propertyToValue = driver.propertyToValue; | ||
driver.propertyToValue = function (value, property) { | ||
if (property.type === 'date' && | ||
(util.isNumber(value) || util.isString(value))) | ||
value = new Date(value); | ||
return propertyToValue.call(this, value, property); | ||
} | ||
var valueToProperty = driver.valueToProperty; | ||
driver.valueToProperty = function (value, property) { | ||
if (property.type === 'date' && | ||
(util.isNumber(value) || util.isString(value))) | ||
value = new Date(value); | ||
return valueToProperty.call(this, value, property); | ||
} | ||
} | ||
function execQuerySync(query, opt) { | ||
@@ -222,4 +274,3 @@ if (arguments.length == 2) | ||
if (db.driver.dialect === 'sqlite') | ||
db.driver.insert = patchInsert; | ||
patchDriver(db.driver); | ||
@@ -226,0 +277,0 @@ var def = db.define; |
{ | ||
"name": "fib-orm", | ||
"version": "1.3.2", | ||
"version": "1.3.4", | ||
"description": "Object Relational Mapping for fibjs", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
36227
9
343