From fec7742a6906971a7d13442725ed0eab27e350c1 Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Fri, 16 Sep 2016 20:37:24 -0400 Subject: [PATCH] Add 'isNewInstance' for updateAttributes * Add 'isNewInstance' to loaded hook for updateAttributes --- lib/dao.js | 1 + test/memory.test.js | 1 + test/persistence-hooks.suite.js | 34 +++++++++------------------------ 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index edd93905..adefe7df 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -3093,6 +3093,7 @@ function(data, options, cb) { data: context.data, hookState: hookState, options: options, + isNewInstance: false, }; Model.notifyObserversOf('loaded', ctx, function(err) { if (err) return cb(err); diff --git a/test/memory.test.js b/test/memory.test.js index e3f18cda..991159d4 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -901,6 +901,7 @@ describe('Unoptimized connector', function() { // disable optimized methods ds.connector.updateOrCreate = false; ds.connector.findOrCreate = false; + ds.connector.upsertWithWhere = false; require('./persistence-hooks.suite')(ds, should, { replaceOrCreateReportsNewInstance: true, diff --git a/test/persistence-hooks.suite.js b/test/persistence-hooks.suite.js index a260a142..ab7158bd 100644 --- a/test/persistence-hooks.suite.js +++ b/test/persistence-hooks.suite.js @@ -1360,6 +1360,7 @@ module.exports = function(dataSource, should, connectorCapabilities) { ctxRecorder.records.should.eql(aCtxForModel(TestModel, { data: {name: 'changed'}, + isNewInstance: false, })); done(); @@ -2039,25 +2040,13 @@ module.exports = function(dataSource, should, connectorCapabilities) { {id: existingInstance.id, name: 'updated name'}, function(err, instance) { if (err) return done(err); - - if (dataSource.connector.updateOrCreate) { - ctxRecorder.records.should.eql(aCtxForModel(TestModel, { - data: { - id: existingInstance.id, - name: 'updated name', - }, - isNewInstance: false, - })); - } else { - ctxRecorder.records.should.eql( - aCtxForModel(TestModel, { - data: { - id: existingInstance.id, - name: 'updated name', - }, - }) - ); - } + ctxRecorder.records.should.eql(aCtxForModel(TestModel, { + data: { + id: existingInstance.id, + name: 'updated name', + }, + isNewInstance: false, + })); done(); }); }); @@ -3267,13 +3256,8 @@ module.exports = function(dataSource, should, connectorCapabilities) { id: existingInstance.id, name: 'updated name', }, + isNewInstance: false, }); - // For non-atomic implementation of upsertWithWhere on update, it calls - // updateAttributes. loaded hook of updateAttributes does not provide - // isNewInstance. - if (dataSource.connector.upsertWithWhere) { - expectedContext.isNewInstance = false; - } ctxRecorder.records.should.eql(aCtxForModel(TestModel, expectedContext)); done(); });