Merge pull request #91 from strongloop/feature/fix-isNewInstance
Return isNewInstance from upsert
This commit is contained in:
commit
2a249beb77
12
lib/mysql.js
12
lib/mysql.js
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue