chore: merge sub app database options (#3640)

This commit is contained in:
ChengLei Shao 2024-03-19 20:34:13 +08:00 committed by GitHub
parent b6d17853d8
commit 599a1aa0c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 7 deletions

View File

@ -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();

View File

@ -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,
};

View File

@ -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']));
}