From f6e4c228eb07a7b1a1a91eb287dfe5aff0e36b31 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 1 Apr 2015 16:25:23 -0700 Subject: [PATCH] Return isNewInstance from upsert --- lib/mysql.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index ca54630..5e656e2 100644 --- a/lib/mysql.js +++ b/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); }); };