Return count when updating or deleting models

This commit is contained in:
Simon Ho 2015-03-24 23:06:40 -07:00
parent 71fda816ba
commit ff538b09c8
1 changed files with 15 additions and 2 deletions

View File

@ -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));
};