From 805b7903a0f926d725c40fc52ba26964a360ac9c Mon Sep 17 00:00:00 2001 From: Chareice Date: Fri, 26 Jan 2024 20:45:37 +0800 Subject: [PATCH 1/2] chore: refresh api --- .../plugin-data-source-manager/src/server/plugin.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 cc24fa766e..8dff86e1b2 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 @@ -76,15 +76,15 @@ export class PluginDataSourceManagerServer extends Plugin { await next(); }, - async ['databaseConnections:refresh'](ctx, next) { + async ['dataSources:refresh'](ctx, next) { const { filterByTk } = ctx.action.params; - const databaseConnection = await ctx.db.getRepository('databaseConnections').findOne({ + const dataSourceModel: DataSourceModel = await ctx.db.getRepository('dataSources').findOne({ filter: { - name: filterByTk, + key: filterByTk, }, }); - await databaseConnection.loadIntoApplication({ + await dataSourceModel.loadIntoApplication({ app: ctx.app, }); From f0343df18dca5030051562f23b7f87078406a2c7 Mon Sep 17 00:00:00 2001 From: Chareice Date: Fri, 26 Jan 2024 20:50:03 +0800 Subject: [PATCH 2/2] chore: testConnection api --- .../data-source-manager/src/data-source-factory.ts | 4 ++++ .../core/data-source-manager/src/data-source.ts | 4 ++++ .../plugin-data-source-manager/src/server/plugin.ts | 13 ++++++------- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/core/data-source-manager/src/data-source-factory.ts b/packages/core/data-source-manager/src/data-source-factory.ts index 439c4ee946..ec1043d9ee 100644 --- a/packages/core/data-source-manager/src/data-source-factory.ts +++ b/packages/core/data-source-manager/src/data-source-factory.ts @@ -7,6 +7,10 @@ export class DataSourceFactory { this.collectionTypes.set(type, dataSourceClass); } + getClass(type: string): typeof DataSource { + return this.collectionTypes.get(type); + } + create(type: string, options: any = {}): DataSource { const klass = this.collectionTypes.get(type); if (!klass) { diff --git a/packages/core/data-source-manager/src/data-source.ts b/packages/core/data-source-manager/src/data-source.ts index 16fe2322ad..1ca75e2550 100644 --- a/packages/core/data-source-manager/src/data-source.ts +++ b/packages/core/data-source-manager/src/data-source.ts @@ -28,6 +28,10 @@ export abstract class DataSource { return this.options.name; } + static testConnection(options?: any): Promise { + return Promise.resolve(true); + } + collectionToResourceMiddleware() { return async (ctx, next) => { const params = parseRequest( 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 8dff86e1b2..905ee14111 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 @@ -1,6 +1,5 @@ import { Application, Plugin } from '@nocobase/server'; import { resolve } from 'path'; -import { Database } from '@nocobase/database'; import remoteCollectionsResourcer from './resourcers/data-sources-collections'; import remoteFieldsResourcer from './resourcers/data-sources-collections-fields'; import { DataSourcesCollectionModel } from './models/data-sources-collection-model'; @@ -56,17 +55,17 @@ export class PluginDataSourceManagerServer extends Plugin { }); this.app.actions({ - async ['databaseConnections:testConnection'](ctx, next) { + async ['dataSources:testConnection'](ctx, next) { const { values } = ctx.action.params; - const db = new Database(values); + const { options, type } = values; + + const klass = this.app.dataSourceManager.factory.getClass(type); try { - await db.sequelize.authenticate(); + await klass.testConnection(options); } catch (error) { - throw new Error(`Unable to connect to the database: ${error.message}`); - } finally { - await db.close(); + throw new Error(`Test connection failed: ${error.message}`); } ctx.body = {