refactor(plugin-workflow): add option to skip triggering in collection event (#6379)

This commit is contained in:
Junyi 2025-03-07 10:51:15 +08:00 committed by GitHub
parent 8b1a718091
commit f6ec9f82b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View File

@ -161,6 +161,23 @@ describe('workflow > triggers > collection', () => {
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('c1');
});
it('skipWorkflow', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
sync: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
},
});
const post = await PostRepo.create({ values: { title: 't1' }, context: { skipWorkflow: true } });
const executions = await workflow.getExecutions();
expect(executions.length).toBe(0);
});
});
describe('config.mode', () => {

View File

@ -53,13 +53,17 @@ export default class CollectionTrigger extends Trigger {
// async function, should return promise
private static async handler(this: CollectionTrigger, workflow: WorkflowModel, data: Model, options) {
const { skipWorkflow = false, stack } = options.context ?? {};
if (skipWorkflow) {
return;
}
const [dataSourceName] = parseCollectionName(workflow.config.collection);
const transaction = this.workflow.useDataSourceTransaction(dataSourceName, options.transaction);
const ctx = await this.prepare(workflow, data, { ...options, transaction });
if (!ctx) {
return;
}
const { stack } = options.context ?? {};
if (workflow.sync) {
await this.workflow.trigger(workflow, ctx, {
transaction,