From 599a1aa0c6bd6f542e8fc457c85662bea32f130e Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 19 Mar 2024 20:34:13 +0800 Subject: [PATCH] chore: merge sub app database options (#3640) --- .../server/__tests__/multiple-apps.test.ts | 25 ++++++++++++++++++- .../src/server/models/application.ts | 8 +++--- .../src/server/server.ts | 4 +-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/__tests__/multiple-apps.test.ts b/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/__tests__/multiple-apps.test.ts index 738714c880..d18ef82de0 100644 --- a/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/__tests__/multiple-apps.test.ts +++ b/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/__tests__/multiple-apps.test.ts @@ -1,6 +1,6 @@ import { Database } from '@nocobase/database'; import { AppSupervisor, Gateway } from '@nocobase/server'; -import { MockServer, createMockServer } from '@nocobase/test'; +import { createMockServer, MockServer } from '@nocobase/test'; import { uid } from '@nocobase/utils'; import { vi } from 'vitest'; import { PluginMultiAppManager } from '../server'; @@ -22,6 +22,29 @@ describe('multiple apps', () => { await app.destroy(); }); + it('should merge database options', async () => { + const name = `td_${uid()}`; + + await db.getRepository('applications').create({ + values: { + name, + options: { + plugins: [], + database: { + underscored: true, + }, + }, + }, + context: { + waitSubAppInstall: true, + }, + }); + + const subApp = await AppSupervisor.getInstance().getApp(name); + + expect(subApp.db.options.underscored).toBeTruthy(); + }); + it('should register db creator', async () => { const fn = vi.fn(); diff --git a/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/models/application.ts b/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/models/application.ts index 48b1fe3892..27dc4983bf 100644 --- a/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/models/application.ts +++ b/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/models/application.ts @@ -1,6 +1,7 @@ import { Model, Transactionable } from '@nocobase/database'; import { Application } from '@nocobase/server'; import { AppOptionsFactory } from '../server'; +import { merge } from '@nocobase/utils'; export interface registerAppOptions extends Transactionable { skipInstall?: boolean; @@ -10,11 +11,12 @@ export interface registerAppOptions extends Transactionable { export class ApplicationModel extends Model { registerToSupervisor(mainApp: Application, options: registerAppOptions) { const appName = this.get('name') as string; - const appOptions = (this.get('options') as any) || {}; + const appModelOptions = (this.get('options') as any) || {}; + + const appOptions = options.appOptionsFactory(appName, mainApp); const subAppOptions = { - ...options.appOptionsFactory(appName, mainApp), - ...appOptions, + ...(merge(appOptions, appModelOptions) as object), name: appName, }; diff --git a/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/server.ts b/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/server.ts index cd1c5675ec..3aa6e32997 100644 --- a/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/server.ts +++ b/packages/plugins/@nocobase/plugin-multi-app-manager/src/server/server.ts @@ -1,6 +1,5 @@ import { Database, IDatabaseOptions, Transactionable } from '@nocobase/database'; import Application, { AppSupervisor, Gateway, Plugin } from '@nocobase/server'; -import { Mutex } from 'async-mutex'; import lodash from 'lodash'; import path from 'path'; import { ApplicationModel } from '../server'; @@ -125,8 +124,6 @@ export class PluginMultiAppManager extends Plugin { appOptionsFactory: AppOptionsFactory = defaultAppOptionsFactory; subAppUpgradeHandler: SubAppUpgradeHandler = defaultSubAppUpgradeHandle; - private beforeGetApplicationMutex = new Mutex(); - static getDatabaseConfig(app: Application): IDatabaseOptions { let oldConfig = app.options.database instanceof Database @@ -136,6 +133,7 @@ export class PluginMultiAppManager extends Plugin { if (!oldConfig && app.db) { oldConfig = app.db.options; } + return lodash.cloneDeep(lodash.omit(oldConfig, ['migrator'])); }