mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
* feat: environments plugin * feat: improve code * fix: improve code * feat: improve code * refactor: package description * feat: bulk import * fix: remove * refactor: file manager support environment variables * refactor: file manager support environment variables * refactor: map manager support environment variables * refactor: support environment variables * refactor: support environment variables * refactor: support delete environment variables * fix: bug * refactor: workflow support environment variables * refactor: email environment variables * refactor: support bulk import * refactor: support bulk import * refactor: support bulk import * refactor: support bulk import * refactor: code improve * feat: env * chore: update * feat: environment * fix: bug * fix: acl snippet * fix: acl snippets * chore: map manager * refactor: support line break * refactor: support password * chore: environment variables * fix: bug * fix: bug * chore: enviroment variables * chore: system settings * fix: improve code * feat: verification * feat: map * feat: file-manager * feat: notification * fix: bug * feat: workflow * fix: improve code * fix: bug * feat: data-source * feat: auth * fix: error * fix: bug * refactor: description * refactor: locale * refactor: locale * refactor: locale * refactor: code improve * refactor: locale * refactor: locale * style: style improve * fix: error * fix: bug * fix: bug * refactor: environment * fix: ellipsis * refactor: password * fix: bug * fix: bug * fix: bug * fix: bug * fix: bug * chore: test * fix: cache * fix: mysql dialect options * refactor: email config form * fix: bug * fix: bug * fix: authenticator.dataValues parse * fix: include undefined * fix: json * fix: json parse * chore: enviromentProvider * fix: acl * fix: rowKey * fix: update ProviderOptions.tsx * feat: get app instance * fix: bug * fix: text * fix: build error * fix: error * chore: migration rules options * chore: migration rules * refactor: code improve * feat: env v2 * chore: environment varibales * chore: environment serve * fix: getVariables * feat: improve code * fix: bug * chore: collection options for migration * chore: tree collection options * chore: migration rules * chore: migration rules * chore: env api * chore: env api * fix: optionsKeysNotAllowedInEnv * fix: required true * fix: improve code * fix: app refresh * fix: remove db.import * fix: type error * fix: map * refactor: locale improve * refactor: tx-cos * fix: undefined * refactor: code improve * chore: use bookworm * fix: npm add user * fix: npm login * fix: npm adduser * fix: npm adduser * fix: expect * fix: expect * fix: environmentVariables * refactor: support bulk delete & filter * refactor: locale improve * feat: filter * refactor: useGlobalVariable * fix: scope * fix: bug * fix: optionsKeysNotAllowedInEnv * fix: test error * fix: test * fix: test * feat: improve code --------- Co-authored-by: chenos <chenlinxh@gmail.com> Co-authored-by: Chareice <chareice@live.com>
135 lines
3.4 KiB
TypeScript
135 lines
3.4 KiB
TypeScript
/**
|
|
* This file is part of the NocoBase (R) project.
|
|
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
* Authors: NocoBase Team.
|
|
*
|
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
*/
|
|
|
|
import { createConsoleLogger, createLogger, Logger, LoggerOptions } from '@nocobase/logger';
|
|
import { ToposortOptions } from '@nocobase/utils';
|
|
import { DataSource } from './data-source';
|
|
import { DataSourceFactory } from './data-source-factory';
|
|
|
|
type DataSourceHook = (dataSource: DataSource) => void;
|
|
|
|
type DataSourceManagerOptions = {
|
|
logger?: LoggerOptions | Logger;
|
|
app?: any;
|
|
};
|
|
|
|
export class DataSourceManager {
|
|
dataSources: Map<string, DataSource>;
|
|
/**
|
|
* @internal
|
|
*/
|
|
factory: DataSourceFactory;
|
|
protected middlewares = [];
|
|
private onceHooks: Array<DataSourceHook> = [];
|
|
private beforeAddHooks: Array<DataSourceHook> = [];
|
|
|
|
constructor(public options: DataSourceManagerOptions = {}) {
|
|
this.dataSources = new Map();
|
|
this.factory = new DataSourceFactory(this);
|
|
this.middlewares = [];
|
|
|
|
if (options.app) {
|
|
options.app.on('beforeStop', async () => {
|
|
for (const dataSource of this.dataSources.values()) {
|
|
await dataSource.close();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
get(dataSourceKey: string) {
|
|
return this.dataSources.get(dataSourceKey);
|
|
}
|
|
|
|
async add(dataSource: DataSource, options: any = {}) {
|
|
let logger;
|
|
|
|
if (this.options.logger) {
|
|
if (typeof this.options.logger['log'] === 'function') {
|
|
logger = this.options.logger as Logger;
|
|
} else {
|
|
logger = createLogger(this.options.logger);
|
|
}
|
|
} else {
|
|
logger = createConsoleLogger();
|
|
}
|
|
|
|
dataSource.setLogger(logger);
|
|
|
|
for (const hook of this.beforeAddHooks) {
|
|
hook(dataSource);
|
|
}
|
|
|
|
const oldDataSource = this.dataSources.get(dataSource.name);
|
|
|
|
if (oldDataSource) {
|
|
await oldDataSource.close();
|
|
}
|
|
|
|
await dataSource.load(options);
|
|
this.dataSources.set(dataSource.name, dataSource);
|
|
|
|
for (const hook of this.onceHooks) {
|
|
hook(dataSource);
|
|
}
|
|
}
|
|
|
|
use(fn: any, options?: ToposortOptions) {
|
|
this.middlewares.push([fn, options]);
|
|
}
|
|
|
|
middleware() {
|
|
const self = this;
|
|
|
|
return async function dataSourceManager(ctx, next) {
|
|
const name = ctx.get('x-data-source') || 'main';
|
|
|
|
if (!self.dataSources.has(name)) {
|
|
ctx.throw(`data source ${name} does not exist`);
|
|
}
|
|
|
|
const ds = self.dataSources.get(name);
|
|
ctx.dataSource = ds;
|
|
|
|
const composedFn = ds.middleware(self.middlewares);
|
|
return composedFn(ctx, next);
|
|
};
|
|
}
|
|
|
|
registerDataSourceType(type: string, DataSourceClass: typeof DataSource) {
|
|
this.factory.register(type, DataSourceClass);
|
|
}
|
|
|
|
getDataSourceType(type: string): typeof DataSource | undefined {
|
|
return this.factory.getClass(type);
|
|
}
|
|
|
|
buildDataSourceByType(type: string, options: any = {}): DataSource {
|
|
return this.factory.create(type, options);
|
|
}
|
|
|
|
beforeAddDataSource(hook: DataSourceHook) {
|
|
this.beforeAddHooks.push(hook);
|
|
for (const dataSource of this.dataSources.values()) {
|
|
hook(dataSource);
|
|
}
|
|
}
|
|
|
|
afterAddDataSource(hook: DataSourceHook) {
|
|
this.addHookAndRun(hook);
|
|
}
|
|
|
|
private addHookAndRun(hook: DataSourceHook) {
|
|
this.onceHooks.push(hook);
|
|
for (const dataSource of this.dataSources.values()) {
|
|
hook(dataSource);
|
|
}
|
|
}
|
|
}
|