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: {