0

I'm having trouble getting LoopBack to perform a many-to-many join query. Considering the hasManyThrough example from the documentation:

var Physician = ds.createModel('Physician', {name: String});
var Patient = ds.createModel('Patient', {name: String});

var Appointment = ds.createModel('Appointment', {
    physicianId: Number,
    patientId: Number,
    appointmentDate: Date
});

Appointment.belongsTo(Patient);
Appointment.belongsTo(Physician);

Physician.hasMany(Patient, {through: Appointment});
Patient.hasMany(Physician, {through: Appointment});

If I try to do a single search to find Patients associated with a particular doctor who have a zip code of 10012, I could try:

physician.patients({where: {zip: 10012}}, fn);

However, the search on the physician's patients is actually only searching on the Appointments table. Is there any way to do a simple search that will be performed on the specific physician's patients directly?

Ryann Graham
  • 8,079
  • 2
  • 29
  • 32
rymac
  • 1
  • 3

1 Answers1

1

LoopBack implements the hasMany/through relation for physician.patients() as follows:

Appointment.find ({ where: { physicianId: 1 },
  include: 'patient',
  collect: 'patient' }, callback);

We're considering to support the filter for the 'include' which brings int 'patient' information.

I suggest you open an issue at https://github.com/strongloop/loopback-datasource-juggler/issues.

Raymond Feng
  • 1,516
  • 9
  • 5