parent
4d6371b119
commit
31d9233c32
@ -3,33 +3,20 @@ import { ToposortOptions } from '@tachybase/utils';
|
|||||||
import { DataSource } from './data-source';
|
import { DataSource } from './data-source';
|
||||||
import { DataSourceFactory } from './data-source-factory';
|
import { DataSourceFactory } from './data-source-factory';
|
||||||
|
|
||||||
type DataSourceHook = (dataSource: DataSource) => void;
|
|
||||||
|
|
||||||
export class DataSourceManager {
|
export class DataSourceManager {
|
||||||
dataSources: Map<string, DataSource>;
|
dataSources: Map<string, DataSource>;
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
factory: DataSourceFactory = new DataSourceFactory();
|
factory: DataSourceFactory = new DataSourceFactory();
|
||||||
|
|
||||||
protected middlewares = [];
|
protected middlewares = [];
|
||||||
private onceHooks: Array<DataSourceHook> = [];
|
|
||||||
|
|
||||||
constructor(public options = {}) {
|
constructor(public options = {}) {
|
||||||
this.dataSources = new Map();
|
this.dataSources = new Map();
|
||||||
this.middlewares = [];
|
this.middlewares = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
get(dataSourceKey: string) {
|
|
||||||
return this.dataSources.get(dataSourceKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
async add(dataSource: DataSource, options: any = {}) {
|
async add(dataSource: DataSource, options: any = {}) {
|
||||||
await dataSource.load(options);
|
await dataSource.load(options);
|
||||||
this.dataSources.set(dataSource.name, dataSource);
|
this.dataSources.set(dataSource.name, dataSource);
|
||||||
|
|
||||||
for (const hook of this.onceHooks) {
|
|
||||||
hook(dataSource);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
use(fn: any, options?: ToposortOptions) {
|
use(fn: any, options?: ToposortOptions) {
|
||||||
@ -38,39 +25,17 @@ export class DataSourceManager {
|
|||||||
|
|
||||||
middleware() {
|
middleware() {
|
||||||
return async (ctx, next) => {
|
return async (ctx, next) => {
|
||||||
const name = ctx.get('x-data-source') || 'main';
|
const name = ctx.get('x-data-source');
|
||||||
|
if (name) {
|
||||||
if (!this.dataSources.has(name)) {
|
if (this.dataSources.has(name)) {
|
||||||
ctx.throw(`data source ${name} does not exist`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ds = this.dataSources.get(name);
|
const ds = this.dataSources.get(name);
|
||||||
ctx.dataSource = ds;
|
ctx.dataSource = ds;
|
||||||
|
|
||||||
return ds.middleware(this.middlewares)(ctx, next);
|
return ds.middleware(this.middlewares)(ctx, next);
|
||||||
|
} else {
|
||||||
|
ctx.throw(`data source ${name} does not exist`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,6 @@ export type MergeOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export interface ICollectionManager {
|
export interface ICollectionManager {
|
||||||
db: any;
|
|
||||||
registerFieldTypes(types: Record<string, any>): void;
|
registerFieldTypes(types: Record<string, any>): void;
|
||||||
registerFieldInterfaces(interfaces: Record<string, any>): void;
|
registerFieldInterfaces(interfaces: Record<string, any>): void;
|
||||||
registerCollectionTemplates(templates: Record<string, any>): void;
|
registerCollectionTemplates(templates: Record<string, any>): void;
|
||||||
|
@ -111,13 +111,9 @@ export class OmniTrigger extends Trigger {
|
|||||||
};
|
};
|
||||||
constructor(workflow) {
|
constructor(workflow) {
|
||||||
super(workflow);
|
super(workflow);
|
||||||
this.workflow.app.dataSourceManager.afterAddDataSource((dataSource) => {
|
this.workflow.app.resourcer.registerActionHandler('trigger', this.triggerAction);
|
||||||
if (!dataSource.collectionManager?.db) {
|
this.workflow.app.acl.allow('*', 'trigger', 'loggedIn');
|
||||||
return;
|
(this.workflow.app.pm.get(PluginErrorHandler) as PluginErrorHandler).errorHandler.register(
|
||||||
}
|
|
||||||
dataSource.resourceManager.registerActionHandler('trigger', this.triggerAction);
|
|
||||||
});
|
|
||||||
workflow.app.pm.get(PluginErrorHandler).errorHandler.register(
|
|
||||||
(err) => err instanceof CustomActionInterceptionError,
|
(err) => err instanceof CustomActionInterceptionError,
|
||||||
async (err, ctx) => {
|
async (err, ctx) => {
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
|
Loading…
Reference in New Issue
Block a user