Merge branch 'feat/dataSource-manager' of github.com:nocobase/nocobase into feat/dataSource-manager

This commit is contained in:
katherinehhh 2024-01-26 21:00:12 +08:00
commit 54fd5eb0ce
3 changed files with 18 additions and 11 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 = {
@ -76,15 +75,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,
});