From e103b50b6397725ce2d1d0cf9f33036944876a6a 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 updateAttes --- lib/dao.js | 2 ++ test/memory.test.js | 1 + test/persistence-hooks.suite.js | 53 ++++++++++----------------------- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index b2f0cd68..95e97836 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -571,6 +571,7 @@ DataAccessObject.upsert = function(data, options, cb) { var context = { Model: Model, data: data, + isNewInstance: info && info.isNewInstance, hookState: ctx.hookState, options: options, }; @@ -3100,6 +3101,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 7312e4d2..8bdf3581 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -880,6 +880,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 5bb56c84..4663bb64 100644 --- a/test/persistence-hooks.suite.js +++ b/test/persistence-hooks.suite.js @@ -1356,6 +1356,7 @@ module.exports = function(dataSource, should, connectorCapabilities) { ctxRecorder.records.should.eql(aCtxForModel(TestModel, { data: { name: 'changed' }, + isNewInstance: false, })); done(); @@ -2010,19 +2011,13 @@ module.exports = function(dataSource, should, connectorCapabilities) { function(err, instance) { if (err) return done(err); - if (dataSource.connector.updateOrCreate) { - ctxRecorder.records.should.eql(aCtxForModel(TestModel, { - data: { id: 'new-id', name: 'a name' }, - })); - } else { - ctxRecorder.records.should.eql(aCtxForModel(TestModel, { - data: { - id: 'new-id', - name: 'a name', - }, - isNewInstance: true, - })); - } + ctxRecorder.records.should.eql(aCtxForModel(TestModel, { + data: { + id: 'new-id', + name: 'a name', + }, + isNewInstance: true, + })); done(); }); }); @@ -2034,24 +2029,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', - }, - })); - } 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(); }); }); @@ -3259,13 +3243,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(); });