Recently, I read the doc of loopback4 on a whim. To be honest, I vomited a little blood when I read it. It may be that I am too bad. I don’t say the English one. This is okay. There are no strange words, but they are from east to west. Overall, there are two feelings. First, it seems that typing a few lines of command code will come out, but if you don’t understand those strange syntactic sugar words, you can’t type your own code, which is inflexible. Second, doc does not see A complete example, all of which suddenly give you a github link halfway through, asking you to see the project.
But after watching it for so long, let me briefly summarize how to use it to build a project
Install Scaffolding
1
npm i -g @loopback/cli
If you are using
1
yarn global add @loopback/cli
Be careful to add your yarn bin to the environment variables, otherwise you will not be able to perform the following step
Create a new project
1
lb4 app
Answer the prompts as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13
? Project name: getting-started ? Project description: Getting started tutorial ? Project root directory: (getting-started) ? Application class name: StarterApplication ? Select features to enable in the project: ❯◉ Enable eslint: add a linter with pre-configured lint rules ◉ Enable prettier: install prettier to format code conforming to rules ◉ Enable mocha: install mocha to run tests ◉ Enable loopbackBuild: use @loopback/build helpers (e.g. lb-eslint) ◉ Enable vscode: add VSCode config files ◉ Enable docker: include Dockerfile and .dockerignore ◉ Enable repositories: include repository imports and RepositoryMixin ◉ Enable services: include service-proxy imports and ServiceMixin
Then, you can start running your own project.
Creating an Empty Controller
1
lb4 controller
Note: If your application is still running, press CTRL+C to stop it before calling the command
Answer the prompts as follows:
1 2 3 4 5 6
? Controller class name: hello ? What kind of controller would you like to generate? Empty Controller create src/controllers/hello.controller.ts update src/controllers/index.ts
Controller hello was now created in src/controllers/
Paste the following contents into the file /src/controllers/hello.controller.ts:
lb4 model ? Model class name: todo ? Please select the model base class Entity (A persisted model with an ID) ? Allow additional (free-form) properties? No Model Todo will be created in src/models/todo.model.ts
Let's add a property to Todo Enter an empty property name when done
? Enter the property name: id ? Property type: number ? Is id the ID property? Yes ? Is id generated automatically? No ? Is it required?: No ? Default value [leave blank for none]:
Let's add another property to Todo Enter an empty property name when done
? Enter the property name: title ? Property type: string ? Is it required?: Yes ? Default value [leave blank for none]:
Let's add another property to Todo Enter an empty property name when done
? Enter the property name: desc ? Property type: string ? Is it required?: No ? Default value [leave blank for none]:
Let's add another property to Todo Enter an empty property name when done
? Enter the property name: isComplete ? Property type: boolean ? Is it required?: No ? Default value [leave blank for none]:
Let's add another property to Todo Enter an empty property name when done
lb4 datasource ? Datasource name: db ? Select the connector for db: In-memory db (supported by StrongLoop) ? window.localStorage key to use for persistence (browser only): ? Full path to file for persistence (server only): ./data/db.json
lb4 repository ? Please select the datasource DbDatasource ? Select the model(s) you want to generate a repository Todo ? Please select the repository base class DefaultCrudRepository (Juggler bridge)
Repository TodoRepository was created in src/repositories/
Creating a Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
lb4 controller ? Controller class name: todo Controller Todo will be created in src/controllers/todo.controller.ts
? What kind of controller would you like to generate? REST Controller with CRUD functions ? What is the name of the model to use with this CRUD repository? Todo ? What is the name of your CRUD repository? TodoRepository ? What is the name of ID property? id ? What is the type of your ID? number ? Is the id omitted when creating a new instance? Yes ? What is the base HTTP path name of the CRUD operations? /todos create src/controllers/todo.controller.ts update src/controllers/index.ts
Controller Todo was created in src/controllers/
Create Relations
Through the above steps, we create a TodoList model and repositorie.
Then we create a connection between the two of them
1 2 3 4 5 6 7 8 9 10
$ lb4 relation ? Please select the relation type hasMany ? Please select source model TodoList ? Please select target model Todo ? Foreign key name to define on the target model todoListId ? Source property name for the relation getter (will be the relation name) todos ? Allow TodoList queries to include data from related Todo instances? Yes create src/controllers/todo-list-todo.controller.ts
Relation HasMany was created in src/
This creates a connection between the two models.
Note that both must have a repositorie, otherwise the association cannot be created.
Because the Create Association step will create a Controller.
Add JWT validation
1 2 3
$ lb4 example todo $ cd loopback4-example-todo $ npm i --save @loopback/authentication @loopback/authentication-jwt
// ---------- ADD IMPORTS ------------- import {authenticate} from'@loopback/authentication'; // ------------------------------------ @authenticate('jwt') // <---- Apply the @authenticate decorator at the class level exportclassTodoController { //... }
Question
So far, you can do todo CRUD through the api provided by loopback, it looks happy, doesn’t it?
But there are a few questions I didn’t find the answers to in the doc
I can’t find a place where I can update two models with one request, such as TodoList and Todo above. I want to update these two related models, I have to make two requests.
Customizing the Request body is very cumbersome, and you must strictly follow its specifications.
The type of a variable I defined in Model is an enum type I defined, and the project cannot be started. The reason for the error is that the type I defined cannot be resolved.
With its JWT verification, you can not customize the UserModel, although it is convenient, but I did not find a place where I can customize the User, so I can not add the field I need to the User.
I used a package called casbin for authentication, and the doc is also all English, so I don’t quite understand it.
These are some of the problems I have encountered, and I hope that as I deepen my understanding of it, I can solve it.
Update on 2021.03.20 (fill in the problems in the previous issue)
How to Customize RequestBody
At present, the fastest way is to create a Model through’lb4 model ', the format is the RequestBody you need, of course, you can also completely type one yourself, for example:
// Mount authentication system this.component(AuthenticationComponent); // Mount jwt component this.component(JWTAuthenticationComponent); // Bind datasource this.dataSource(MemoryDataSource, UserServiceBindings.DATASOURCE_NAME);