From 89ad88857d02bdbc07a105b554caec3ce399f963 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 5 Mar 2024 15:58:45 +0800 Subject: [PATCH] chore: remove data source (#3610) --- .../src/server/__tests__/data-sources.test.ts | 39 +++++++++++++++++++ .../server/models/data-sources-field-model.ts | 19 ++++----- .../src/server/plugin.ts | 4 ++ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts index 126d9851f..6f317e5e2 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts @@ -303,6 +303,45 @@ describe('data source', async () => { expect(mockDataSource).toBeInstanceOf(MockDataSource); }); + it('should destroy data source', async () => { + class MockCollectionManager extends CollectionManager {} + + class MockDataSource extends DataSource { + static testConnection(options?: any): Promise { + return Promise.resolve(true); + } + + async load(): Promise { + await waitSecond(1000); + } + + createCollectionManager(options?: any): any { + return new MockCollectionManager(); + } + } + + app.dataSourceManager.factory.register('mock', MockDataSource); + + await app.db.getRepository('dataSources').create({ + values: { + key: 'mockInstance1', + type: 'mock', + displayName: 'Mock', + options: {}, + }, + }); + + await waitSecond(2000); + + expect(app.dataSourceManager.dataSources.get('mockInstance1')).toBeDefined(); + + await app.agent().resource('dataSources').destroy({ + filterByTk: 'mockInstance1', + }); + + expect(app.dataSourceManager.dataSources.get('mockInstance1')).not.toBeDefined(); + }); + describe('data source collections', () => { beforeEach(async () => { class MockCollectionManager extends CollectionManager {} diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-sources-field-model.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-sources-field-model.ts index e977482f3..b2be5c746 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-sources-field-model.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-sources-field-model.ts @@ -19,18 +19,6 @@ export class DataSourcesFieldModel extends MagicAttributeModel { const newOptions = mergeOptions(oldField ? oldField.options : {}, options); collection.setField(name, newOptions); - - // const interfaceOption = options.interface; - - // if (interfaceOption === 'updatedAt') { - // // @ts-ignore - // collection.model._timestampAttributes.createdAt = this.get('name'); - // } - // - // if (interfaceOption === 'createdAt') { - // // @ts-ignore - // collection.model._timestampAttributes.updatedAt = this.get('name'); - // } } unload(loadOptions: LoadOptions) { @@ -38,7 +26,14 @@ export class DataSourcesFieldModel extends MagicAttributeModel { const options = this.get(); const { collectionName, name, dataSourceKey } = options; const dataSource = app.dataSourceManager.dataSources.get(dataSourceKey); + if (!dataSource) { + return; + } const collection = dataSource.collectionManager.getCollection(collectionName); + if (!collection) { + return; + } + collection.removeField(name); } } diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts index d65ee7b40..2bb994871 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts @@ -322,6 +322,10 @@ export class PluginDataSourceManagerServer extends Plugin { }); }); + this.app.db.on('dataSources.afterDestroy', async (model: DataSourceModel) => { + this.app.dataSourceManager.dataSources.delete(model.get('key')); + }); + this.app.on('afterStart', async (app: Application) => { const dataSourcesRecords: DataSourceModel[] = await this.app.db.getRepository('dataSources').find({ filter: {