Skip to content

Commit 76398b7

Browse files
authored
Merge pull request #1105 from strongloop/newInstance_updateAttributes_2.x
Add 'isNewInstance' for updateAttributes
2 parents 24eb3d3 + e103b50 commit 76398b7

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

lib/dao.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ DataAccessObject.upsert = function(data, options, cb) {
571571
var context = {
572572
Model: Model,
573573
data: data,
574+
isNewInstance: info && info.isNewInstance,
574575
hookState: ctx.hookState,
575576
options: options,
576577
};
@@ -3100,6 +3101,7 @@ function(data, options, cb) {
31003101
data: context.data,
31013102
hookState: hookState,
31023103
options: options,
3104+
isNewInstance: false,
31033105
};
31043106
Model.notifyObserversOf('loaded', ctx, function(err) {
31053107
if (err) return cb(err);

test/memory.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ describe('Unoptimized connector', function() {
880880
// disable optimized methods
881881
ds.connector.updateOrCreate = false;
882882
ds.connector.findOrCreate = false;
883+
ds.connector.upsertWithWhere = false;
883884

884885
require('./persistence-hooks.suite')(ds, should, {
885886
replaceOrCreateReportsNewInstance: true,

test/persistence-hooks.suite.js

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ module.exports = function(dataSource, should, connectorCapabilities) {
13561356

13571357
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
13581358
data: { name: 'changed' },
1359+
isNewInstance: false,
13591360
}));
13601361

13611362
done();
@@ -2010,19 +2011,13 @@ module.exports = function(dataSource, should, connectorCapabilities) {
20102011
function(err, instance) {
20112012
if (err) return done(err);
20122013

2013-
if (dataSource.connector.updateOrCreate) {
2014-
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-
data: { id: 'new-id', name: 'a name' },
2016-
}));
2017-
} else {
2018-
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2019-
data: {
2020-
id: 'new-id',
2021-
name: 'a name',
2022-
},
2023-
isNewInstance: true,
2024-
}));
2025-
}
2014+
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015+
data: {
2016+
id: 'new-id',
2017+
name: 'a name',
2018+
},
2019+
isNewInstance: true,
2020+
}));
20262021
done();
20272022
});
20282023
});
@@ -2034,24 +2029,13 @@ module.exports = function(dataSource, should, connectorCapabilities) {
20342029
{ id: existingInstance.id, name: 'updated name' },
20352030
function(err, instance) {
20362031
if (err) return done(err);
2037-
2038-
if (dataSource.connector.updateOrCreate) {
2039-
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2040-
data: {
2041-
id: existingInstance.id,
2042-
name: 'updated name',
2043-
},
2044-
}));
2045-
} else {
2046-
ctxRecorder.records.should.eql(
2047-
aCtxForModel(TestModel, {
2048-
data: {
2049-
id: existingInstance.id,
2050-
name: 'updated name',
2051-
},
2052-
})
2053-
);
2054-
}
2032+
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2033+
data: {
2034+
id: existingInstance.id,
2035+
name: 'updated name',
2036+
},
2037+
isNewInstance: false,
2038+
}));
20552039
done();
20562040
});
20572041
});
@@ -3259,13 +3243,8 @@ module.exports = function(dataSource, should, connectorCapabilities) {
32593243
id: existingInstance.id,
32603244
name: 'updated name',
32613245
},
3246+
isNewInstance: false,
32623247
});
3263-
// For non-atomic implementation of upsertWithWhere on update, it calls
3264-
// updateAttributes. loaded hook of updateAttributes does not provide
3265-
// isNewInstance.
3266-
if (dataSource.connector.upsertWithWhere) {
3267-
expectedContext.isNewInstance = false;
3268-
}
32693248
ctxRecorder.records.should.eql(aCtxForModel(TestModel, expectedContext));
32703249
done();
32713250
});

0 commit comments

Comments
 (0)