Create database model
Esta página aún no está disponible en tu idioma.
Database models are used to define the structure of the data that will be stored in the database. This allows you to define the data your module will store in the database. In each model, we define all the properties with their types, and their respective relationships with other models.
For start creating a model, you just need to run the following command:
It will ask you for the name of the model, and the module where you want to create it.
After that, it will create a file in the src/modules/<module>/models
folder with the name of the model.
The file will contain the following code:
To start defining the model, you need to define the properties of the model in the getModel
function.
The getModel
function receives a schema
object, which contains the available types to define the properties of the model.
schema.String
- Defines a string property.schema.Number
- Defines a number property.schema.Boolean
- Defines a boolean property.schema.Date
- Defines a date property.schema.Blob
- Defines a blob property.schema.JSON
- Defines a JSON property.
Now, let’s define an example model for an user.
First in the getModel
function, we will define the properties of the model:
then, in the define
function, we can define validations and relationships with other models.
usage
To use the model in a command, you just need to get the model from framework.database.models and use it.
You can find more information about the usage in the canario package page.