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.
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 }}
Application directory structure:
├── models # Used to store data model description files│ └── pet.mod.json├── db└── ui└── app.json
Run Migrate
in the project root directory to create/update the data table structure and insert default data.
cd /data/appyao migrate -n pet
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"}'
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.
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 }}
Application directory structure:
├── models # Used to store data model description files│ └── pet.mod.json├── db└── ui└── app.json
Run Migrate
in the project root directory to create/update the data table structure and insert default data.
cd /data/appyao migrate -n pet
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"}'