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);
}
getClass(type: string): typeof DataSource {
return this.collectionTypes.get(type);
}
create(type: string, options: any = {}): DataSource {
const klass = this.collectionTypes.get(type);
if (!klass) {

View File

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

View File

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