From 902772f1af50b409ba58a3f3043cd8d6f3d9ac14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 16 Mar 2015 11:26:57 +0100 Subject: [PATCH 1/2] Reject CREATE with a duplicate id Modify the memory connector to reject requests to create a record with a duplicate id. Add a unit-test to verify this behaviour across all connectors. --- lib/connectors/memory.js | 7 +++++++ test/manipulation.test.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 91c00263..ed7a581b 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -210,6 +210,13 @@ Memory.prototype.create = function create(model, data, callback) { if(!this.collection(model)) { this.collection(model, {}); } + + if (this.collection(model)[id]) { + return process.nextTick(function() { + callback(new Error('Duplicate entry for ' + model + '.' + idName)); + }); + } + this.collection(model)[id] = serialize(data); this.saveToFile(id, callback); }; diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 77311069..875ccf18 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -291,6 +291,25 @@ describe('manipulation', function () { }); }); + it('should refuse to create object with duplicate id', function(done) { + // NOTE(bajtos) We cannot reuse Person model here, + // `settings.forceId` aborts the CREATE request at the validation step. + var Product = db.define('Product', { name: String }); + db.automigrate(function(err) { + if (err) return done(err); + + Product.create({ name: 'a-name' }, function(err, p) { + if (err) return done(err); + Product.create({ id: p.id, name: 'duplicate' }, function(err) { + if (!err) { + return done(new Error('Create should have rejected duplicate id.')); + } + err.message.should.match(/duplicate/i); + done(); + }); + }); + }); + }); }); describe('save', function () { From 6f11c2d7172cdae58e066a5b43b0effec1a7d6bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 16 Mar 2015 12:36:54 +0100 Subject: [PATCH 2/2] DAO: Fix updateOrCreate to set persisted:true Before this commit, the following code would not work: Change.updateOrCreate({...}, function(err, ch) { // somewhere later, modify "ch" and save the changes ch.save(cb); }); --- lib/dao.js | 2 +- test/manipulation.test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index 50a0c273..35a79244 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -393,7 +393,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data function done(err, data) { var obj; if (data && !(data instanceof Model)) { - inst._initProperties(data); + inst._initProperties(data, { persisted: true }); obj = inst; } else { obj = data; diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 875ccf18..16eddd97 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -567,6 +567,15 @@ describe('manipulation', function () { }); }); }); + + it('should allow save() of the created instance', function(done) { + Person.updateOrCreate( + { id: 'new-id', name: 'a-name' }, + function(err, inst) { + if (err) return done(err); + inst.save(done); + }); + }); }); describe('findOrCreate', function() {