From 3c6aba95c9cf7d13460396a10e598bcb66b36d41 Mon Sep 17 00:00:00 2001 From: Chareice Date: Wed, 31 Jan 2024 12:55:18 +0800 Subject: [PATCH] chore: update dataSources --- .../src/server/__tests__/data-sources.test.ts | 60 ++++++++++++++++++- .../src/server/collections/data-sources.ts | 4 ++ .../src/server/models/data-source.ts | 4 +- .../src/server/plugin.ts | 25 ++++---- 4 files changed, 79 insertions(+), 14 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 b5fa71cef8..18d7c0b74b 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 @@ -51,6 +51,59 @@ describe('data source', async () => { expect(listResp2.body.data[0].status).toBe('loaded'); }); + it('should not load connection after change enabled status', async () => { + const testConnectionFn = vi.fn(); + + class MockDataSource extends DataSource { + static testConnection(options?: any): Promise { + testConnectionFn(); + return Promise.resolve(true); + } + async load(): Promise {} + + createCollectionManager(options?: any): any { + return undefined; + } + } + + app.dataSourceManager.factory.register('mock', MockDataSource); + + await app + .agent() + .resource('dataSources') + .create({ + values: { + options: {}, + type: 'mock', + key: 'mockInstance1', + enabled: true, + }, + }); + + testConnectionFn.mockClear(); + + const findDataSourceInstance = async () => { + return await app.db.getRepository('dataSources').findOne({ + filterByTk: 'mockInstance1', + }); + }; + + expect((await findDataSourceInstance()).get('enabled')).toBeTruthy(); + + await app + .agent() + .resource('dataSources') + .update({ + filterByTk: 'mockInstance1', + values: { + enabled: false, + }, + }); + + expect(testConnectionFn).toBeCalledTimes(0); + expect((await findDataSourceInstance()).get('enabled')).toBeFalsy(); + }); + it('should call test connection after options change', async () => { const testConnectionFn = vi.fn(); @@ -92,11 +145,14 @@ describe('data source', async () => { .resource('dataSources') .update({ filterByTk: 'mockInstance1', - options: { - otherOptions: 'test', + values: { + options: { + otherOptions: 'test', + }, }, }); + await waitSecond(1000); expect(testConnectionFn).toBeCalledTimes(1); }); diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts index 82a464fa82..70f19acbf1 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/collections/data-sources.ts @@ -14,6 +14,10 @@ export default defineCollection({ type: 'string', name: 'displayName', }, + { + type: 'string', + name: 'type', + }, { type: 'json', name: 'options', diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts index 4182e87695..4baa152fad 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts @@ -1,4 +1,4 @@ -import { MagicAttributeModel, Transaction } from '@nocobase/database'; +import { Model, Transaction } from '@nocobase/database'; import { Application } from '@nocobase/server'; import { LocalData } from '../services/database-introspector'; import { setCurrentRole } from '@nocobase/plugin-acl'; @@ -44,7 +44,7 @@ const availableActions: { }, }; -export class DataSourceModel extends MagicAttributeModel { +export class DataSourceModel extends Model { async loadIntoApplication(options: { app: Application; transaction?: Transaction }) { const { app, transaction } = options; 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 a983d961d3..be4c413b3f 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 @@ -39,22 +39,26 @@ export class PluginDataSourceManagerServer extends Plugin { }); this.app.db.on('dataSources.beforeSave', async (model: DataSourceModel) => { - const dataSourceOptions = model.get('options'); - const type = model.get('type'); + if (model.changed('options')) { + const dataSourceOptions = model.get('options'); + const type = model.get('type'); - const klass = this.app.dataSourceManager.factory.getClass(type); + const klass = this.app.dataSourceManager.factory.getClass(type); - try { - await klass.testConnection(dataSourceOptions); - } catch (error) { - throw new Error(`Test connection failed: ${error.message}`); + try { + await klass.testConnection(dataSourceOptions); + } catch (error) { + throw new Error(`Test connection failed: ${error.message}`); + } } }); this.app.db.on('dataSources.afterSave', async (model: DataSourceModel, options) => { - model.loadIntoApplication({ - app: this.app, - }); + if (model.changed('options')) { + model.loadIntoApplication({ + app: this.app, + }); + } }); this.app.db.on('dataSources.afterCreate', async (model: DataSourceModel, options) => { @@ -74,6 +78,7 @@ export class PluginDataSourceManagerServer extends Plugin { this.app.use(async (ctx, next) => { await next(); + const { actionName, resourceName, params } = ctx.action; if (resourceName === 'dataSources' && actionName == 'list') {