在log删除的时候自动解除oper的关联

This commit is contained in:
pqcqaq 2024-10-16 19:53:34 +08:00
parent 5b4e5cd4e0
commit 7b20c963d0
1 changed files with 41 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import { EntityDict as BaseEntityDict } from '../base-app-domain';
import { EntityDict } from '../types/Entity';
import { CreateTriggerInTxn, SeqAttribute, Trigger } from '../types';
import { CreateTriggerInTxn, RemoveTriggerInTxn, SeqAttribute, Trigger } from '../types';
import { generateNewIdAsync } from '../utils/uuid';
import { AsyncContext } from './AsyncRowStore';
import assert from 'assert';
@ -377,5 +377,44 @@ export const logTriggers: Trigger<EntityDict & BaseEntityDict, 'log', AsyncConte
return (result.oper?.remove || 0) + (result2.log?.remove || 0);
}
} as CreateTriggerInTxn<EntityDict & BaseEntityDict, 'log', AsyncContext<EntityDict & BaseEntityDict>>
} as CreateTriggerInTxn<EntityDict & BaseEntityDict, 'log', AsyncContext<EntityDict & BaseEntityDict>>,
// 在删除log时解除与此log关联的oper
{
name: '当删除log时解除与此log关联的oper',
entity: 'log',
action: 'remove',
when: 'before',
fn: async ({ operation }, context) => {
const { filter } = operation;
assert(filter);
// 查询这次删除操作涉及到的所有log
const logs = await context.select('log', {
data: {
id: 1,
oper$log: {
$entity: 'oper',
data: {
id: 1,
},
},
},
filter,
}, {});
const operIds = logs.flatMap(log => (log as BaseEntityDict['log']['Schema']).oper$log!.map(oper => oper.id));
let result = 0;
for (const operId of operIds) {
const result2 = await context.operate('oper', {
id: operId,
action: 'update',
data: {
logId: null,
},
}, {});
result += result2.oper?.update || 0;
}
return result;
}
} as RemoveTriggerInTxn<EntityDict & BaseEntityDict, 'log', AsyncContext<EntityDict & BaseEntityDict>>,
];