ibm-cloudloopbackjsloopbackapiconnect

Loopback - one-to-many relation between 3 models


I'm using IBM API Connect for a Loopback application. I've 3 models - owner, home and room. The relationship is described as follows:

OWNER:

HOME:

In my case, through API Connect CLI, Owner is an REST exposed model (REST resource) and is supposed to be directly related to the Home model via one-to-many relationship. Home & Room are two models which are not exposed to REST. Home is directly related to Room via one-to-many relationship. Owner and Room are not directly related to each other but is related via Home model.

I want to get the HTTP URL in these forms:

/owners/{id}/homes/{id}/rooms/{id}   

to get details of a room for a particular home of the owner

I've chosed one-to-many relationship for Owner-Home and Home-Room with respective foreign key. Since its not a many-to-many relationship, I did not use any through model.

The URLs that I'm getting doesn't have any Room counterpart :

/owners/{id}/homes/{id}

Any ideas how can I do that?


Solution

  • I believe you are looking for a function called nestRemoting().

    Take your case as an example, you need to call Owner.nestRemoting('homes') in the boot file to enable nest endpoints

    Details please see our doc: http://loopback.io/doc/en/lb2/Nested-queries.html

    In loopback-example-relations I created a branch contains your models and relations, and how to get nest remote apis work, please check: https://github.com/strongloop/loopback-example-relations/blob/example/nest-relation/server/boot/initNestRelation.js#L3-L4