From ff538b09c8cf0d0f3298a58362185501d84f2867 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Tue, 24 Mar 2015 23:06:40 -0700 Subject: [PATCH] Return count when updating or deleting models --- lib/mysql.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 23ae922..ca54630 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -641,6 +641,17 @@ MySQL.prototype.count = function count(model, callback, where) { }; +MySQL.prototype.update = +MySQL.prototype.updateAll = function(model, where, data, cb) { + var query = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + + this.toFields(model, data) + ' ' + this.buildWhere(model, where); + this.query(query, function(err, info) { + var affectedRows = info && typeof info.affectedRows === 'number' ? + info.affectedRows : undefined; + cb && cb(err, {count: affectedRows}); + }); +}; + /** * Delete instances for the given model @@ -657,8 +668,10 @@ MySQL.prototype.destroyAll = function destroyAll(model, where, callback) { } this.query('DELETE FROM ' + this.tableEscaped(model) + ' ' + this.buildWhere(model, where || {}), - function (err, data) { - callback && callback(err, data); + function(err, info) { + var affectedRows = info && typeof info.affectedRows === 'number' ? + info.affectedRows : undefined; + callback && callback(err, {count: affectedRows}); }.bind(this)); };