Reputation: 405
I have a requirement where the model schema will be generated by the user from a user interface that will be exposed by REST API that means dynamic models, controllers and repositories. This already has been achieved.
Now the requirement is to allow users to specify relations between tables from the same user interface. For e.g. I am creating a table order
then while defining its properties I should be able to map product_id
property to product
table.
How can I achieve this?
Upvotes: 1
Views: 329
Reputation: 10785
I am afraid LoopBack 4 does not offer first-class support for building model relations dynamically yet. We have been discussing this feature in GitHub issue loopback-next#2483
Essentially, there are two parts of the problem:
Create repository artifacts for navigating to related models.
This is IMO the easier part, since all you need to do is take the class constructor created by defineCrudRepositoryClass
for the model you are trying to expose via REST API and create a subclass that will call methods like createHasManyRepositoryFactoryFor
and registerInclusionResolver
from the constructor.
Define a controller class implementing relation-specific REST API endpoints.
This is slightly more involved as you need to implement function that will define a new controller class with dynamically generated OpenAPI metadata for each model relation you want to expose via REST API. I would start with copy and paste of defineCrudRestController
function and then reworking individual endpoints to provide navigational API for your relation. You should probably create multiple define{relation}Controller
functions, one for each relation type (e.g. defineHasManyController
).
Upvotes: 0