Skip to content Skip to sidebar Skip to footer

Set The Custom Properties In Joining Model Using LoopBacks HasManyThrough

I am trying to set up a many to many relationship in LoopBack 2.1.2 http://docs.strongloop.com/display/LB/HasManyThrough+relations I tried to POST /api/patients/:patientId/physicia

Solution 1:

Disclaimer: I am a LoopBack developer working for StrongLoop.

Is there one API call to create this in one transaction?

No, there is no such API at the moment.

What is the best way to add a new physician to a patient and setting the appointmentDate?

You have to send two requests: The first one to create a physician (POST /physicians), the second one to create the appointment (POST /appointments).

Alternatively, you can use "Patient hasMany appointments" instead of "Patient hasMany physicians through Appointment", in which case the appointment can be added via

POST /patients/:patientId/appointments`

You will still have to create the physician first.

Do I have to create my own RESTFUL API call?

You can certainly do that, although I personally don't understand why two requests are a problem in this case. The operation "create a new physician with an appointment for the given patient" looks weird to me. Two steps ("create a new physician", and some time later "make an appointment") make more sense to me.

However, if you have a good example where it makes sense to create both records in one request, then please open a github issue in strongloop/loopback to discuss this further.

More info

At the moment, the "hasMany through" relation is tuned for the purpose of "hasAndBelongsToMany" relation, where the "through" model is just a container for the two id properties (foreign keys). That's the reason why the relation methods like POST /api/patients/:patientId/physicians do not support "through" properties like "appointmentDate".

I have created a github issue loopback#466 to discuss how to improve this part of LoopBack, feel free to comment there.

There is also a bug in loopback-explorer (#39), where the UI suggest that POST /patients/{id}/physicians is expecting an Appointment, even though the implementation expects a Physician instead.


Post a Comment for "Set The Custom Properties In Joining Model Using LoopBacks HasManyThrough"