Merge pull request #91 from strongloop/feature/fix-isNewInstance

Return isNewInstance from upsert
This commit is contained in:
Raymond Feng 2015-04-02 08:20:38 -07:00
commit 2a249beb77
1 changed files with 10 additions and 2 deletions

View File

@ -254,11 +254,19 @@ MySQL.prototype.updateOrCreate = MySQL.prototype.save = function (model, data, c
sql += ' VALUES (' + fieldValues.join(', ') + ')';
sql += ' ON DUPLICATE KEY UPDATE ' + combined.join(', ');
this.query(sql, function (err, info) {
this.query(sql, function(err, info) {
if (!err && info && info.insertId) {
data.id = info.insertId;
}
callback(err, data);
var meta = {};
if (info) {
// When using the INSERT ... ON DUPLICATE KEY UPDATE statement,
// the returned value is as follows:
// 1 for each successful INSERT.
// 2 for each successful UPDATE.
meta.isNewInstance = (info.affectedRows === 1);
}
callback(err, data, meta);
});
};