diff --git a/lib/Model.js b/lib/Model.js index db218e6..fbc25f4 100644 --- a/lib/Model.js +++ b/lib/Model.js @@ -91,16 +91,15 @@ Model.prototype.find = function (conditions, callback) { if (!_.isPlainObject(conditions)) conditions = { _id: conditions }; // Handle any renames - for(var upstream in $.options.renames) { - var downstream = $.options.renames[upstream]; - - conditions[upstream] = conditions[upstream] || conditions[downstream]; - - if(conditions[upstream] === undefined) - delete conditions[upstream]; + for(var upstream in this.options.renames) { + var downstream = this.options.renames[upstream]; - if(conditions[downstream] !== undefined) + if(conditions.hasOwnProperty(upstream)) { + if(conditions.hasOwnProperty(downstream)) delete conditions[downstream]; + } else if(conditions.hasOwnProperty(downstream)) { + conditions[upstream] = conditions[downstream]; delete conditions[downstream]; + } } transform.up(this.options.transforms, conditions); @@ -137,16 +136,15 @@ Model.prototype.findOne = Model.prototype.get = function (conditions, callback) if (!_.isPlainObject(conditions)) conditions = { _id: conditions }; // Handle any renames - for(var upstream in $.options.renames) { - var downstream = $.options.renames[upstream]; - - conditions[upstream] = conditions[upstream] || conditions[downstream]; - - if(conditions[upstream] === undefined) - delete conditions[upstream]; + for(var upstream in this.options.renames) { + var downstream = this.options.renames[upstream]; - if(conditions[downstream] !== undefined) + if(conditions.hasOwnProperty(upstream)) { + if(conditions.hasOwnProperty(downstream)) delete conditions[downstream]; + } else if(conditions.hasOwnProperty(downstream)) { + conditions[upstream] = conditions[downstream]; delete conditions[downstream]; + } } transform.up(this.options.transforms, conditions); @@ -226,13 +224,12 @@ Model.prototype.insert = Model.prototype.create = function (object, callback) { for(var upstream in $.options.renames) { var downstream = $.options.renames[upstream]; - obj[upstream] = obj[upstream] || obj[downstream]; - - if(obj[upstream] === undefined) - delete obj[upstream]; - - if(obj[downstream] !== undefined) + if(obj.hasOwnProperty(upstream)) { + if(obj.hasOwnProperty(downstream)) delete obj[downstream]; + } else if(obj.hasOwnProperty(downstream)) { + obj[upstream] = obj[downstream]; delete obj[downstream]; + } } // Transform the object @@ -274,13 +271,12 @@ Model.prototype.update = function (conditions, changes, callback) { for(var upstream in this.options.renames) { var downstream = this.options.renames[upstream]; - conditions[upstream] = conditions[upstream] || conditions[downstream]; - - if(conditions[upstream] === undefined) - delete conditions[upstream]; - - if(conditions[downstream] !== undefined) + if(conditions.hasOwnProperty(upstream)) { + if(conditions.hasOwnProperty(downstream)) delete conditions[downstream]; + } else if(conditions.hasOwnProperty(downstream)) { + conditions[upstream] = conditions[downstream]; delete conditions[downstream]; + } } transform.up(this.options.transforms, conditions); @@ -308,13 +304,12 @@ Model.prototype.count = function (conditions, callback) { for(var upstream in this.options.renames) { var downstream = this.options.renames[upstream]; - conditions[upstream] = conditions[upstream] || conditions[downstream]; - - if(conditions[upstream] === undefined) - delete conditions[upstream]; - - if(conditions[downstream] !== undefined) + if(conditions.hasOwnProperty(upstream)) { + if(conditions.hasOwnProperty(downstream)) delete conditions[downstream]; + } else if(conditions.hasOwnProperty(downstream)) { + conditions[upstream] = conditions[downstream]; delete conditions[downstream]; + } } transform.up(this.options.transforms, conditions); @@ -349,13 +344,12 @@ Model.prototype.remove = function (conditions, callback) { for(var upstream in this.options.renames) { var downstream = this.options.renames[upstream]; - conditions[upstream] = conditions[upstream] || conditions[downstream]; - - if(conditions[upstream] === undefined) - delete conditions[upstream]; - - if(conditions[downstream] !== undefined) + if(conditions.hasOwnProperty(upstream)) { + if(conditions.hasOwnProperty(downstream)) delete conditions[downstream]; + } else if(conditions.hasOwnProperty(downstream)) { + conditions[upstream] = conditions[downstream]; delete conditions[downstream]; + } } transform.up(this.options.transforms, conditions); diff --git a/lib/utils/transforms.js b/lib/utils/transforms.js index 6f4b74e..0454492 100644 --- a/lib/utils/transforms.js +++ b/lib/utils/transforms.js @@ -17,8 +17,8 @@ function transform(transforms, method, properties) { if (transforms[k] && transforms[k][method]) { // Have a direct transform to apply var newValue = transforms[k][method](properties[k]); - if(newValue === undefined && properties[k] !== undefined) delete properties[k]; - else if(newValue !== undefined) properties[k] = newValue; + if(newValue === undefined) delete properties[k]; + else properties[k] = newValue; } else if (transforms[k]) { // Have a nested transformation to apply transform(transforms[k], method, properties[k]); diff --git a/package.json b/package.json index d9d7308..6f0bfb8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iridium", - "version": "1.3.7", + "version": "1.3.8", "author": "Benjamin Pannell ", "description": "A custom lightweight ORM for MongoDB designed for power-users", "homepage": "https://sierrasoftworks.com/iridium", diff --git a/test/model.js b/test/model.js index 2ba29b9..be50491 100644 --- a/test/model.js +++ b/test/model.js @@ -35,7 +35,7 @@ describe('orm', function () { before(function(done) { model = new Model(db, 'model', { - name: String + name: /.+/ }, { renames: { _id: 'name'