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`).
This commit is contained in:
parent
21e1083e88
commit
e441924fa3
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue