Overview

Individual models are easy to understand and work with. But in reality, modelsare often connected or related. When you build a real-world application withmultiple models, you’ll typically need to define relations between models. Forexample:

  • A customer has many orders and each order is owned by a customer.
  • A user can be assigned to one or more roles and a role can have zero or moreusers.
  • A physician takes care of many patients through appointments. A patient cansee many physicians too.With connected models, LoopBack exposes as a set of APIs to interact with eachof the model instances and query and filter the information based on theclient’s needs.

Model relation in LoopBack 3 is one of its powerful features which helps usersdefine real-world mappings between their models, access sensible CRUD APIs foreach of the models, and add querying and filtering capabilities for the relationAPIs after scaffolding their LoopBack applications. In LoopBack 4, with theintroduction of repositories, we aim to simplify the approachto relations by creating constrained repositories. This means that certainconstraints need to be honoured by the target model repository based on therelation definition, and thus we produce a constrained version of it as anavigational property on the source repository.

Here are the currently supported relations:

Note:

The hasMany relation may alternatively be implemented using thereferencesMany and embedsMany relations. These relations are similar, butnot the same. Since each database paradigm comes with different trade-offs andthus different databases require the applications to use different relationtypes, use the relation best suited for your database.

The articles on each type of relation above will show you how to leverage thenew relation engine to define and configure relations in your LoopBackapplication.

To generate a HasMany or BelongsTo relation through the CLI, seeRelation generator.