mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 19:47:26 +00:00
chore: testConnection api
This commit is contained in:
parent
805b7903a0
commit
f0343df18d
@ -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) {
|
||||||
|
@ -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(
|
||||||
|
@ -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 = {
|
||||||
|
Loading…
Reference in New Issue
Block a user