Photo Credits: https://www.pexels.com/photo/six-white-ceramic-mugs-1251175/
Working in progress
In this CoffeeShop example, there are 3 entities:
A coffee shop can have multiple reviews. As illustrated in the diagram below, the CoffeeShop
model has a hasMany
model relation with the Review
model. Besides, the CoffeeShop
model has only one Income
instance, thus has a hasOne
model relation with Income
model.
In this repo, I'd like to show you how I create this CoffeeShop app step-by-step and add my findings along the way regarding model relations. This CoffeeShop app is inspired by CoffeeShop Review app in LoopBack 3.
Create the CoffeeShop
, Review
and Income
models mentioned above.
Scaffold a LoopBack4 application using lb4 app
command.
Tips: If you know you're going to accept all default, you can use the --yes
option, i.e. lb4 app your-app-name --yes
. It will save you a few keystrokes of accepting the defaults.
You can create the models using the lb4 model
command.
To save you some time, I've created the corresponding json for the 3 models: coffeeshop-model.json
$ lb4 model coffeeshop-model.json --yes
$ lb4 model review-model.json --yes
$ lb4 model income-model.json --yes
Before we can run the relation CLI, we need to have the Repositories created.
Create the datasource and repositories using lb4 datasource
and lb4 repository
command.
hasMany
relation for CoffeeShop and Review model using CLI$ lb4 relation
? Please select the relation type hasMany
? Please select source model CoffeeShop
? Please select target model Review
? Foreign key name to define on the target model coffeeShopId
? Source property name for the relation getter reviews
create src/controllers/coffee-shop-review.controller.ts
Relation HasMany was created in src/
Model Relation Explained
What the command does are:
CoffeeShop
model, it adds the reviews
property with the @hasMany
decoratorReview
model, it adds the coffeeShopId
property.If you only need to navigate from CoffeeShop
to Review
but not the other way round, you can stop here and no need to add the belongsTo
relation.
belongsTo
relation for Review and CoffeeShop model using CLIlb4 relation
is ready, it should be even easier@model
decorator in the Review and Income which are documented in https://github.com/strongloop/loopback-next/pull/2448
automigrate
feature to create the databasenpm run migrate
Modify CoffeeShopRepository to be able to access the ReviewRepository
Create CoffeeShopReviewController
This is used to do the CRUD operations for reviews related to the coffee shop.
Use lb4 discover
to discover the models.
Run npm start
.
Go to localhost:3000/explorer
.
{
"name": "My Toronto shop",
"city": "Toronto"
}
{
"name": "My NYC shop",
"city": "New York City"
}
To verify it is working, call GET /coffee-shops
[
{
"id": 1,
"name": "My Toronto shop",
"city": "Toronto"
},
{
"id": 2,
"name": "My NYC shop",
"city": "New York City"
}
]
Specify the coffeeShopId
as the parameter
For coffeeShopId
is 1
{
"date": "2019-04-26T14:58:15.638Z",
"rating": 4,
"comments": "good service"
}
For coffeeShopId
is 2
{
"date": "2019-04-26T14:58:15.638Z",
"rating": 2,
"comments": "long lineup all the time"
}
To verify, call GET /coffee-shops/{coffeeShopId}/reviews