Merge pull request #73 from strongloop/foreceId-true

Fix replaceById to report an error when id not found
This commit is contained in:
Loay 2017-01-26 00:39:16 -05:00 committed by GitHub
commit 292b90a6e4
1 changed files with 15 additions and 1 deletions

View File

@ -690,9 +690,23 @@ SQLConnector.prototype.update = function(model, where, data, options, cb) {
*/ */
SQLConnector.prototype._replace = function(model, where, data, options, cb) { SQLConnector.prototype._replace = function(model, where, data, options, cb) {
var stmt = this.buildReplace(model, where, data, options); var stmt = this.buildReplace(model, where, data, options);
this.execute(stmt.sql, stmt.params, options, cb); this.execute(stmt.sql, stmt.params, options, function(err, info) {
if (err) return cb(err);
if (info.affectedRows === 0) {
return cb(errorIdNotFoundForReplace(where.id));
} else {
return cb(null, info);
}
});
}; };
function errorIdNotFoundForReplace(idValue) {
var msg = g.f('Could not replace. Object with id %s does not exist!', idValue);
var error = new Error(msg);
error.statusCode = error.status = 404;
return error;
}
SQLConnector.prototype._executeAlteringQuery = function(model, sql, params, options, cb) { SQLConnector.prototype._executeAlteringQuery = function(model, sql, params, options, cb) {
var self = this; var self = this;
this.execute(sql, params, options, function(err, info) { this.execute(sql, params, options, function(err, info) {