From c232653c5db9b789ee3b7be4fcacdfc98072de80 Mon Sep 17 00:00:00 2001 From: Loay Gewily Date: Fri, 13 Jan 2017 17:45:04 -0500 Subject: [PATCH] Fix replaceById unfound id bug --- lib/sql.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/sql.js b/lib/sql.js index 1a02407..1f157ef 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -690,9 +690,23 @@ SQLConnector.prototype.update = function(model, where, data, options, cb) { */ SQLConnector.prototype._replace = function(model, where, data, options, cb) { 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) { var self = this; this.execute(sql, params, options, function(err, info) {