Renamed EmbedsMany 'reference' option to 'belongsTo'
This commit is contained in:
parent
1782b439f1
commit
06f2b32c21
|
@ -1794,27 +1794,27 @@ EmbedsMany.prototype.add = function (acInst, data, cb) {
|
||||||
var modelInstance = this.modelInstance;
|
var modelInstance = this.modelInstance;
|
||||||
|
|
||||||
var options = definition.options;
|
var options = definition.options;
|
||||||
var referenceDef = options.reference && modelTo.relations[options.reference];
|
var belongsTo = options.belongsTo && modelTo.relations[options.belongsTo];
|
||||||
|
|
||||||
if (!referenceDef) {
|
if (!belongsTo) {
|
||||||
throw new Error('Invalid reference: ' + options.reference || '(none)');
|
throw new Error('Invalid reference: ' + options.belongsTo || '(none)');
|
||||||
}
|
}
|
||||||
|
|
||||||
var fk2 = referenceDef.keyTo;
|
var fk2 = belongsTo.keyTo;
|
||||||
var pk2 = referenceDef.modelTo.definition.idName() || 'id';
|
var pk2 = belongsTo.modelTo.definition.idName() || 'id';
|
||||||
|
|
||||||
var query = {};
|
var query = {};
|
||||||
|
|
||||||
query[fk2] = (acInst instanceof referenceDef.modelTo) ? acInst[pk2] : acInst;
|
query[fk2] = (acInst instanceof belongsTo.modelTo) ? acInst[pk2] : acInst;
|
||||||
|
|
||||||
var filter = { where: query };
|
var filter = { where: query };
|
||||||
|
|
||||||
referenceDef.applyScope(modelInstance, filter);
|
belongsTo.applyScope(modelInstance, filter);
|
||||||
|
|
||||||
referenceDef.modelTo.findOne(filter, function(err, ref) {
|
belongsTo.modelTo.findOne(filter, function(err, ref) {
|
||||||
if (ref instanceof referenceDef.modelTo) {
|
if (ref instanceof belongsTo.modelTo) {
|
||||||
var inst = self.build(data || {});
|
var inst = self.build(data || {});
|
||||||
inst[options.reference](ref);
|
inst[options.belongsTo](ref);
|
||||||
modelInstance.save(function(err) {
|
modelInstance.save(function(err) {
|
||||||
cb(err, err ? null : inst);
|
cb(err, err ? null : inst);
|
||||||
});
|
});
|
||||||
|
@ -1835,22 +1835,22 @@ EmbedsMany.prototype.remove = function (acInst, cb) {
|
||||||
var modelInstance = this.modelInstance;
|
var modelInstance = this.modelInstance;
|
||||||
|
|
||||||
var options = definition.options;
|
var options = definition.options;
|
||||||
var referenceDef = options.reference && modelTo.relations[options.reference];
|
var belongsTo = options.belongsTo && modelTo.relations[options.belongsTo];
|
||||||
|
|
||||||
if (!referenceDef) {
|
if (!belongsTo) {
|
||||||
throw new Error('Invalid reference: ' + options.reference || '(none)');
|
throw new Error('Invalid reference: ' + options.belongsTo || '(none)');
|
||||||
}
|
}
|
||||||
|
|
||||||
var fk2 = referenceDef.keyTo;
|
var fk2 = belongsTo.keyTo;
|
||||||
var pk2 = referenceDef.modelTo.definition.idName() || 'id';
|
var pk2 = belongsTo.modelTo.definition.idName() || 'id';
|
||||||
|
|
||||||
var query = {};
|
var query = {};
|
||||||
|
|
||||||
query[fk2] = (acInst instanceof referenceDef.modelTo) ? acInst[pk2] : acInst;
|
query[fk2] = (acInst instanceof belongsTo.modelTo) ? acInst[pk2] : acInst;
|
||||||
|
|
||||||
var filter = { where: query };
|
var filter = { where: query };
|
||||||
|
|
||||||
referenceDef.applyScope(modelInstance, filter);
|
belongsTo.applyScope(modelInstance, filter);
|
||||||
|
|
||||||
modelInstance[definition.accessor](filter, function(err, items) {
|
modelInstance[definition.accessor](filter, function(err, items) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
|
@ -1562,7 +1562,7 @@ describe('relations', function () {
|
||||||
Category.embedsMany(Link, {
|
Category.embedsMany(Link, {
|
||||||
as: 'items', // rename
|
as: 'items', // rename
|
||||||
scope: { include: 'product' }, // always include
|
scope: { include: 'product' }, // always include
|
||||||
options: { reference: 'product' } // optional, for add()/remove()
|
options: { belongsTo: 'product' } // optional, for add()/remove()
|
||||||
});
|
});
|
||||||
Link.belongsTo(Product, {
|
Link.belongsTo(Product, {
|
||||||
foreignKey: 'id', // re-use the actual product id
|
foreignKey: 'id', // re-use the actual product id
|
||||||
|
|
Loading…
Reference in New Issue