mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 03:02:19 +08:00
Merge branch 'main' into next
This commit is contained in:
commit
3d1d7a50cb
@ -10,9 +10,15 @@
|
|||||||
import { ToposortOptions } from '@nocobase/utils';
|
import { ToposortOptions } from '@nocobase/utils';
|
||||||
import { DataSource } from './data-source';
|
import { DataSource } from './data-source';
|
||||||
import { DataSourceFactory } from './data-source-factory';
|
import { DataSourceFactory } from './data-source-factory';
|
||||||
|
import { createConsoleLogger, createLogger, Logger, LoggerOptions } from '@nocobase/logger';
|
||||||
|
|
||||||
type DataSourceHook = (dataSource: DataSource) => void;
|
type DataSourceHook = (dataSource: DataSource) => void;
|
||||||
|
|
||||||
|
type DataSourceManagerOptions = {
|
||||||
|
logger?: LoggerOptions | Logger;
|
||||||
|
app?: any;
|
||||||
|
};
|
||||||
|
|
||||||
export class DataSourceManager {
|
export class DataSourceManager {
|
||||||
dataSources: Map<string, DataSource>;
|
dataSources: Map<string, DataSource>;
|
||||||
/**
|
/**
|
||||||
@ -23,9 +29,17 @@ export class DataSourceManager {
|
|||||||
private onceHooks: Array<DataSourceHook> = [];
|
private onceHooks: Array<DataSourceHook> = [];
|
||||||
private beforeAddHooks: Array<DataSourceHook> = [];
|
private beforeAddHooks: Array<DataSourceHook> = [];
|
||||||
|
|
||||||
constructor(public options = {}) {
|
constructor(public options: DataSourceManagerOptions = {}) {
|
||||||
this.dataSources = new Map();
|
this.dataSources = new Map();
|
||||||
this.middlewares = [];
|
this.middlewares = [];
|
||||||
|
|
||||||
|
if (options.app) {
|
||||||
|
options.app.on('beforeStop', async () => {
|
||||||
|
for (const dataSource of this.dataSources.values()) {
|
||||||
|
await dataSource.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get(dataSourceKey: string) {
|
get(dataSourceKey: string) {
|
||||||
@ -33,10 +47,30 @@ export class DataSourceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async add(dataSource: DataSource, options: any = {}) {
|
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) {
|
for (const hook of this.beforeAddHooks) {
|
||||||
hook(dataSource);
|
hook(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const oldDataSource = this.dataSources.get(dataSource.name);
|
||||||
|
|
||||||
|
if (oldDataSource) {
|
||||||
|
await oldDataSource.close();
|
||||||
|
}
|
||||||
|
|
||||||
await dataSource.load(options);
|
await dataSource.load(options);
|
||||||
this.dataSources.set(dataSource.name, dataSource);
|
this.dataSources.set(dataSource.name, dataSource);
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ import EventEmitter from 'events';
|
|||||||
import compose from 'koa-compose';
|
import compose from 'koa-compose';
|
||||||
import { loadDefaultActions } from './load-default-actions';
|
import { loadDefaultActions } from './load-default-actions';
|
||||||
import { ICollectionManager } from './types';
|
import { ICollectionManager } from './types';
|
||||||
|
import { Logger } from '@nocobase/logger';
|
||||||
|
|
||||||
export type DataSourceOptions = any;
|
export type DataSourceOptions = any;
|
||||||
|
|
||||||
@ -20,12 +21,17 @@ export abstract class DataSource extends EventEmitter {
|
|||||||
public collectionManager: ICollectionManager;
|
public collectionManager: ICollectionManager;
|
||||||
public resourceManager: ResourceManager;
|
public resourceManager: ResourceManager;
|
||||||
public acl: ACL;
|
public acl: ACL;
|
||||||
|
logger: Logger;
|
||||||
|
|
||||||
constructor(protected options: DataSourceOptions) {
|
constructor(protected options: DataSourceOptions) {
|
||||||
super();
|
super();
|
||||||
this.init(options);
|
this.init(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLogger(logger: Logger) {
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
get name() {
|
get name() {
|
||||||
return this.options.name;
|
return this.options.name;
|
||||||
}
|
}
|
||||||
@ -82,6 +88,7 @@ export abstract class DataSource extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async load(options: any = {}) {}
|
async load(options: any = {}) {}
|
||||||
|
async close() {}
|
||||||
|
|
||||||
abstract createCollectionManager(options?: any): ICollectionManager;
|
abstract createCollectionManager(options?: any): ICollectionManager;
|
||||||
|
|
||||||
|
@ -181,7 +181,6 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|||||||
modelHook: ModelHook;
|
modelHook: ModelHook;
|
||||||
delayCollectionExtend = new Map<string, { collectionOptions: CollectionOptions; mergeOptions?: any }[]>();
|
delayCollectionExtend = new Map<string, { collectionOptions: CollectionOptions; mergeOptions?: any }[]>();
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
collectionGroupManager = new CollectionGroupManager(this);
|
|
||||||
interfaceManager = new InterfaceManager(this);
|
interfaceManager = new InterfaceManager(this);
|
||||||
|
|
||||||
collectionFactory: CollectionFactory = new CollectionFactory(this);
|
collectionFactory: CollectionFactory = new CollectionFactory(this);
|
||||||
|
@ -54,3 +54,4 @@ export * from './view/view-inference';
|
|||||||
export * from './helpers';
|
export * from './helpers';
|
||||||
export { default as sqlParser, SQLParserTypes } from './sql-parser';
|
export { default as sqlParser, SQLParserTypes } from './sql-parser';
|
||||||
export * from './interfaces';
|
export * from './interfaces';
|
||||||
|
export { default as fieldTypeMap } from './view/field-type-map';
|
||||||
|
@ -20,5 +20,10 @@ export default function buildQueryInterface(db: Database) {
|
|||||||
sqlite: SqliteQueryInterface,
|
sqlite: SqliteQueryInterface,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const dialect = db.options.dialect;
|
||||||
|
if (!map[dialect]) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return new map[db.options.dialect](db);
|
return new map[db.options.dialect](db);
|
||||||
}
|
}
|
||||||
|
@ -94,4 +94,5 @@ const sqlite = {
|
|||||||
json: ['json', 'array'],
|
json: ['json', 'array'],
|
||||||
};
|
};
|
||||||
|
|
||||||
export default { postgres, mysql, sqlite, mariadb: mysql };
|
const fieldTypeMap = { postgres, mysql, sqlite, mariadb: mysql };
|
||||||
|
export default fieldTypeMap;
|
||||||
|
@ -1189,7 +1189,10 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
|
|||||||
useACL: options.acl,
|
useACL: options.acl,
|
||||||
});
|
});
|
||||||
|
|
||||||
this._dataSourceManager = new DataSourceManager();
|
this._dataSourceManager = new DataSourceManager({
|
||||||
|
logger: this.logger,
|
||||||
|
app: this,
|
||||||
|
});
|
||||||
|
|
||||||
// can not use await here
|
// can not use await here
|
||||||
this.dataSourceManager.dataSources.set('main', mainDataSourceInstance);
|
this.dataSourceManager.dataSources.set('main', mainDataSourceInstance);
|
||||||
|
@ -123,7 +123,7 @@ export class DataSourceModel extends Model {
|
|||||||
localData: await this.loadLocalData(),
|
localData: await this.loadLocalData(),
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
app.logger.error(`load data source failed, ${e}`);
|
app.logger.error(`load data source failed`, { cause: e });
|
||||||
|
|
||||||
if (pluginDataSourceManagerServer.dataSourceStatus[dataSourceKey] === 'loading') {
|
if (pluginDataSourceManagerServer.dataSourceStatus[dataSourceKey] === 'loading') {
|
||||||
pluginDataSourceManagerServer.dataSourceStatus[dataSourceKey] = 'loading-failed';
|
pluginDataSourceManagerServer.dataSourceStatus[dataSourceKey] = 'loading-failed';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user