更新数据 delete,destroy,deletewhere,destroywhere

数据模型内置了数据删除处理器, 这些处理器可用于服务接口(API)和数据流(Flow)数据删除功能。

示例

新建模型

新增models/category.mod.json文件,写入以下内容:

{
"name": "书籍分类",
"table": {
"name": "category",
"comment": "书籍分类"
},
"columns": [
{
"label": "ID",
"name": "id",
"type": "ID",
"comment": "ID",
"primary": true
},
{
"label": "父级id",
"name": "parent_id",
"type": "integer",
"nullable": true
},
{
"label": "分类名称",
"name": "name",
"type": "string",
"length": 128,
"index": true
}
],
"relations": {
"book": {
"type": "hasMany",
"model": "book",
"key": "category_id",
"foreign": "id",
"query": {}
},
"parent": {
"type": "hasOne",
"model": "category",
"key": "id",
"foreign": "parent_id",
"query": {}
}
},
"option": {
"timestamps": true,
"soft_deletes": true
},
"values": [
{
"id": 1,
"parent_id": null,
"name": "文史类"
},
{
"id": 2,
"parent_id": 1,
"name": "历史"
},
{
"id": 3,
"parent_id": 1,
"name": "古诗"
},
{
"id": 4,
"parent_id": null,
"name": "理工类"
},
{
"id": 5,
"parent_id": 4,
"name": "数学"
},
{
"id": 6,
"parent_id": 4,
"name": "物理"
}
]
}

Delete 传入 id 删除,如果有设置软删除,则会变成软删除,新建scripts/test.js

function deletes() {
return Process("models.category.delete", 10, {});
}

运行 yao run scripts.test.deletes

Destroy 直接从数据库中删除:

function Destroy() {
return Process("models.category.destroy", 9, {});
}

运行 yao run scripts.test.Destroy

Deletewhere 批量删除,也是软删除

function Deletewhere() {
return Process("models.category.deletewhere", {
wheres: [{ column: "parent_id", value: 4 }],
});
}

运行 yao run scripts.test.Deletewhere

Deletewhere 批量删除,也是软删除

function Destroywhere() {
return Process("models.category.destroywhere", {
wheres: [{ column: "parent_id", value: 4 }],
});
}

运行 yao run scripts.test.Destroywhere

更新数据 delete,destroy,deletewhere,destroywhere

数据模型内置了数据删除处理器, 这些处理器可用于服务接口(API)和数据流(Flow)数据删除功能。

示例

新建模型

新增models/category.mod.json文件,写入以下内容:

{
"name": "书籍分类",
"table": {
"name": "category",
"comment": "书籍分类"
},
"columns": [
{
"label": "ID",
"name": "id",
"type": "ID",
"comment": "ID",
"primary": true
},
{
"label": "父级id",
"name": "parent_id",
"type": "integer",
"nullable": true
},
{
"label": "分类名称",
"name": "name",
"type": "string",
"length": 128,
"index": true
}
],
"relations": {
"book": {
"type": "hasMany",
"model": "book",
"key": "category_id",
"foreign": "id",
"query": {}
},
"parent": {
"type": "hasOne",
"model": "category",
"key": "id",
"foreign": "parent_id",
"query": {}
}
},
"option": {
"timestamps": true,
"soft_deletes": true
},
"values": [
{
"id": 1,
"parent_id": null,
"name": "文史类"
},
{
"id": 2,
"parent_id": 1,
"name": "历史"
},
{
"id": 3,
"parent_id": 1,
"name": "古诗"
},
{
"id": 4,
"parent_id": null,
"name": "理工类"
},
{
"id": 5,
"parent_id": 4,
"name": "数学"
},
{
"id": 6,
"parent_id": 4,
"name": "物理"
}
]
}

Delete 传入 id 删除,如果有设置软删除,则会变成软删除,新建scripts/test.js

function deletes() {
return Process("models.category.delete", 10, {});
}

运行 yao run scripts.test.deletes

Destroy 直接从数据库中删除:

function Destroy() {
return Process("models.category.destroy", 9, {});
}

运行 yao run scripts.test.Destroy

Deletewhere 批量删除,也是软删除

function Deletewhere() {
return Process("models.category.deletewhere", {
wheres: [{ column: "parent_id", value: 4 }],
});
}

运行 yao run scripts.test.Deletewhere

Deletewhere 批量删除,也是软删除

function Destroywhere() {
return Process("models.category.destroywhere", {
wheres: [{ column: "parent_id", value: 4 }],
});
}

运行 yao run scripts.test.Destroywhere