Skip to content

Commit

Permalink
More improvements to rename functionality, v1.3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 3, 2013
1 parent af292aa commit 235a310
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
74 changes: 34 additions & 40 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iridium",
"version": "1.3.7",
"version": "1.3.8",
"author": "Benjamin Pannell <admin@sierrasoftworks.com>",
"description": "A custom lightweight ORM for MongoDB designed for power-users",
"homepage": "https://sierrasoftworks.com/iridium",
Expand Down
2 changes: 1 addition & 1 deletion test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('orm', function () {

before(function(done) {
model = new Model(db, 'model', {
name: String
name: /.+/
}, {
renames: {
_id: 'name'
Expand Down

0 comments on commit 235a310

Please sign in to comment.