From d61ad37be7c616b84b366ac993d930a2ebfc9547 Mon Sep 17 00:00:00 2001 From: Chareice Date: Thu, 28 Dec 2023 15:22:04 +0800 Subject: [PATCH] feat: support multiple database instances in application --- packages/core/server/src/application.ts | 39 +++++++++++++++---------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index c379f26bb0..fbecbd1dda 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -4,14 +4,14 @@ import { actions as authActions, AuthManager, AuthManagerOptions } from '@nocoba import { Cache, CacheManager, CacheManagerOptions } from '@nocobase/cache'; import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/database'; import { - AppLoggerOptions, - createLogger, - createAppLogger, AppLogger, - LoggerOptions, + AppLoggerOptions, + createAppLogger, + createLogger, getLoggerFilePath, + LoggerOptions, } from '@nocobase/logger'; -import { Resourcer, ResourceOptions } from '@nocobase/resourcer'; +import { ResourceOptions, Resourcer } from '@nocobase/resourcer'; import { applyMixins, AsyncEmitter, measureExecutionTime, Toposort, ToposortOptions } from '@nocobase/utils'; import { Command, CommandOptions, ParseOptions } from 'commander'; import { IncomingMessage, Server, ServerResponse } from 'http'; @@ -30,9 +30,9 @@ import { createAppProxy, createI18n, createResourcer, + enablePerfHooks, getCommandFullName, registerMiddlewares, - enablePerfHooks, } from './helper'; import { ApplicationVersion } from './helpers/application-version'; import { Locale } from './locale'; @@ -41,8 +41,7 @@ import { InstallOptions, PluginManager } from './plugin-manager'; import { randomUUID } from 'crypto'; import packageJson from '../package.json'; import chalk from 'chalk'; -import { RecordableHistogram, performance } from 'node:perf_hooks'; -import path from 'path'; +import { RecordableHistogram } from 'node:perf_hooks'; export type PluginType = string | typeof Plugin; export type PluginConfiguration = PluginType | [PluginType, any]; @@ -141,6 +140,7 @@ export class Application exten private _maintainingCommandStatus: MaintainingCommandStatus; private _maintainingStatusBeforeCommand: MaintainingCommandStatus | null; private _actionCommand: Command; + private _databases: Map = new Map(); constructor(public options: ApplicationOptions) { super(); @@ -169,10 +169,8 @@ export class Application exten return this._cronJobManager; } - protected _db: Database; - get db() { - return this._db; + return this.getDb(); } protected _logger: AppLogger; @@ -346,7 +344,8 @@ export class Application exten if (options?.reload) { this.setMaintainingMessage('app reload'); this.log.info(`app.reload()`, { method: 'load' }); - const oldDb = this._db; + const oldDb = this.getDb(); + this.init(); if (!oldDb.closed()) { await oldDb.close(); @@ -698,6 +697,9 @@ export class Application exten dirname: getLoggerFilePath(this.name || 'main', dirname || ''), }); } + getDb(name = 'main') { + return this._databases.get(name); + } protected init() { const options = this.options; @@ -718,17 +720,18 @@ export class Application exten this._cronJobManager = new CronJobManager(this); - if (this._db) { + if (this.getDb()) { // MaxListenersExceededWarning - this._db.removeAllListeners(); + this.getDb().removeAllListeners(); } - this._db = this.createDatabase(options); + this.setDb(this.createDatabase(options)); this._resourcer = createResourcer(options); this._cli = this.createCli(); this._i18n = createI18n(options); - this.context.db = this._db; + this.context.db = this.getDb(); + // this.context.logger = this._logger; this.context.resourcer = this._resourcer; this.context.cacheManager = this._cacheManager; @@ -800,6 +803,10 @@ export class Application exten }); return db; } + + private setDb(db: Database, name = 'main') { + this._databases.set(name, db); + } } applyMixins(Application, [AsyncEmitter]);