Fix Update bug when id not found

This commit is contained in:
Loay 2017-04-10 17:55:29 -04:00
parent 6bb07257d3
commit f654056f18
1 changed files with 17 additions and 1 deletions

View File

@ -889,10 +889,26 @@ SQLConnector.prototype._constructUpdateQuery = function(model, where, fields) {
* @param {Function} cb The callback function
*/
SQLConnector.prototype.update = function(model, where, data, options, cb) {
var self = this;
var stmt = this.buildUpdate(model, where, data, options);
this._executeAlteringQuery(model, stmt.sql, stmt.params, options, cb || NOOP);
this._executeAlteringQuery(model, stmt.sql, stmt.params, options, function(err, info) {
if (err) return cb(err);
if (where.id && info.count === 0) {
return cb(errorIdNotFoundForUpdate(where.id));
} else {
return cb(null, info) || NOOP;
}
});
};
function errorIdNotFoundForUpdate(idValue) {
var msg = g.f(
'Could not update attributes. {{Object}} with {{id}} %s does not exist!', idValue);
var error = new Error(msg);
error.statusCode = error.status = 404;
return error;
}
/**
* Replace all instances that match the where clause with the given data
* @param {String} model The model name