From 5fdd521985ae66caa12d6652824b627c34f4a577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 18 Dec 2015 15:59:19 +0100 Subject: [PATCH] Enhance "persisted" hook in DAO.updateAttributes Add `isNewInstance:false` to the context reported by DAO.updateAttributes() --- lib/dao.js | 1 + test/persistence-hooks.suite.js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 4b6fefa2..7a2aaf14 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -2548,6 +2548,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op where: byIdQuery(Model, getIdValue(Model, inst)).where, data: context.data, currentInstance: inst, + isNewInstance: false, hookState: hookState, options: options }; diff --git a/test/persistence-hooks.suite.js b/test/persistence-hooks.suite.js index 5c7dce5f..452472f6 100644 --- a/test/persistence-hooks.suite.js +++ b/test/persistence-hooks.suite.js @@ -1210,7 +1210,8 @@ module.exports = function(dataSource, should) { id: existingInstance.id, name: 'changed', extra: null - } + }, + isNewInstance: false })); done(); @@ -1685,7 +1686,7 @@ module.exports = function(dataSource, should) { function(err, instance) { if (err) return done(err); - observedContexts.should.eql(aTestModelCtx({ + var expectedContext = aTestModelCtx({ where: { id: existingInstance.id }, data: { id: existingInstance.id, @@ -1696,7 +1697,15 @@ module.exports = function(dataSource, should) { name: 'updated name', extra: undefined } - })); + }); + + if (!dataSource.connector.updateOrCreate) { + // When the connector does not provide updateOrCreate, + // DAO falls back to updateAttributes which sets this flag + expectedContext.isNewInstance = false; + } + + observedContexts.should.eql(expectedContext); done(); }); });