mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 15:39:24 +08:00
feat: beforeAddDataSource hook (#4810)
This commit is contained in:
parent
5dc626d2d6
commit
a1b2c25a94
@ -165,6 +165,24 @@ describe('example', () => {
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
it('should call beforeAddDataSource hook', async () => {
|
||||
const hook = vi.fn();
|
||||
|
||||
const app = await createMockServer({
|
||||
acl: false,
|
||||
resourcer: {
|
||||
prefix: '/api/',
|
||||
},
|
||||
name: 'update-filter',
|
||||
});
|
||||
|
||||
app.dataSourceManager.beforeAddDataSource(hook);
|
||||
// it should be called on main datasource
|
||||
expect(hook).toBeCalledTimes(1);
|
||||
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
it('should register every datasource instance', async () => {
|
||||
const hook = vi.fn();
|
||||
|
||||
|
@ -21,6 +21,7 @@ export class DataSourceManager {
|
||||
factory: DataSourceFactory = new DataSourceFactory();
|
||||
protected middlewares = [];
|
||||
private onceHooks: Array<DataSourceHook> = [];
|
||||
private beforeAddHooks: Array<DataSourceHook> = [];
|
||||
|
||||
constructor(public options = {}) {
|
||||
this.dataSources = new Map();
|
||||
@ -32,6 +33,10 @@ export class DataSourceManager {
|
||||
}
|
||||
|
||||
async add(dataSource: DataSource, options: any = {}) {
|
||||
for (const hook of this.beforeAddHooks) {
|
||||
hook(dataSource);
|
||||
}
|
||||
|
||||
await dataSource.load(options);
|
||||
this.dataSources.set(dataSource.name, dataSource);
|
||||
|
||||
@ -71,6 +76,13 @@ export class DataSourceManager {
|
||||
return this.factory.create(type, options);
|
||||
}
|
||||
|
||||
beforeAddDataSource(hook: DataSourceHook) {
|
||||
this.beforeAddHooks.push(hook);
|
||||
for (const dataSource of this.dataSources.values()) {
|
||||
hook(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
afterAddDataSource(hook: DataSourceHook) {
|
||||
this.addHookAndRun(hook);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user