rymac
rymac

Reputation: 1

MySQL many-to-many relationship in LoopBack

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?

Upvotes: 0

Views: 1271

Answers (1)

Raymond Feng
Raymond Feng

Reputation: 1536

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.

Upvotes: 1

Related Questions