Allow customizing embedded relation property (#1513)
This commit is contained in:
parent
7373476fd8
commit
1d8603c491
|
@ -2045,7 +2045,10 @@ RelationDefinition.embedsOne = function(modelFrom, modelToRef, params) {
|
||||||
methods: params.methods,
|
methods: params.methods,
|
||||||
});
|
});
|
||||||
|
|
||||||
var opts = {type: modelTo};
|
var opts = Object.assign(
|
||||||
|
params.options && params.options.property ? params.options.property : {},
|
||||||
|
{type: modelTo}
|
||||||
|
);
|
||||||
|
|
||||||
if (params.default === true) {
|
if (params.default === true) {
|
||||||
opts.default = function() { return new modelTo(); };
|
opts.default = function() { return new modelTo(); };
|
||||||
|
@ -2441,9 +2444,14 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelToRef, param
|
||||||
embed: true,
|
embed: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
modelFrom.dataSource.defineProperty(modelFrom.modelName, propertyName, {
|
var opts = Object.assign(
|
||||||
type: [modelTo], default: function() { return []; },
|
params.options && params.options.property ? params.options.property : {},
|
||||||
});
|
{
|
||||||
|
type: [modelTo], default: function() { return []; },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
modelFrom.dataSource.defineProperty(modelFrom.modelName, propertyName, opts);
|
||||||
|
|
||||||
if (typeof modelTo.dataSource.connector.generateId !== 'function') {
|
if (typeof modelTo.dataSource.connector.generateId !== 'function') {
|
||||||
modelFrom.validate(propertyName, function(err) {
|
modelFrom.validate(propertyName, function(err) {
|
||||||
|
|
|
@ -4283,13 +4283,20 @@ describe('relations', function() {
|
||||||
);
|
);
|
||||||
Address = tmp.define('Address', {street: String}, {idInjection: false});
|
Address = tmp.define('Address', {street: String}, {idInjection: false});
|
||||||
Other = db.define('Other', {name: String});
|
Other = db.define('Other', {name: String});
|
||||||
});
|
|
||||||
|
|
||||||
it('can be declared using embedsOne method', function(done) {
|
|
||||||
Person.embedsOne(Passport, {
|
Person.embedsOne(Passport, {
|
||||||
default: {name: 'Anonymous'}, // a bit contrived
|
default: {name: 'Anonymous'}, // a bit contrived
|
||||||
methods: {check: function() { return true; }},
|
methods: {check: function() { return true; }},
|
||||||
|
options: {
|
||||||
|
property: {
|
||||||
|
postgresql: {
|
||||||
|
columnName: 'passport_item',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can be declared using embedsOne method', function(done) {
|
||||||
Person.embedsOne(Address); // all by default
|
Person.embedsOne(Address); // all by default
|
||||||
db.automigrate(['Person'], done);
|
db.automigrate(['Person'], done);
|
||||||
});
|
});
|
||||||
|
@ -4303,6 +4310,11 @@ describe('relations', function() {
|
||||||
p.passportItem.destroy.should.be.a.function;
|
p.passportItem.destroy.should.be.a.function;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('respects property options on the embedded property', function() {
|
||||||
|
Person.definition.properties.passport.should.have.property('postgresql');
|
||||||
|
Person.definition.properties.passport.postgresql.should.eql({columnName: 'passport_item'});
|
||||||
|
});
|
||||||
|
|
||||||
it('should setup a custom method on accessor', function() {
|
it('should setup a custom method on accessor', function() {
|
||||||
var rel = Person.relations['passportItem'];
|
var rel = Person.relations['passportItem'];
|
||||||
rel.defineMethod('other', function() {
|
rel.defineMethod('other', function() {
|
||||||
|
@ -4704,7 +4716,15 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can be declared', function(done) {
|
it('can be declared', function(done) {
|
||||||
Person.embedsMany(Address);
|
Person.embedsMany(Address, {
|
||||||
|
options: {
|
||||||
|
property: {
|
||||||
|
postgresql: {
|
||||||
|
dataType: 'json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
db.automigrate(['Person'], done);
|
db.automigrate(['Person'], done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -4733,6 +4753,11 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('respects property options on the embedded property', function() {
|
||||||
|
Person.definition.properties.addresses.should.have.property('postgresql');
|
||||||
|
Person.definition.properties.addresses.postgresql.should.eql({dataType: 'json'});
|
||||||
|
});
|
||||||
|
|
||||||
it('should create embedded items on scope', function(done) {
|
it('should create embedded items on scope', function(done) {
|
||||||
Person.findOne(function(err, p) {
|
Person.findOne(function(err, p) {
|
||||||
p.addressList.create({street: 'Street 2'}, function(err, address) {
|
p.addressList.create({street: 'Street 2'}, function(err, address) {
|
||||||
|
|
Loading…
Reference in New Issue