From 4b70f2498f38ff15af69dbe40edfc6691555a996 Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Mon, 1 Sep 2014 23:59:52 +0800 Subject: [PATCH] Fix options for hasManyThrough doesn't apply Signed-off-by: Clark Wang --- lib/datasource.js | 2 +- test/loopback-dl.test.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/datasource.js b/lib/datasource.js index 6f3a088c..19522e29 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -417,7 +417,7 @@ DataSource.prototype.defineRelations = function (modelClass, relations) { throughModel.once('dataAccessConfigured', function (model) { if (isModelDataSourceAttached(targetModel)) { // The target model is resolved - var params = traverse(relations).clone(); + var params = traverse(relation).clone(); params.as = name; params.model = targetModel; params.through = model; diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 940c9a79..bb02bfcf 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -1110,6 +1110,27 @@ describe('Load models with relations', function () { done(); }); + it('should handle hasMany through options', function (done) { + var ds = new DataSource('memory'); + var Physician = ds.createModel('Physician', { + name: String + }, {relations: {patients: {model: 'Patient', type: 'hasMany', foreignKey: 'leftId', through: 'Appointment'}}}); + + var Patient = ds.createModel('Patient', { + name: String + }, {relations: {physicians: {model: 'Physician', type: 'hasMany', foreignKey: 'rightId', through: 'Appointment'}}}); + + var Appointment = ds.createModel('Appointment', { + physicianId: Number, + patientId: Number, + appointmentDate: Date + }, {relations: {patient: {type: 'belongsTo', model: 'Patient'}, physician: {type: 'belongsTo', model: 'Physician'}}}); + + assert(Physician.relations['patients'].keyTo === 'leftId'); + assert(Patient.relations['physicians'].keyTo === 'rightId'); + done(); + }); + it('should set up relations after attach', function (done) { var ds = new DataSource('memory'); var modelBuilder = new ModelBuilder();