diff --git a/lib/datasource.js b/lib/datasource.js index 34f40e0b..1f3846c5 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter; var util = require('util'); var assert = require('assert'); var async = require('async'); +var traverse = require('traverse'); if (process.env.DEBUG === 'loopback') { // For back-compatibility @@ -396,12 +397,9 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { // Check if the through model doesn't exist or resolved if (!throughModel || isModelDataSourceAttached(throughModel)) { // The target model is resolved - var params = { - foreignKey: relation.foreignKey, - as: name, - model: model, - options: relation.options - }; + var params = traverse(relation).clone(); + params.as = name; + params.model = model; if (throughModel) { params.through = throughModel; } @@ -415,13 +413,10 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { throughModel.once('dataSourceAttached', function (model) { if (isModelDataSourceAttached(targetModel)) { // The target model is resolved - var params = { - foreignKey: relation.foreignKey, - as: name, - model: targetModel, - through: model, - options: relation.options - }; + var params = traverse(relations).clone(); + params.as = name; + params.model = targetModel; + params.through = model; modelClass[relation.type].call(modelClass, name, params); } }); @@ -438,17 +433,14 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { if (r.through) { throughModel = isModelClass(r.through) ? r.through : this.getModel(r.through, true); } - if (!isModelDataSourceAttached(targetModel) || (throughModel && !isModelDataSourceAttached(throughModel))) { + if ((!r.polymorphic && !isModelDataSourceAttached(targetModel)) || (throughModel && !isModelDataSourceAttached(throughModel))) { // Create a listener to defer the relation set up createListener(rn, r, targetModel, throughModel); } else { // The target model is resolved - var params = { - foreignKey: r.foreignKey, - as: rn, - model: targetModel, - options: r.options - }; + var params = traverse(r).clone(); + params.as = rn; + params.model = targetModel; if (throughModel) { params.through = throughModel; }