From 06f2b32c21c669cf93bb70194edba1c8945873e4 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Tue, 29 Jul 2014 21:56:59 +0200 Subject: [PATCH] Renamed EmbedsMany 'reference' option to 'belongsTo' --- lib/relation-definition.js | 34 +++++++++++++++++----------------- test/relations.test.js | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 96c18c32..bbafb8d0 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1794,27 +1794,27 @@ EmbedsMany.prototype.add = function (acInst, data, cb) { var modelInstance = this.modelInstance; var options = definition.options; - var referenceDef = options.reference && modelTo.relations[options.reference]; + var belongsTo = options.belongsTo && modelTo.relations[options.belongsTo]; - if (!referenceDef) { - throw new Error('Invalid reference: ' + options.reference || '(none)'); + if (!belongsTo) { + throw new Error('Invalid reference: ' + options.belongsTo || '(none)'); } - var fk2 = referenceDef.keyTo; - var pk2 = referenceDef.modelTo.definition.idName() || 'id'; + var fk2 = belongsTo.keyTo; + var pk2 = belongsTo.modelTo.definition.idName() || 'id'; var query = {}; - query[fk2] = (acInst instanceof referenceDef.modelTo) ? acInst[pk2] : acInst; + query[fk2] = (acInst instanceof belongsTo.modelTo) ? acInst[pk2] : acInst; var filter = { where: query }; - referenceDef.applyScope(modelInstance, filter); + belongsTo.applyScope(modelInstance, filter); - referenceDef.modelTo.findOne(filter, function(err, ref) { - if (ref instanceof referenceDef.modelTo) { + belongsTo.modelTo.findOne(filter, function(err, ref) { + if (ref instanceof belongsTo.modelTo) { var inst = self.build(data || {}); - inst[options.reference](ref); + inst[options.belongsTo](ref); modelInstance.save(function(err) { cb(err, err ? null : inst); }); @@ -1835,22 +1835,22 @@ EmbedsMany.prototype.remove = function (acInst, cb) { var modelInstance = this.modelInstance; var options = definition.options; - var referenceDef = options.reference && modelTo.relations[options.reference]; + var belongsTo = options.belongsTo && modelTo.relations[options.belongsTo]; - if (!referenceDef) { - throw new Error('Invalid reference: ' + options.reference || '(none)'); + if (!belongsTo) { + throw new Error('Invalid reference: ' + options.belongsTo || '(none)'); } - var fk2 = referenceDef.keyTo; - var pk2 = referenceDef.modelTo.definition.idName() || 'id'; + var fk2 = belongsTo.keyTo; + var pk2 = belongsTo.modelTo.definition.idName() || 'id'; var query = {}; - query[fk2] = (acInst instanceof referenceDef.modelTo) ? acInst[pk2] : acInst; + query[fk2] = (acInst instanceof belongsTo.modelTo) ? acInst[pk2] : acInst; var filter = { where: query }; - referenceDef.applyScope(modelInstance, filter); + belongsTo.applyScope(modelInstance, filter); modelInstance[definition.accessor](filter, function(err, items) { if (err) return cb(err); diff --git a/test/relations.test.js b/test/relations.test.js index fb26a40c..9319a2f6 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1562,7 +1562,7 @@ describe('relations', function () { Category.embedsMany(Link, { as: 'items', // rename scope: { include: 'product' }, // always include - options: { reference: 'product' } // optional, for add()/remove() + options: { belongsTo: 'product' } // optional, for add()/remove() }); Link.belongsTo(Product, { foreignKey: 'id', // re-use the actual product id