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