LoopBack 4 TodoList Application Tutorial - Add TodoList Model

Building a checklist for your Todo models

A todo item is often grouped into a checklist along with other todo items sothat it can be used to measure the progress of a bigger picture.

A data set can often be related to another data set, so that an entity may beable to provide access to another entity based on its relationship with theother entity. To take TodoListApplication one step further and establishrelations with the existing Todo model as real-world applications often tendto do, we’ll introduce the model TodoList.

We’ll create the TodoList model to represent a checklist that containsmultiple Todo items. Let’s define TodoList model with the following properties:

  • a unique id
  • a title
  • a color to represent the TodoList withWe can use the lb4 model command and answer the prompts to generate the modelfor us as follows:
  1. $ lb4 model
  2. ? Model class name: TodoList
  3. ? Please select the model base class Entity
  4. ? Allow additional (free-form) properties? No
  5. Model TodoList will be created in src/models/todo-list.model.ts
  6. Let's add a property to TodoList
  7. Enter an empty property name when done
  8. ? Enter the property name: id
  9. ? Property type: number
  10. ? Is ID field? Yes
  11. ? Required?: No
  12. ? Default value [leave blank for none]:
  13. Let's add another property to TodoList
  14. Enter an empty property name when done
  15. ? Enter the property name: title
  16. ? Property type: string
  17. ? Required?: Yes
  18. ? Default value [leave blank for none]:
  19. Let's add another property to TodoList
  20. Enter an empty property name when done
  21. ? Enter the property name: color
  22. ? Property type: string
  23. ? Required?: No
  24. ? Default value [leave blank for none]:
  25. Let's add another property to TodoList
  26. Enter an empty property name when done
  27. ? Enter the property name:
  28. create src/models/todo-list.model.ts
  29. update src/models/index.ts
  30. Model TodoList was created in src/models/

Now that we have our new model, we need to define its relation with the Todomodel. Add the following import statements and property to the TodoList modeland update the TodoListRelations interface to include todos:

src/models/todo-list.model.ts

  1. import {hasMany} from '@loopback/repository';
  2. import {Todo, TodoWithRelations} from './todo.model';
  3. @model()
  4. export class TodoList extends Entity {
  5. // ...properties defined by the CLI...
  6. @hasMany(() => Todo)
  7. todos?: Todo[];
  8. // ...constructor def...
  9. }
  10. export interface TodoListRelations {
  11. todos?: TodoWithRelations[];
  12. }
  13. export type TodoListWithRelations = TodoList & TodoListRelations;

The @hasMany() decorator defines this property. As the decorator’s namesuggests, @hasMany() informs LoopBack 4 that a todo list can have many todoitems.

To complement TodoList’s relationship to Todo, we’ll add in the todoListIdproperty on the Todo model to define the relation on both ends, along withupdating the TodoRelations interface to include todoList:

src/models/todo.model.ts

  1. @model()
  2. export class Todo extends Entity {
  3. // ...properties defined by the CLI...
  4. @belongsTo(() => TodoList)
  5. todoListId: number;
  6. // ...constructor def...
  7. }
  8. export interface TodoRelations {
  9. todoList?: TodoListWithRelations;
  10. }
  11. export type TodoWithRelations = Todo & TodoRelations;

Once the models have been completely configured, it’s time to move on to addinga repository for TodoList.

Navigation

Introduction: TodoList Tutorial

Next step: Add TodoList repository