LoopBack 4 TodoList Application Tutorial - Add TodoList Repository
Page Contents

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.

$ 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.

$ 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/

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 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 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.

export class TodoList extends Entity {
  // ...properties defined by the CLI...

  @hasMany(() => Todo)
  todos?: Todo[];

  // ...constructor def...
}

Similarly for the belongsTo relation:

export class Todo extends Entity {
  // ...properties of the Todo model

  @belongsTo(() => TodoList)
  todoListId: number;

  // ...constructor def...
}

When we ran the lb4 relation command, we accepted the default of Yes to the prompt:

? 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.

Let’s take a closer look at the TodoListRepository.

src/repositories/todo-list.repository.ts

this.todos = this.createHasManyRepositoryFactoryFor(
  'todos',
  todoRepositoryGetter,
);
// this line enables inclusion for this relation
this.registerInclusionResolver('todos', this.todos.inclusionResolver);

As a result, when you get a TodoList, a todos property will be included that contains your related Todos, for example:

{
  "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 also comes with an inclusion resolver function that we can register on the TodoRepository.

src/repositories/todo.repository.ts

this.todoList = this.createBelongsToAccessorFor(
  'todoList',
  todoListRepositoryGetter,
);
// this line enables inclusion for this relation
this.registerInclusionResolver('todoList', this.todoList.inclusionResolver);

Previous step: Add TodoList Repository

Last step: Add TodoList Controller