primary 主键
2025/4/16小于 1 分钟
primary 主键
说明
调用getChangedDescriptor的时候才需要设置的属性
一般用于数组类型的字段上,用于两个对象的比较,根据 primary
来判断当前的数据状态是新增,修改还是删除
案例
案例一
模型
class TestItem extends ModelBase {
@Column()
public id?: string
@Column()
public msg?: string
@Column()
public name?: string
}
class Test extends ModelBase {
@Column({
model: () => TestItem,
})
public list!: TestItem[]
}
实例初始化
const test = new Test({
list: [
{
id: '1',
},
{
id: '2',
msg: 'message2',
},
{
id: '4',
msg: 'message4',
},
],
})
修改实例
// update id 2
test.list[1].setColumnData('msg', undefined)
test.list[1].name = 'newName'
// add id 3
const cItem = new TestItem({ id: '3' })
cItem.msg = 'message3'
test.list.push(cItem)
// remove id 4
test.list = test.list.filter((item) => {
if (item.id !== '4') {
return true
}
})
打印日志
console.log(test.getChangedDescriptor())
/*
{
list:{
action: 'UPDATE',
changeDescriptor: {
update: [
{ id: '1' },
{ id: '2', name: 'newName' },
{ id: '3', message: 'message3' },
]
},
currentValue: [
{ id: "1" },
{ id: "2", name: "newName" },
{ id: "3", msg: "message3"}
],
dataKey: 'list',
oldValue: [
{ id: "1" },
{ id: "2", msg: "message2", },
{ id: "4", msg: "message4", },
],
}
}
*/
注意事项
getChangedDescriptor()的输出格式
- key(实例化属性的 key)
- dataKey
- 变化属性的 output 流向数据的 key
- currentValue
- 最新值
- oldValue
- 旧值
- changeDescriptor
- 变化的数据的描述,包括 create、update、delete
- action
- UPDATE、CREATE、DELETE
- dataKey