-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(docs): update TodoList tutorial #4126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| --- | ||
| lang: en | ||
| title: 'Add Model Relations' | ||
| keywords: LoopBack 4.0, LoopBack 4 | ||
| sidebar: lb4_sidebar | ||
| permalink: /doc/en/lb4/todo-list-tutorial-relations.html | ||
| summary: LoopBack 4 TodoList Application Tutorial - Add TodoList Repository | ||
| --- | ||
|
|
||
| ### Define the model relation | ||
|
|
||
| We are going to add the model relation to indicate the relation that `TodoList` | ||
| _hasMany_ `Todo` using the | ||
| [`lb4 relation` command](https://loopback.io/doc/en/lb4/Relation-generator.html). | ||
|
|
||
| ```sh | ||
| $ 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 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/ | ||
| ``` | ||
|
|
||
| Now, we're going to add the relation for `Todo`. That is, `Todo` _belongsTo_ | ||
| `TodoList`. | ||
|
|
||
| ```sh | ||
| $ lb4 relation | ||
| ? Please select the relation type belongsTo | ||
| ? Please select source model Todo | ||
| ? Please select target model TodoList | ||
| ? Source property name for the relation getter todoListId | ||
| ? Allow Todo queries to include data from related TodoList instances? Yes | ||
| create src/controllers/todo-todo-list.controller.ts | ||
|
|
||
| Relation BelongsTo was created in src/ | ||
| ``` | ||
|
|
||
| {% include note.html content=" | ||
| we use **default** foreign key and source property names in this case. | ||
| If you'd like to customize them, please check `Relation Metadata` | ||
| https://loopback.io/doc/en/lb4/HasMany-relation.html#relation-metadata and other | ||
| relations as well. | ||
| " %} | ||
|
|
||
| ### Behind the scene | ||
|
|
||
| If you want to understand the code changes introduced from the relation | ||
| generator command, read on the details in this section; otherwise, you are ready | ||
agnes512 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| to move to the next step to create the controller. | ||
|
|
||
| #### Relation decorators | ||
|
|
||
| When we added the `hasMany` relation using the `lb4 relation` command, it added | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When we added the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, it added the property + the decorator. How about: When we added the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, we still need a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should be OK. how about this: When we added the |
||
| the `@hasMany()` decorator together with the `todos` property. As the | ||
| decorator's name suggests, `@hasMany()` informs LoopBack 4 that a todo list can | ||
| have many todo items. | ||
|
|
||
| ```ts | ||
| export class TodoList extends Entity { | ||
| // ...properties defined by the CLI... | ||
|
|
||
| @hasMany(() => Todo) | ||
| todos?: Todo[]; | ||
|
|
||
| // ...constructor def... | ||
| } | ||
| ``` | ||
|
|
||
| Similarly for the `belongsTo` relation: | ||
|
|
||
| ```ts | ||
| export class Todo extends Entity { | ||
| // ...properties of the Todo model | ||
|
|
||
| @belongsTo(() => TodoList) | ||
| todoListId: number; | ||
|
|
||
| // ...constructor def... | ||
| } | ||
| ``` | ||
|
|
||
| #### Inclusion of Related Models | ||
|
|
||
| When we ran the `lb4 relation` command, we accepted the default of `Yes` to the | ||
| prompt: | ||
|
|
||
| ```sh | ||
| ? Allow Order queries to include data from related Customer instances? (Y/n) | ||
| ``` | ||
|
|
||
| This registers the `inclusionResolver` for the relation(s) you were working with | ||
| above. | ||
|
|
||
| Make sure to choose ‘yes’ if you’d like to use inclusion and your model is | ||
| traversable. In the example of getting the related `Todo` objects for each | ||
| `TodoList`, it registers the inclusion resolver that comes with the | ||
| [`HasManyRepositoryFactory`](https://loopback.io/doc/en/lb4/apidocs.repository.hasmanyrepository.html). | ||
|
|
||
| Let's take a closer look at the `TodoListRepository`. | ||
| {% include code-caption.html content="src/repositories/todo-list.repository.ts" %} | ||
|
|
||
| ```ts | ||
| this.todos = this.createHasManyRepositoryFactoryFor( | ||
| 'todos', | ||
| todoRepositoryGetter, | ||
| ); | ||
| // this line enables inclusion for this relation | ||
| this.registerInclusionResolver('todos', this.todos.inclusionResolver); | ||
agnes512 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| As a result, when you get a `TodoList`, a `todos` property will be included that | ||
| contains your related `Todo`s, for example: | ||
|
|
||
| ```json | ||
| { | ||
| "id": 2, | ||
| "title": "My daily chores", | ||
| "todos": [ | ||
| { | ||
| "id": 3, | ||
| "title": "play space invaders", | ||
| "desc": "Become the very best!", | ||
| "todoListId": 2 | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| On the other end, | ||
| [`BelongsToAccessor`](https://loopback.io/doc/en/lb4/apidocs.repository.belongstoaccessor.html) | ||
| also comes with an inclusion resolver function that we can register on the | ||
| `TodoRepository`. | ||
|
|
||
| {% include code-caption.html content="src/repositories/todo.repository.ts" %} | ||
|
|
||
| ```ts | ||
| this.todoList = this.createBelongsToAccessorFor( | ||
| 'todoList', | ||
| todoListRepositoryGetter, | ||
| ); | ||
| // this line enables inclusion for this relation | ||
| this.registerInclusionResolver('todoList', this.todoList.inclusionResolver); | ||
agnes512 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Navigation | ||
|
|
||
| Previous step: [Add TodoList Repository](todo-list-tutorial-repository.md) | ||
|
|
||
| Last step: [Add TodoList Controller](todo-list-tutorial-controller.md) | ||

Uh oh!
There was an error while loading. Please reload this page.