Merge branch 'main' into next

This commit is contained in:
GitHub Actions Bot 2024-07-18 06:45:12 +00:00
commit 3d1d7a50cb
8 changed files with 55 additions and 5 deletions

View File

@ -10,9 +10,15 @@
import { ToposortOptions } from '@nocobase/utils';
import { DataSource } from './data-source';
import { DataSourceFactory } from './data-source-factory';
import { createConsoleLogger, createLogger, Logger, LoggerOptions } from '@nocobase/logger';
type DataSourceHook = (dataSource: DataSource) => void;
type DataSourceManagerOptions = {
logger?: LoggerOptions | Logger;
app?: any;
};
export class DataSourceManager {
dataSources: Map<string, DataSource>;
/**
@ -23,9 +29,17 @@ export class DataSourceManager {
private onceHooks: Array<DataSourceHook> = [];
private beforeAddHooks: Array<DataSourceHook> = [];
constructor(public options = {}) {
constructor(public options: DataSourceManagerOptions = {}) {
this.dataSources = new Map();
this.middlewares = [];
if (options.app) {
options.app.on('beforeStop', async () => {
for (const dataSource of this.dataSources.values()) {
await dataSource.close();
}
});
}
}
get(dataSourceKey: string) {
@ -33,10 +47,30 @@ export class DataSourceManager {
}
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);

View File

@ -13,6 +13,7 @@ import EventEmitter from 'events';
import compose from 'koa-compose';
import { loadDefaultActions } from './load-default-actions';
import { ICollectionManager } from './types';
import { Logger } from '@nocobase/logger';
export type DataSourceOptions = any;
@ -20,12 +21,17 @@ export abstract class DataSource extends EventEmitter {
public collectionManager: ICollectionManager;
public resourceManager: ResourceManager;
public acl: ACL;
logger: Logger;
constructor(protected options: DataSourceOptions) {
super();
this.init(options);
}
setLogger(logger: Logger) {
this.logger = logger;
}
get name() {
return this.options.name;
}
@ -82,6 +88,7 @@ export abstract class DataSource extends EventEmitter {
}
async load(options: any = {}) {}
async close() {}
abstract createCollectionManager(options?: any): ICollectionManager;

View File

@ -181,7 +181,6 @@ export class Database extends EventEmitter implements AsyncEmitter {
modelHook: ModelHook;
delayCollectionExtend = new Map<string, { collectionOptions: CollectionOptions; mergeOptions?: any }[]>();
logger: Logger;
collectionGroupManager = new CollectionGroupManager(this);
interfaceManager = new InterfaceManager(this);
collectionFactory: CollectionFactory = new CollectionFactory(this);

View File

@ -54,3 +54,4 @@ export * from './view/view-inference';
export * from './helpers';
export { default as sqlParser, SQLParserTypes } from './sql-parser';
export * from './interfaces';
export { default as fieldTypeMap } from './view/field-type-map';

View File

@ -20,5 +20,10 @@ export default function buildQueryInterface(db: Database) {
sqlite: SqliteQueryInterface,
};
const dialect = db.options.dialect;
if (!map[dialect]) {
return null;
}
return new map[db.options.dialect](db);
}

View File

@ -94,4 +94,5 @@ const sqlite = {
json: ['json', 'array'],
};
export default { postgres, mysql, sqlite, mariadb: mysql };
const fieldTypeMap = { postgres, mysql, sqlite, mariadb: mysql };
export default fieldTypeMap;

View File

@ -1189,7 +1189,10 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
useACL: options.acl,
});
this._dataSourceManager = new DataSourceManager();
this._dataSourceManager = new DataSourceManager({
logger: this.logger,
app: this,
});
// can not use await here
this.dataSourceManager.dataSources.set('main', mainDataSourceInstance);

View File

@ -123,7 +123,7 @@ export class DataSourceModel extends Model {
localData: await this.loadLocalData(),
});
} catch (e) {
app.logger.error(`load data source failed, ${e}`);
app.logger.error(`load data source failed`, { cause: e });
if (pluginDataSourceManagerServer.dataSourceStatus[dataSourceKey] === 'loading') {
pluginDataSourceManagerServer.dataSourceStatus[dataSourceKey] = 'loading-failed';