? 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
然后这个时候,你就可以跑起来自己的项目了。
创建空的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/
创建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/
创建Relation
通过上面的步骤我们再创建一个TodoList 的 model和repositorie
然后我们创建他们两之间的联系
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/
这样我们就给两个model之间创建了联系。
注意,二者必须都有repositorie才可以,不然没法创建关联。
因为创建关联这一步会创建一个Controller出来。
添加JWT校验
1 2 3
$ lb4 example todo $ cd loopback4-example-todo $ npm i --save @loopback/authentication @loopback/authentication-jwt
// Mount authentication system this.component(AuthenticationComponent); // Mount jwt component this.component(JWTAuthenticationComponent); // Bind datasource this.dataSource(MemoryDataSource, UserServiceBindings.DATASOURCE_NAME);