Merge branch 'fabien-feature/dynamic-scope'
This commit is contained in:
commit
be1a53482a
|
@ -819,6 +819,8 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
|
|||
keyFrom: fk,
|
||||
keyTo: idName,
|
||||
modelTo: modelTo,
|
||||
properties: params.properties,
|
||||
scope: params.scope,
|
||||
options: params.options
|
||||
});
|
||||
|
||||
|
@ -851,8 +853,8 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
|
|||
BelongsTo.prototype.create = function(targetModelData, cb) {
|
||||
var self = this;
|
||||
var modelTo = this.definition.modelTo;
|
||||
var fk = this.definition.keyTo;
|
||||
var pk = this.definition.keyFrom;
|
||||
var fk = this.definition.keyFrom;
|
||||
var pk = this.definition.keyTo;
|
||||
var modelInstance = this.modelInstance;
|
||||
|
||||
if (typeof targetModelData === 'function' && !cb) {
|
||||
|
@ -860,6 +862,8 @@ BelongsTo.prototype.create = function(targetModelData, cb) {
|
|||
targetModelData = {};
|
||||
}
|
||||
|
||||
this.definition.applyProperties(modelInstance, targetModelData || {});
|
||||
|
||||
modelTo.create(targetModelData, function(err, targetModel) {
|
||||
if(!err) {
|
||||
modelInstance[fk] = targetModel[pk];
|
||||
|
@ -873,6 +877,7 @@ BelongsTo.prototype.create = function(targetModelData, cb) {
|
|||
|
||||
BelongsTo.prototype.build = function(targetModelData) {
|
||||
var modelTo = this.definition.modelTo;
|
||||
this.definition.applyProperties(this.modelInstance, targetModelData || {});
|
||||
return new modelTo(targetModelData);
|
||||
};
|
||||
|
||||
|
@ -911,7 +916,12 @@ BelongsTo.prototype.related = function (refresh, params) {
|
|||
} else if (typeof params === 'function') { // acts as async getter
|
||||
var cb = params;
|
||||
if (cachedValue === undefined) {
|
||||
modelTo.findById(modelInstance[fk], function (err, inst) {
|
||||
var query = {where: {}};
|
||||
query.where[pk] = modelInstance[fk];
|
||||
|
||||
this.definition.applyScope(modelInstance, query);
|
||||
|
||||
modelTo.findOne(query, function (err, inst) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
@ -919,7 +929,8 @@ BelongsTo.prototype.related = function (refresh, params) {
|
|||
return cb(null, null);
|
||||
}
|
||||
// Check if the foreign key matches the primary key
|
||||
if (inst[pk] === modelInstance[fk]) {
|
||||
if (inst[pk] && modelInstance[fk]
|
||||
&& inst[pk].toString() === modelInstance[fk].toString()) {
|
||||
self.resetCache(inst);
|
||||
cb(null, inst);
|
||||
} else {
|
||||
|
@ -1193,7 +1204,8 @@ HasOne.prototype.related = function (refresh, params) {
|
|||
return cb(null, null);
|
||||
}
|
||||
// Check if the foreign key matches the primary key
|
||||
if (inst[fk] === modelInstance[pk]) {
|
||||
if (inst[fk] && modelInstance[pk]
|
||||
&& inst[fk].toString() === modelInstance[pk].toString()) {
|
||||
self.resetCache(inst);
|
||||
cb(null, inst);
|
||||
} else {
|
||||
|
|
|
@ -404,7 +404,7 @@ describe('relations', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('hasMany with scope', function () {
|
||||
describe('hasMany with scope and properties', function () {
|
||||
it('can be declared with properties', function (done) {
|
||||
db = getSchema();
|
||||
Category = db.define('Category', {name: String, productType: String});
|
||||
|
@ -556,6 +556,45 @@ describe('relations', function () {
|
|||
});
|
||||
|
||||
});
|
||||
|
||||
describe('belongsTo with scope', function () {
|
||||
var Person, Passport;
|
||||
|
||||
it('can be declared with scope and properties', function (done) {
|
||||
Person = db.define('Person', {name: String, age: Number});
|
||||
Passport = db.define('Passport', {name: String, notes: String});
|
||||
Passport.belongsTo(Person, {
|
||||
properties: { notes: 'passportNotes' },
|
||||
scope: { fields: { id: true, name: true } }
|
||||
});
|
||||
db.automigrate(done);
|
||||
});
|
||||
|
||||
it('should create record on scope', function (done) {
|
||||
var p = new Passport({ name: 'Passport', notes: 'Some notes...' });
|
||||
p.person.create({ id: 3, name: 'Fred', age: 36 }, function(err, person) {
|
||||
p.personId.should.equal(person.id);
|
||||
p.save(function (err, p) {
|
||||
person.name.should.equal('Fred');
|
||||
person.passportNotes.should.equal('Some notes...');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should find record on scope', function (done) {
|
||||
Passport.findOne(function (err, p) {
|
||||
p.personId.should.equal(3);
|
||||
p.person(function(err, person) {
|
||||
person.name.should.equal('Fred');
|
||||
person.should.not.have.property('age');
|
||||
person.should.not.have.property('passportNotes');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('hasOne', function () {
|
||||
var Supplier, Account;
|
||||
|
|
Loading…
Reference in New Issue