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 idtitle
: a titledesc
: a description that details the specific task to be accomplishedisComplete
: a boolean flag for whether or not we’ve completed the taskWe can use thelb4 model
command and answer the prompts to generate the modelfor us. Pressreturn
with an empty property name to generate the model. Followthese steps:
lb4 model
? Model class name: todo
? Please select the model base class: Entity
? 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 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
? Enter the property name:
create src/models/todo.model.ts
update src/models/index.ts
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