diff --git a/packages/core/data-source-manager/src/data-source-manager.ts b/packages/core/data-source-manager/src/data-source-manager.ts index a6eddcf5e..de9845ac2 100644 --- a/packages/core/data-source-manager/src/data-source-manager.ts +++ b/packages/core/data-source-manager/src/data-source-manager.ts @@ -3,33 +3,20 @@ import { ToposortOptions } from '@tachybase/utils'; import { DataSource } from './data-source'; import { DataSourceFactory } from './data-source-factory'; -type DataSourceHook = (dataSource: DataSource) => void; - export class DataSourceManager { dataSources: Map; - /** - * @internal - */ factory: DataSourceFactory = new DataSourceFactory(); + protected middlewares = []; - private onceHooks: Array = []; constructor(public options = {}) { this.dataSources = new Map(); this.middlewares = []; } - get(dataSourceKey: string) { - return this.dataSources.get(dataSourceKey); - } - async add(dataSource: DataSource, options: any = {}) { await dataSource.load(options); this.dataSources.set(dataSource.name, dataSource); - - for (const hook of this.onceHooks) { - hook(dataSource); - } } use(fn: any, options?: ToposortOptions) { @@ -38,39 +25,17 @@ export class DataSourceManager { middleware() { return async (ctx, next) => { - const name = ctx.get('x-data-source') || 'main'; - - if (!this.dataSources.has(name)) { - ctx.throw(`data source ${name} does not exist`); + const name = ctx.get('x-data-source'); + if (name) { + if (this.dataSources.has(name)) { + const ds = this.dataSources.get(name); + ctx.dataSource = ds; + return ds.middleware(this.middlewares)(ctx, next); + } else { + ctx.throw(`data source ${name} does not exist`); + } } - - const ds = this.dataSources.get(name); - ctx.dataSource = ds; - - return ds.middleware(this.middlewares)(ctx, next); + await next(); }; } - - registerDataSourceType(type: string, DataSourceClass: typeof DataSource) { - this.factory.register(type, DataSourceClass); - } - - getDataSourceType(type: string): typeof DataSource | undefined { - return this.factory.getClass(type); - } - - buildDataSourceByType(type: string, options: any = {}): DataSource { - return this.factory.create(type, options); - } - - afterAddDataSource(hook: DataSourceHook) { - this.addHookAndRun(hook); - } - - private addHookAndRun(hook: DataSourceHook) { - this.onceHooks.push(hook); - for (const dataSource of this.dataSources.values()) { - hook(dataSource); - } - } } diff --git a/packages/core/data-source-manager/src/types.ts b/packages/core/data-source-manager/src/types.ts index e2c16f279..887c3ae84 100644 --- a/packages/core/data-source-manager/src/types.ts +++ b/packages/core/data-source-manager/src/types.ts @@ -54,7 +54,6 @@ export type MergeOptions = { }; export interface ICollectionManager { - db: any; registerFieldTypes(types: Record): void; registerFieldInterfaces(interfaces: Record): void; registerCollectionTemplates(templates: Record): void; diff --git a/packages/plugins/@tachybase/plugin-workflow/src/server/features/omni-trigger/CustomActionTrigger.ts b/packages/plugins/@tachybase/plugin-workflow/src/server/features/omni-trigger/CustomActionTrigger.ts index ff131206d..fc0291d9a 100644 --- a/packages/plugins/@tachybase/plugin-workflow/src/server/features/omni-trigger/CustomActionTrigger.ts +++ b/packages/plugins/@tachybase/plugin-workflow/src/server/features/omni-trigger/CustomActionTrigger.ts @@ -111,13 +111,9 @@ export class OmniTrigger extends Trigger { }; constructor(workflow) { super(workflow); - this.workflow.app.dataSourceManager.afterAddDataSource((dataSource) => { - if (!dataSource.collectionManager?.db) { - return; - } - dataSource.resourceManager.registerActionHandler('trigger', this.triggerAction); - }); - workflow.app.pm.get(PluginErrorHandler).errorHandler.register( + this.workflow.app.resourcer.registerActionHandler('trigger', this.triggerAction); + this.workflow.app.acl.allow('*', 'trigger', 'loggedIn'); + (this.workflow.app.pm.get(PluginErrorHandler) as PluginErrorHandler).errorHandler.register( (err) => err instanceof CustomActionInterceptionError, async (err, ctx) => { ctx.body = {