fix(plugin-workflow): fix collection trigger not work after data source sync

This commit is contained in:
mytharcher 2025-06-24 22:15:07 +08:00
parent e527e4d6ba
commit 3773c02faa
3 changed files with 55 additions and 3 deletions

View File

@ -294,7 +294,7 @@ export default class PluginWorkflowServer extends Plugin {
}
private initTriggers<T extends Trigger>(more: { [key: string]: T | { new (p: Plugin): T } } = {}) {
this.registerTrigger('collection', CollectionTrigger);
this.registerTrigger(CollectionTrigger.TYPE, CollectionTrigger);
this.registerTrigger('schedule', ScheduleTrigger);
for (const [name, trigger] of Object.entries(more)) {

View File

@ -1231,7 +1231,7 @@ describe('workflow > triggers > collection', () => {
expect(e3s.length).toBe(1);
});
it.skip('sync event on another', async () => {
it('sync event on another', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
@ -1261,5 +1261,36 @@ describe('workflow > triggers > collection', () => {
const p2s = await AnotherPostRepo.find();
expect(p2s.length).toBe(1);
});
it.only('trigger event after data source reload', async () => {
const w1 = await WorkflowModel.create({
enabled: true,
sync: true,
type: 'collection',
config: {
mode: 1,
collection: 'another:posts',
},
});
const AnotherPostRepo = anotherDB.getRepository('posts');
const p1 = await AnotherPostRepo.create({ values: { title: 't2' } });
const e1s = await w1.getExecutions();
expect(e1s.length).toBe(1);
const res1 = await agent.resource('dataSources').refresh({
filterByTk: 'another',
clientStatus: 'loaded',
});
expect(res1.status).toBe(200);
await sleep(1000);
const p2 = await AnotherPostRepo.create({ values: { title: 't2' } });
const e2s = await w1.getExecutions();
expect(e2s.length).toBe(2);
});
});
});

View File

@ -15,7 +15,7 @@ import { ICollection, parseCollectionName, SequelizeCollectionManager } from '@n
import Trigger from '.';
import { toJSON } from '../utils';
import type { WorkflowModel } from '../types';
import type { EventOptions } from '../Plugin';
import Plugin, { EventOptions } from '../Plugin';
import { Context } from '@nocobase/actions';
export interface CollectionChangeTriggerConfig {
@ -49,8 +49,29 @@ function getFieldRawName(collection: ICollection, name: string) {
}
export default class CollectionTrigger extends Trigger {
static TYPE = 'collection';
events = new Map();
constructor(workflow: Plugin) {
super(workflow);
workflow.app.dataSourceManager.beforeAddDataSource((dataSource) => {
if (dataSource.collectionManager instanceof SequelizeCollectionManager) {
const workflowPlugin = workflow.app.pm.get(Plugin) as Plugin;
const workflows = workflowPlugin.enabledCache.values();
for (const item of workflows) {
const [dataSourceName, collectionName] = parseCollectionName(item.config.collection);
if (item.type !== CollectionTrigger.TYPE || dataSourceName !== dataSource.name) {
continue;
}
this.off(item);
this.on(item);
}
}
});
}
// async function, should return promise
private static async handler(this: CollectionTrigger, workflowId: number, eventType: string, data: Model, options) {
const workflow = this.workflow.enabledCache.get(workflowId);