From 21e1083e88fc53b2c4489d446c6ade783615d3ec Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Sun, 7 Sep 2014 13:10:23 +0200 Subject: [PATCH] Implemented persistent: true option for embedsOne --- lib/relation-definition.js | 41 ++++++++++++++++++++++------------- test/relations.test.js | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index dbfc4af5..54474635 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1775,18 +1775,28 @@ EmbedsOne.prototype.create = function (targetModelData, cb) { var inst = this.build(targetModelData); - var err = inst.isValid() ? null : new ValidationError(inst); - - if (err) { - return process.nextTick(function() { - cb(err); + var updateEmbedded = function() { + modelInstance.updateAttribute(propertyName, + inst, function(err) { + cb(err, err ? null : inst); }); - } + }; - modelInstance.updateAttribute(propertyName, - inst, function(err) { - cb(err, err ? null : inst); - }); + if (this.definition.options.persistent) { + inst.save(function(err) { // will validate + if (err) return cb(err, inst); + updateEmbedded(); + }); + } else { + var err = inst.isValid() ? null : new ValidationError(inst); + if (err) { + process.nextTick(function() { + cb(err); + }); + } else { + updateEmbedded(); + } + } }; EmbedsOne.prototype.build = function (targetModelData) { @@ -2199,26 +2209,27 @@ EmbedsMany.prototype.create = function (targetModelData, cb) { var inst = this.build(targetModelData); - var updateEmbeddedList = function() { + var updateEmbedded = function() { modelInstance.updateAttribute(propertyName, embeddedList, function(err, modelInst) { cb(err, err ? null : inst); }); - } + }; if (this.definition.options.persistent) { inst.save(function(err) { // will validate if (err) return cb(err, inst); - updateEmbeddedList(); + updateEmbedded(); }); } else { var err = inst.isValid() ? null : new ValidationError(inst); if (err) { - return process.nextTick(function() { + process.nextTick(function() { cb(err); }); + } else { + updateEmbedded(); } - updateEmbeddedList(); } }; diff --git a/test/relations.test.js b/test/relations.test.js index cd131762..7e64cd7e 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1833,6 +1833,50 @@ describe('relations', function () { }); + describe('embedsOne - persisted model', function () { + + // This test spefically uses the Memory connector + // in order to test the use of the auto-generated + // id, in the sequence of the related model. + + before(function () { + db = getMemoryDataSource(); + Person = db.define('Person', {name: String}); + Passport = db.define('Passport', + {name:{type:'string', required: true}} + ); + }); + + it('can be declared using embedsOne method', function (done) { + Person.embedsOne(Passport, { + options: {persistent: true} + }); + db.automigrate(done); + }); + + it('should create an item - to offset id', function(done) { + Passport.create({name:'Wilma'}, function(err, p) { + should.not.exist(err); + p.id.should.equal(1); + p.name.should.equal('Wilma'); + done(); + }); + }); + + it('should create an embedded item on scope', function(done) { + Person.create({name: 'Fred'}, function(err, p) { + should.not.exist(err); + p.passportItem.create({name: 'Fredric'}, function(err, passport) { + should.not.exist(err); + p.passport.id.should.eql(2); + p.passport.name.should.equal('Fredric'); + done(); + }); + }); + }); + + }); + describe('embedsMany', function () { var address1, address2;