Create a relation between two LoopBack models

Bram Borggreve
InstructorBram Borggreve
Share this video with your friends

Social Share Links

Send Tweet

A relation in LoopBack is a way to associate the data from multiple models with each other. LoopBack supports various types of relations each with a different use case.

In this lesson we will create a second model called Category and create a relation between the Category and the Product models. To do this we will first add a property to the Product model called categoryId. This will allow us to store for each Product the category it belongs to. We then move on to create the actual relationships.

Our first relationship defines that a Category hasMany Products and the second one that a Product belongsTo a Category.

After defining these relations we will use the API Explorer to see how we can interact with this related data.

[00:00] Inside our project, we run LB model category. It takes the model name from the comment line, so we can accept the defaults. We use the default in-memory DB data source, the default persistent model base class, accept to expose the category via REST, and accept the default [inaudible] form, and we accept to create a common model.

[00:18] Our category model has been created, and we can now add the model properties. We add one property called name. We select type string, set it to be required, and at no default value. As this is the only property we need, we hit enter to quit the dialog.

[00:33] When we look at our project, we see that the category got defined at model config, and that the category definition and its class got created in common models. Now, let's create a relation between the category and the product model.

[00:47] First, we run LB property to add a property to the product model. We select the product model, use category ID as the property name, select type number. We don't make it required, and we don't use there default value.

[01:00] Then we run LB relation, we select a category model, select relation type has many, and select product as the second model. The name of the relationship will be products. Category has many products. It's a foreign key, we set the category ID, and we will not use a through model.

[01:16] We run LB relation a second time. This time, the first model we select is the product model. The relationship type is belongs to, and we select the category as the second model. The name of the relation will be category, so product belongs to category.

[01:29] It's a foreign key, we set the category ID, and this type of relation will not ask for a through model. When we look at our products model, we see that a new endpoint got added. This endpoint can be used to interact with the new relationship.

[01:41] In the category models, we see a few endpoints that allow to interact with the product's relationship. Let's create some data to see how this works. Let's add a category, drinks and food. When we look at our endpoint, we see we have the two category in place.

[01:55] In the product model, we add coffee and tea, and add it to the first category. We add lemon cake and cheesecake, and add it to the second category. When we look at our products endpoint, we see that the categories got added.

[02:08] To see the relation working, we can go to the category model, open the get endpoint for categories ID products, and enter a category ID. We will see that this will return only the products in this category. When we change the ID, we see that we get the products of the other category.

[02:25] We can also view which category is related to a product. To do so, we go to the get products ID category endpoint, and enter the ID. We will see that this product belongs in category drinks, and we see that this product belongs in the category food.

[02:40] Now, if we look at our project, we see how the relations are defined. In the categories model, we find the new relation under the projects key. It defines the type of relation, the model it relates to, and the foreign key. In the product model, we can see that the category ID property got added, and that the relation called category is also defined.