From e441924fa3bc74cc4d5c98c149a77818edb979de Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Sun, 7 Sep 2014 13:24:05 +0200 Subject: [PATCH] Allow embedsOne to use auto-generated id (from connector) If the connector has a `generateId` method - like Transient - it can be used to set the id (when the property has been set to `generated: true`). --- lib/relation-definition.js | 16 +++++++++++++++- test/relations.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 54474635..0637ef9f 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1803,11 +1803,25 @@ EmbedsOne.prototype.build = function (targetModelData) { var modelTo = this.definition.modelTo; var modelInstance = this.modelInstance; var propertyName = this.definition.keyFrom; + var forceId = this.definition.options.forceId; + var persistent = this.definition.options.persistent; + var connector = modelTo.dataSource.connector; targetModelData = targetModelData || {}; this.definition.applyProperties(modelInstance, targetModelData); + var pk = this.definition.keyTo; + var pkProp = modelTo.definition.properties[pk]; + + var assignId = (forceId || targetModelData[pk] === undefined); + assignId = assignId && !persistent && (pkProp && pkProp.generated); + + if (assignId && typeof connector.generateId === 'function') { + var id = connector.generateId(modelTo.modelName, targetModelData, pk); + targetModelData[pk] = id; + } + var embeddedInstance = new modelTo(targetModelData); modelInstance[propertyName] = embeddedInstance; @@ -2241,7 +2255,7 @@ EmbedsMany.prototype.build = function(targetModelData) { var connector = modelTo.dataSource.connector; var pk = this.definition.keyTo; - var pkProp = modelTo.definition.properties[pk] + var pkProp = modelTo.definition.properties[pk]; var pkType = pkProp && pkProp.type; var embeddedList = this.embeddedList(); diff --git a/test/relations.test.js b/test/relations.test.js index 7e64cd7e..57987a76 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1877,6 +1877,37 @@ describe('relations', function () { }); + describe('embedsOne - generated id', function () { + + before(function () { + tmp = getTransientDataSource(); + db = getSchema(); + Person = db.define('Person', {name: String}); + Passport = tmp.define('Passport', + {id: {type:'string', id: true, generated:true}}, + {name: {type:'string', required: true}} + ); + }); + + it('can be declared using embedsOne method', function (done) { + Person.embedsOne(Passport); + db.automigrate(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); + passport.id.should.match(/^[0-9a-fA-F]{24}$/); + p.passport.name.should.equal('Fredric'); + done(); + }); + }); + }); + + }); + describe('embedsMany', function () { var address1, address2;