chore: testConnection api

This commit is contained in:
Chareice 2024-01-26 20:50:03 +08:00
parent 805b7903a0
commit f0343df18d
No known key found for this signature in database
3 changed files with 14 additions and 7 deletions

View File

@ -7,6 +7,10 @@ export class DataSourceFactory {
this.collectionTypes.set(type, dataSourceClass); this.collectionTypes.set(type, dataSourceClass);
} }
getClass(type: string): typeof DataSource {
return this.collectionTypes.get(type);
}
create(type: string, options: any = {}): DataSource { create(type: string, options: any = {}): DataSource {
const klass = this.collectionTypes.get(type); const klass = this.collectionTypes.get(type);
if (!klass) { if (!klass) {

View File

@ -28,6 +28,10 @@ export abstract class DataSource {
return this.options.name; return this.options.name;
} }
static testConnection(options?: any): Promise<boolean> {
return Promise.resolve(true);
}
collectionToResourceMiddleware() { collectionToResourceMiddleware() {
return async (ctx, next) => { return async (ctx, next) => {
const params = parseRequest( const params = parseRequest(

View File

@ -1,6 +1,5 @@
import { Application, Plugin } from '@nocobase/server'; import { Application, Plugin } from '@nocobase/server';
import { resolve } from 'path'; import { resolve } from 'path';
import { Database } from '@nocobase/database';
import remoteCollectionsResourcer from './resourcers/data-sources-collections'; import remoteCollectionsResourcer from './resourcers/data-sources-collections';
import remoteFieldsResourcer from './resourcers/data-sources-collections-fields'; import remoteFieldsResourcer from './resourcers/data-sources-collections-fields';
import { DataSourcesCollectionModel } from './models/data-sources-collection-model'; import { DataSourcesCollectionModel } from './models/data-sources-collection-model';
@ -56,17 +55,17 @@ export class PluginDataSourceManagerServer extends Plugin {
}); });
this.app.actions({ this.app.actions({
async ['databaseConnections:testConnection'](ctx, next) { async ['dataSources:testConnection'](ctx, next) {
const { values } = ctx.action.params; const { values } = ctx.action.params;
const db = new Database(values); const { options, type } = values;
const klass = this.app.dataSourceManager.factory.getClass(type);
try { try {
await db.sequelize.authenticate(); await klass.testConnection(options);
} catch (error) { } catch (error) {
throw new Error(`Unable to connect to the database: ${error.message}`); throw new Error(`Test connection failed: ${error.message}`);
} finally {
await db.close();
} }
ctx.body = { ctx.body = {