mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 15:37:36 +00:00
chore: update dataSources
This commit is contained in:
parent
931a69ccad
commit
3c6aba95c9
@ -51,6 +51,59 @@ describe('data source', async () => {
|
||||
expect(listResp2.body.data[0].status).toBe('loaded');
|
||||
});
|
||||
|
||||
it('should not load connection after change enabled status', async () => {
|
||||
const testConnectionFn = vi.fn();
|
||||
|
||||
class MockDataSource extends DataSource {
|
||||
static testConnection(options?: any): Promise<boolean> {
|
||||
testConnectionFn();
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
async load(): Promise<void> {}
|
||||
|
||||
createCollectionManager(options?: any): any {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
app.dataSourceManager.factory.register('mock', MockDataSource);
|
||||
|
||||
await app
|
||||
.agent()
|
||||
.resource('dataSources')
|
||||
.create({
|
||||
values: {
|
||||
options: {},
|
||||
type: 'mock',
|
||||
key: 'mockInstance1',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
testConnectionFn.mockClear();
|
||||
|
||||
const findDataSourceInstance = async () => {
|
||||
return await app.db.getRepository('dataSources').findOne({
|
||||
filterByTk: 'mockInstance1',
|
||||
});
|
||||
};
|
||||
|
||||
expect((await findDataSourceInstance()).get('enabled')).toBeTruthy();
|
||||
|
||||
await app
|
||||
.agent()
|
||||
.resource('dataSources')
|
||||
.update({
|
||||
filterByTk: 'mockInstance1',
|
||||
values: {
|
||||
enabled: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(testConnectionFn).toBeCalledTimes(0);
|
||||
expect((await findDataSourceInstance()).get('enabled')).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should call test connection after options change', async () => {
|
||||
const testConnectionFn = vi.fn();
|
||||
|
||||
@ -92,11 +145,14 @@ describe('data source', async () => {
|
||||
.resource('dataSources')
|
||||
.update({
|
||||
filterByTk: 'mockInstance1',
|
||||
options: {
|
||||
otherOptions: 'test',
|
||||
values: {
|
||||
options: {
|
||||
otherOptions: 'test',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await waitSecond(1000);
|
||||
expect(testConnectionFn).toBeCalledTimes(1);
|
||||
});
|
||||
|
||||
|
@ -14,6 +14,10 @@ export default defineCollection({
|
||||
type: 'string',
|
||||
name: 'displayName',
|
||||
},
|
||||
{
|
||||
type: 'string',
|
||||
name: 'type',
|
||||
},
|
||||
{
|
||||
type: 'json',
|
||||
name: 'options',
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { MagicAttributeModel, Transaction } from '@nocobase/database';
|
||||
import { Model, Transaction } from '@nocobase/database';
|
||||
import { Application } from '@nocobase/server';
|
||||
import { LocalData } from '../services/database-introspector';
|
||||
import { setCurrentRole } from '@nocobase/plugin-acl';
|
||||
@ -44,7 +44,7 @@ const availableActions: {
|
||||
},
|
||||
};
|
||||
|
||||
export class DataSourceModel extends MagicAttributeModel {
|
||||
export class DataSourceModel extends Model {
|
||||
async loadIntoApplication(options: { app: Application; transaction?: Transaction }) {
|
||||
const { app, transaction } = options;
|
||||
|
||||
|
@ -39,22 +39,26 @@ export class PluginDataSourceManagerServer extends Plugin {
|
||||
});
|
||||
|
||||
this.app.db.on('dataSources.beforeSave', async (model: DataSourceModel) => {
|
||||
const dataSourceOptions = model.get('options');
|
||||
const type = model.get('type');
|
||||
if (model.changed('options')) {
|
||||
const dataSourceOptions = model.get('options');
|
||||
const type = model.get('type');
|
||||
|
||||
const klass = this.app.dataSourceManager.factory.getClass(type);
|
||||
const klass = this.app.dataSourceManager.factory.getClass(type);
|
||||
|
||||
try {
|
||||
await klass.testConnection(dataSourceOptions);
|
||||
} catch (error) {
|
||||
throw new Error(`Test connection failed: ${error.message}`);
|
||||
try {
|
||||
await klass.testConnection(dataSourceOptions);
|
||||
} catch (error) {
|
||||
throw new Error(`Test connection failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.app.db.on('dataSources.afterSave', async (model: DataSourceModel, options) => {
|
||||
model.loadIntoApplication({
|
||||
app: this.app,
|
||||
});
|
||||
if (model.changed('options')) {
|
||||
model.loadIntoApplication({
|
||||
app: this.app,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
this.app.db.on('dataSources.afterCreate', async (model: DataSourceModel, options) => {
|
||||
@ -74,6 +78,7 @@ export class PluginDataSourceManagerServer extends Plugin {
|
||||
|
||||
this.app.use(async (ctx, next) => {
|
||||
await next();
|
||||
|
||||
const { actionName, resourceName, params } = ctx.action;
|
||||
|
||||
if (resourceName === 'dataSources' && actionName == 'list') {
|
||||
|
Loading…
Reference in New Issue
Block a user