Skip to content

Commit

Permalink
Merge pull request #2 from sayap/stale
Browse files Browse the repository at this point in the history
Raise error when trying to update a stale object when calling `safeSave` or `safeUpdate`
  • Loading branch information
fadrizul committed Apr 11, 2014
2 parents 5c0fd5b + 36d9982 commit 0d55632
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
53 changes: 50 additions & 3 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,27 @@ var Connection = Class.extend({
return;
}

this._save(true, obj, callback);
this._save(true, false, obj, callback);
},

safeSave: function(obj, callback) {
var self = this;
if (obj instanceof Array) {
if (obj.length === 0) {
callback(null);
} else {
this.save(obj[0], function(err) {
if (err) {
callback(err);
return;
}
self.save(obj.slice(1), callback);
});
}
return;
}

this._save(true, true, obj, callback);
},

update: function(obj, callback) {
Expand All @@ -51,10 +71,30 @@ var Connection = Class.extend({
return;
}

this._save(false, obj, callback);
this._save(false, false, obj, callback);
},

safeUpdate: function(obj, callback) {
var self = this;
if (obj instanceof Array) {
if (obj.length === 0) {
callback(null);
} else {
this.update(obj[0], function(err) {
if (err) {
callback(err);
return;
}
self.update(obj.slice(1), callback);
});
}
return;
}

this._save(false, true, obj, callback);
},

_save: function(isNew, obj, callback) {
_save: function(isNew, raiseStaleError, obj, callback) {
var self = this;

if (obj.validate && typeof(obj.validate) === 'function') {
Expand Down Expand Up @@ -110,6 +150,13 @@ var Connection = Class.extend({
callback(err);
return;
}
if (raiseStaleError && !isNew && !data.changes) {
if (typeof obj.lockVersion !== "undefined" && obj.lockVersion !== null) {
callback(new Error("Attempted to update a stale object: "
+ obj._getModel().modelName) + " " + obj.getId());
return;
}
}
if (isNew && data.lastId) {
var idPropName = obj._getModel().getIdPropertyName();
obj[idPropName] = data.lastId;
Expand Down
22 changes: 22 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,34 @@ exports.define = function (name, columnDefs, opts) {
};
this.save = persistUtil.bind('save', this.save, this);

this.safeSave = function (connection, callback) {
if (!connection) {
throw new Error("connection is null or undefined");
}
if (!connection.update) {
throw new Error("argument 1 to save does not appear to be a connection");
}

if (this._isPersisted()) {
connection.safeUpdate(this, callback);
} else {
connection.safeSave(this, callback);
}
};
this.safeSave = persistUtil.bind('safeSave', this.safeSave, this);

this.update = function (connection, props, callback) {
copyValuesIntoObject(props, this);
this.save(connection, callback);
};
this.update = persistUtil.bind('update', this.update, this);

this.safeUpdate = function (connection, props, callback) {
copyValuesIntoObject(props, this);
this.safeSave(connection, callback);
};
this.safeUpdate = persistUtil.bind('safeUpdate', this.safeUpdate, this);

this.delete = function (connection, callback) {
var self = this;
this._getModel().emit("beforeDelete", this);
Expand Down

0 comments on commit 0d55632

Please sign in to comment.