Skip to content

Commit

Permalink
Fixes for Model
Browse files Browse the repository at this point in the history
 - Removes obsolete rename functionality
 - Fixes validation on insert
  • Loading branch information
notheotherben committed Dec 11, 2013
1 parent 4d69c29 commit f9abc82
Showing 1 changed file with 1 addition and 73 deletions.
74 changes: 1 addition & 73 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,6 @@ Model.prototype.find = function (conditions, callback) {
var $ = this;
if (!_.isPlainObject(conditions)) conditions = { _id: conditions };

// Handle any renames
for(var upstream in this.options.rename) {
var downstream = this.options.rename[upstream];

if(conditions.hasOwnProperty(upstream)) {
if(conditions.hasOwnProperty(downstream)) delete conditions[downstream];
} else if(conditions.hasOwnProperty(downstream)) {
conditions[upstream] = conditions[downstream];
delete conditions[downstream];
}
}

this.toSource(conditions);

this.collection.find(conditions).toArray(function (err, results) {
Expand Down Expand Up @@ -183,18 +171,6 @@ Model.prototype.findOne = Model.prototype.get = function (conditions, callback)
var $ = this;
if (!_.isPlainObject(conditions)) conditions = { _id: conditions };

// Handle any renames
for(var upstream in this.options.rename) {
var downstream = this.options.rename[upstream];

if(conditions.hasOwnProperty(upstream)) {
if(conditions.hasOwnProperty(downstream)) delete conditions[downstream];
} else if(conditions.hasOwnProperty(downstream)) {
conditions[upstream] = conditions[downstream];
delete conditions[downstream];
}
}

this.toSource(conditions);

this.collection.findOne(conditions, function (err, results) {
Expand Down Expand Up @@ -265,21 +241,9 @@ Model.prototype.insert = Model.prototype.create = function (object, callback) {
if(err) return prepNext(err);

// Validate the object
var validation = validate($.options.schema, obj, $.extraValidators);
var validation = validate($.schema, obj, undefined, $.extraValidators);
if(!validation.passed) return prepNext(validation.toError());

// Handle any renames
for(var upstream in $.options.rename) {
var downstream = $.options.rename[upstream];

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
$.toSource(obj);

Expand Down Expand Up @@ -315,18 +279,6 @@ Model.prototype.update = function (conditions, changes, callback) {
/// <param name="callback" type="Function">A function to be called once the update has completed</param>
/// </signature>

// Handle any renames
for(var upstream in this.options.rename) {
var downstream = this.options.rename[upstream];

if(conditions.hasOwnProperty(upstream)) {
if(conditions.hasOwnProperty(downstream)) delete conditions[downstream];
} else if(conditions.hasOwnProperty(downstream)) {
conditions[upstream] = conditions[downstream];
delete conditions[downstream];
}
}

this.toSource(conditions);

this.collection.update(conditions, changes, { w: callback ? 1 : 0, multi: true }, callback);
Expand All @@ -348,18 +300,6 @@ Model.prototype.count = function (conditions, callback) {
conditions = {};
}

// Handle any renames
for(var upstream in this.options.rename) {
var downstream = this.options.rename[upstream];

if(conditions.hasOwnProperty(upstream)) {
if(conditions.hasOwnProperty(downstream)) delete conditions[downstream];
} else if(conditions.hasOwnProperty(downstream)) {
conditions[upstream] = conditions[downstream];
delete conditions[downstream];
}
}

this.toSource(conditions);

this.collection.count(conditions, callback);
Expand Down Expand Up @@ -388,18 +328,6 @@ Model.prototype.remove = function (conditions, callback) {

if (!_.isPlainObject(conditions)) conditions = { _id: conditions };

// Handle any renames
for(var upstream in this.options.rename) {
var downstream = this.options.rename[upstream];

if(conditions.hasOwnProperty(upstream)) {
if(conditions.hasOwnProperty(downstream)) delete conditions[downstream];
} else if(conditions.hasOwnProperty(downstream)) {
conditions[upstream] = conditions[downstream];
delete conditions[downstream];
}
}

this.toSource(conditions);

this.collection.remove(conditions, { w: callback ? 1 : 0 }, callback);
Expand Down

0 comments on commit f9abc82

Please sign in to comment.