From 4e145a8868a9bdc849f5d9d8a0169537e258754c Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Fri, 22 Mar 2024 12:20:12 +0800 Subject: [PATCH] fix: load data source when data source load failed (#3793) --- .../src/data-source-manager.ts | 1 + .../src/server/__tests__/data-sources.test.ts | 39 ++++++++++++++++++- .../src/server/plugin.ts | 7 ++-- 3 files changed, 42 insertions(+), 5 deletions(-) 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 8f1fb77de..13e8ba461 100644 --- a/packages/core/data-source-manager/src/data-source-manager.ts +++ b/packages/core/data-source-manager/src/data-source-manager.ts @@ -14,6 +14,7 @@ export class DataSourceManager { } async add(dataSource: DataSource, options: any = {}) { + console.log('add', dataSource.name, options); await dataSource.load(options); this.dataSources.set(dataSource.name, dataSource); } 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 6f317e5e2..7c2fbd712 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 @@ -342,9 +342,10 @@ describe('data source', async () => { expect(app.dataSourceManager.dataSources.get('mockInstance1')).not.toBeDefined(); }); + class MockCollectionManager extends CollectionManager {} + describe('data source collections', () => { beforeEach(async () => { - class MockCollectionManager extends CollectionManager {} class MockDataSource extends DataSource { async load(): Promise { this.collectionManager.defineCollection({ @@ -397,6 +398,7 @@ describe('data source', async () => { const listResp = await app.agent().resource('dataSources').list({ appends: 'collections', }); + expect(listResp.status).toBe(200); const listEnabledResp = await app.agent().resource('dataSources').listEnabled({}); @@ -406,6 +408,41 @@ describe('data source', async () => { expect(item.collections).toBeDefined(); }); + it('should get data source with collections when data source load failed', async () => { + class MockDataSource2 extends DataSource { + async load(): Promise { + throw new Error('load failed'); + } + + createCollectionManager(options?: any) { + return new MockCollectionManager(); + } + } + + app.dataSourceManager.factory.register('mock2', MockDataSource2); + + await app.db.getRepository('dataSources').create({ + values: { + key: 'mockInstance2', + type: 'mock2', + displayName: 'Mock 2', + options: { + password: '123456', + }, + }, + }); + + await waitSecond(1000); + + const listResp = await app.agent().resource('dataSources').listEnabled({ + appends: 'collections', + }); + + expect(listResp.status).toBe(200); + + console.log(listResp.body.data); + }); + it('should get collections from datasource', async () => { const listResp = await app.agent().resource('dataSources').list(); expect(listResp.status).toBe(200); 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 ea4933139..8d0cbecf4 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 @@ -159,15 +159,14 @@ export class PluginDataSourceManagerServer extends Plugin { const dataSource = app.dataSourceManager.dataSources.get(dataSourceModel.get('key')); const dataSourceStatus = plugin.dataSourceStatus[dataSourceModel.get('key')]; - // @ts-ignore - const isDBInstance = !!dataSource.collectionManager.db; - const item: any = { key: dataSourceModel.get('key'), displayName: dataSourceModel.get('displayName'), status: dataSourceStatus, type: dataSourceModel.get('type'), - isDBInstance, + + // @ts-ignore + isDBInstance: !!dataSource?.collectionManager.db, }; if (dataSourceStatus === 'loading-failed' || dataSourceStatus === 'reloading-failed') {