Comparing version 0.0.7 to 0.0.8
@@ -37,7 +37,2 @@ 'use strict'; | ||
// fast exit | ||
if (dbdata === undefined || dbdata === null) { | ||
return undefined; | ||
} | ||
if (typeof(dbdata) === 'object') { | ||
@@ -839,2 +834,54 @@ for (key in dbdata) { | ||
/** | ||
* Compacts the given object (removes empty objects). | ||
* | ||
* @param {Object} obj Object to compact | ||
* @return {Object} the updated object, the top level object is modified, the return is just a helper | ||
*/ | ||
function compactObject(obj) { | ||
if (obj === undefined || obj === null || | ||
obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj instanceof Date || | ||
obj instanceof ObjectID || obj instanceof Binary) { | ||
return obj; | ||
} | ||
if (!Array.isArray(obj) && typeof obj === 'object') { | ||
var key; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
var v = obj[key]; | ||
// handle undefined | ||
if (v === undefined) { | ||
delete obj[key]; | ||
continue; | ||
} | ||
// special cases (skip) | ||
if (v === null || | ||
v instanceof String || v instanceof Number || v instanceof Boolean || v instanceof Date || | ||
v instanceof ObjectID || v instanceof Binary) { | ||
continue; | ||
} | ||
// handle objects | ||
if (!Array.isArray(v) && typeof v === 'object') { | ||
if (Object.keys(v).length === 0) { | ||
delete obj[key]; | ||
} else { | ||
// recurse | ||
compactObject(v); | ||
// need to recheck since the object might have been updated | ||
if (Object.keys(v).length === 0) { | ||
delete obj[key]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Creates a new Document Model class | ||
@@ -1278,3 +1325,3 @@ * | ||
Model.prototype.toJSON = function () { | ||
return this._internalDocument; | ||
return compactObject(this._internalDocument); | ||
}; | ||
@@ -1288,3 +1335,3 @@ | ||
Model.prototype.toString = function () { | ||
return JSON.stringify(this._internalDocument); | ||
return JSON.stringify(compactObject(this._internalDocument)); | ||
}; | ||
@@ -1334,3 +1381,4 @@ | ||
collection.save(self._internalDocument, options, function (err, savedDocument) { | ||
// compact the internal document so we save on DB storage and network latency | ||
collection.save(compactObject(self._internalDocument), options, function (err, savedDocument) { | ||
if (err) { | ||
@@ -1337,0 +1385,0 @@ return callback(err); |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"engines": { | ||
@@ -11,0 +11,0 @@ "node": ">=0.4.12" |
@@ -140,2 +140,6 @@ # ODM Documentation | ||
undefined Arrays and Object properties are always initialized to `[``]`|{} on get | ||
undefined Arrays and Object properties are always initialized to `[``]`|{} on get | ||
# TODO Future | ||
* As a future feature a dirty flag should be added to quickly avoid saving no changes |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83008
1719
144