LoopBack 4 Todo Application Tutorial - Add the Todo Model

Models

Now we can begin working on the representation of our data for use withLoopBack 4. To that end, we’re going to create a Todo model that can representinstances of a task for our Todo list. The Todo model will serve both as aData Transfer Object (alsoknown as a DTO) for representing incoming Todo instances on requests, as well asour data structure for use with loopback-datasource-juggler.

A model describes business domain objects and defines a list of properties withname, type, and other constraints.

Models are used for data exchange on the wire or between different systems.

For more information about Models and how they are used in LoopBack, seeModels.

Note:LoopBack 3 treated models as the ‘center’ of operations; in LoopBack 4, that is no longer the case. While LoopBack 4 provides many of the helper methods and decorators that allow you to utilize models in a similar way, you are no longer required to do so!

Building your Todo model

A todo list is all about tracking tasks. For this to be useful, it will need tolet you label tasks so that you can distinguish between them, add extrainformation to describe those tasks, and finally, provide a way of trackingwhether or not they’re complete.

The to-do model has the following properties:

  • id: a unique id
  • title: a title
  • desc: a description that details the specific task to be accomplished
  • isComplete: a boolean flag for whether or not we’ve completed the taskWe can use the lb4 model command and answer the prompts to generate the modelfor us. Press return with an empty property name to generate the model. Followthese steps:
  1. lb4 model
  2. ? Model class name: todo
  3. ? Please select the model base class: Entity
  4. ? Allow additional (free-form) properties? No
  5. Model Todo will be created in src/models/todo.model.ts
  6. Let's add a property to Todo
  7. Enter an empty property name when done
  8. ? Enter the property name: id
  9. ? Property type: number
  10. ? Is id the ID property? Yes
  11. ? Is it required?: No
  12. ? Default value [leave blank for none]:
  13. Let's add another property to Todo
  14. Enter an empty property name when done
  15. ? Enter the property name: title
  16. ? Property type: string
  17. ? Is it required?: Yes
  18. ? Default value [leave blank for none]:
  19. Let's add another property to Todo
  20. Enter an empty property name when done
  21. ? Enter the property name: desc
  22. ? Property type: string
  23. ? Is it required?: No
  24. ? Default value [leave blank for none]:
  25. Let's add another property to Todo
  26. Enter an empty property name when done
  27. ? Enter the property name: isComplete
  28. ? Property type: boolean
  29. ? Is it required?: No
  30. ? Default value [leave blank for none]:
  31. Let's add another property to Todo
  32. Enter an empty property name when done
  33. ? Enter the property name:
  34. create src/models/todo.model.ts
  35. update src/models/index.ts
  36. Model Todo was created in src/models/

Now that we have our model, it’s time to add adatasource so we can perform real CRUDoperations!

Navigation

Previous step: Create your app scaffolding

Next step: Add a datasource