|
|
|
@ -6,6 +6,8 @@ |
|
|
|
|
|
|
|
|
|
var dateToJSON = function () { return { $$date: this.getTime() }; } |
|
|
|
|
, originalDateToJSON = Date.prototype.toJSON |
|
|
|
|
, util = require('util') |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -23,6 +25,26 @@ function checkKey (k, v) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Check a DB object and throw an error if it's not valid |
|
|
|
|
* Works by applying the above checkKey function to all fields recursively |
|
|
|
|
*/ |
|
|
|
|
function checkObject (obj) { |
|
|
|
|
if (util.isArray(obj)) { |
|
|
|
|
obj.forEach(function (o) { |
|
|
|
|
checkObject(o); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (typeof obj === 'object') { |
|
|
|
|
Object.keys(obj).forEach(function (k) { |
|
|
|
|
checkKey(k, obj[k]); |
|
|
|
|
checkObject(obj[k]); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Serialize an object to be persisted to a one-line string |
|
|
|
|
* Accepted primitive types: Number, String, Boolean, Date, null |
|
|
|
@ -88,7 +110,6 @@ function deepCopy (obj) { |
|
|
|
|
if (typeof obj === 'object') { |
|
|
|
|
res = {}; |
|
|
|
|
Object.keys(obj).forEach(function (k) { |
|
|
|
|
checkKey(k, obj[k]); |
|
|
|
|
res[k] = deepCopy(obj[k]); |
|
|
|
|
}); |
|
|
|
|
return res; |
|
|
|
@ -105,6 +126,9 @@ function deepCopy (obj) { |
|
|
|
|
function modify (obj, updateQuery) { |
|
|
|
|
updateQuery = deepCopy(updateQuery); |
|
|
|
|
updateQuery._id = obj._id; |
|
|
|
|
|
|
|
|
|
checkObject(updateQuery); |
|
|
|
|
|
|
|
|
|
return updateQuery; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -113,4 +137,5 @@ function modify (obj, updateQuery) { |
|
|
|
|
module.exports.serialize = serialize; |
|
|
|
|
module.exports.deserialize = deserialize; |
|
|
|
|
module.exports.deepCopy = deepCopy; |
|
|
|
|
module.exports.checkObject = checkObject; |
|
|
|
|
module.exports.modify = modify; |
|
|
|
|