create data model

View Widget Documentation

A data model corresponds to a data table in the database. The structure of the data table is described through a JSON file and placed in the models directory. Use the yao migrate command to create/update the data table structure design.

Data model description

Write a data model description file pet.mod.json and place it in the models directory of the application.

Description file content:

{
"name": "Pet Model",
"table": { "name": "pet", "comment": "pet model" },
"columns": [
{
"label": "ID",
"name": "id",
"type": "ID",
"comment": "ID",
"primary": true
},
{
"label": "date",
"name": "day",
"type": "datetime",
"index": true
},
{
"label": "Name",
"name": "name",
"type": "string",
"length": 128,
"index": true
},
{
"label": "Status",
"name": "status",
"type": "enum",
"default": "enabled",
"option": ["enabled", "disabled"],
"comment": "Status: enabled on, disabled off",
"index": true
},
{
"label": "userid",
"name": "user_id",
"type": "integer",
"index": true
},
{
"label": "Total Amount",
"name": "amount",
"type": "decimal",
"index": true
},
{
"label": "Notes",
"name": "remark",
"type": "text",
"nullable": true
}
],
"values": [],
"relations": {
"users": {
"type": "hasOne",
"model": "user",
"key": "id",
"foreign": "user_-id",
"query": {}
}
},
"option": { "timestamps": true, "soft_deletes": true }
}
Tip: option.timestamps is true, automatically created The created_at and updated_at fields hold the creation time and update time of the data record.option.soft_deletes is true, automatically createddeleted_at field, which is used to mark deleted records. Refer to the data model documentation for details.

Application directory structure:

├── models # Used to store data model description files
│ └── pet.mod.json
├── db
└── ui
└── app.json

Migrate create/update data table

Run Migrate in the project root directory to create/update the data table structure and insert default data.

cd /data/app
yao migrate -n pet
Note: The migrate command will delete the data table with the same name in the database and recreate it, please pay attention to data backup.

data query

List query

yao run models.pet.Get '::{}'

Query data by primary key

yao run models.category.find 1 {}

delete a piece of data

yao run models.pet.Delete 1 {}

add a piece of data

yao run models.pet.Create '::{"name":"cat", "day":"2022-01-01 08:00:00", "status":"enabled", "user_id":1, "amount":1000,"remark":"cat....."}'

update a piece of data

yao run models.pet.Update 1 '::{"remark":"A cute tricolor cat"}'

Save a piece of data, specify the primary key to update, do not specify create and create.

yao run models.pet.Save '::{"name":"cat", "day":"2022-01-01 08:00:00", "status":"enabled", "user_id":1, "amount":1000,"remark":"cat....."}'
yao run models.pet.Save '::{"id":1,"remark":"A cute tricolor cat"}'

create data model

View Widget Documentation

A data model corresponds to a data table in the database. The structure of the data table is described through a JSON file and placed in the models directory. Use the yao migrate command to create/update the data table structure design.

Data model description

Write a data model description file pet.mod.json and place it in the models directory of the application.

Description file content:

{
"name": "Pet Model",
"table": { "name": "pet", "comment": "pet model" },
"columns": [
{
"label": "ID",
"name": "id",
"type": "ID",
"comment": "ID",
"primary": true
},
{
"label": "date",
"name": "day",
"type": "datetime",
"index": true
},
{
"label": "Name",
"name": "name",
"type": "string",
"length": 128,
"index": true
},
{
"label": "Status",
"name": "status",
"type": "enum",
"default": "enabled",
"option": ["enabled", "disabled"],
"comment": "Status: enabled on, disabled off",
"index": true
},
{
"label": "userid",
"name": "user_id",
"type": "integer",
"index": true
},
{
"label": "Total Amount",
"name": "amount",
"type": "decimal",
"index": true
},
{
"label": "Notes",
"name": "remark",
"type": "text",
"nullable": true
}
],
"values": [],
"relations": {
"users": {
"type": "hasOne",
"model": "user",
"key": "id",
"foreign": "user_-id",
"query": {}
}
},
"option": { "timestamps": true, "soft_deletes": true }
}
Tip: option.timestamps is true, automatically created The created_at and updated_at fields hold the creation time and update time of the data record.option.soft_deletes is true, automatically createddeleted_at field, which is used to mark deleted records. Refer to the data model documentation for details.

Application directory structure:

├── models # Used to store data model description files
│ └── pet.mod.json
├── db
└── ui
└── app.json

Migrate create/update data table

Run Migrate in the project root directory to create/update the data table structure and insert default data.

cd /data/app
yao migrate -n pet
Note: The migrate command will delete the data table with the same name in the database and recreate it, please pay attention to data backup.

data query

List query

yao run models.pet.Get '::{}'

Query data by primary key

yao run models.category.find 1 {}

delete a piece of data

yao run models.pet.Delete 1 {}

add a piece of data

yao run models.pet.Create '::{"name":"cat", "day":"2022-01-01 08:00:00", "status":"enabled", "user_id":1, "amount":1000,"remark":"cat....."}'

update a piece of data

yao run models.pet.Update 1 '::{"remark":"A cute tricolor cat"}'

Save a piece of data, specify the primary key to update, do not specify create and create.

yao run models.pet.Save '::{"name":"cat", "day":"2022-01-01 08:00:00", "status":"enabled", "user_id":1, "amount":1000,"remark":"cat....."}'
yao run models.pet.Save '::{"id":1,"remark":"A cute tricolor cat"}'