chore: merge sub app database options (#3640)
This commit is contained in:
		
							parent
							
								
									b6d17853d8
								
							
						
					
					
						commit
						599a1aa0c6
					
				@ -1,6 +1,6 @@
 | 
				
			|||||||
import { Database } from '@nocobase/database';
 | 
					import { Database } from '@nocobase/database';
 | 
				
			||||||
import { AppSupervisor, Gateway } from '@nocobase/server';
 | 
					import { AppSupervisor, Gateway } from '@nocobase/server';
 | 
				
			||||||
import { MockServer, createMockServer } from '@nocobase/test';
 | 
					import { createMockServer, MockServer } from '@nocobase/test';
 | 
				
			||||||
import { uid } from '@nocobase/utils';
 | 
					import { uid } from '@nocobase/utils';
 | 
				
			||||||
import { vi } from 'vitest';
 | 
					import { vi } from 'vitest';
 | 
				
			||||||
import { PluginMultiAppManager } from '../server';
 | 
					import { PluginMultiAppManager } from '../server';
 | 
				
			||||||
@ -22,6 +22,29 @@ describe('multiple apps', () => {
 | 
				
			|||||||
    await app.destroy();
 | 
					    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 () => {
 | 
					  it('should register db creator', async () => {
 | 
				
			||||||
    const fn = vi.fn();
 | 
					    const fn = vi.fn();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
import { Model, Transactionable } from '@nocobase/database';
 | 
					import { Model, Transactionable } from '@nocobase/database';
 | 
				
			||||||
import { Application } from '@nocobase/server';
 | 
					import { Application } from '@nocobase/server';
 | 
				
			||||||
import { AppOptionsFactory } from '../server';
 | 
					import { AppOptionsFactory } from '../server';
 | 
				
			||||||
 | 
					import { merge } from '@nocobase/utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface registerAppOptions extends Transactionable {
 | 
					export interface registerAppOptions extends Transactionable {
 | 
				
			||||||
  skipInstall?: boolean;
 | 
					  skipInstall?: boolean;
 | 
				
			||||||
@ -10,11 +11,12 @@ export interface registerAppOptions extends Transactionable {
 | 
				
			|||||||
export class ApplicationModel extends Model {
 | 
					export class ApplicationModel extends Model {
 | 
				
			||||||
  registerToSupervisor(mainApp: Application, options: registerAppOptions) {
 | 
					  registerToSupervisor(mainApp: Application, options: registerAppOptions) {
 | 
				
			||||||
    const appName = this.get('name') as string;
 | 
					    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 = {
 | 
					    const subAppOptions = {
 | 
				
			||||||
      ...options.appOptionsFactory(appName, mainApp),
 | 
					      ...(merge(appOptions, appModelOptions) as object),
 | 
				
			||||||
      ...appOptions,
 | 
					 | 
				
			||||||
      name: appName,
 | 
					      name: appName,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,5 @@
 | 
				
			|||||||
import { Database, IDatabaseOptions, Transactionable } from '@nocobase/database';
 | 
					import { Database, IDatabaseOptions, Transactionable } from '@nocobase/database';
 | 
				
			||||||
import Application, { AppSupervisor, Gateway, Plugin } from '@nocobase/server';
 | 
					import Application, { AppSupervisor, Gateway, Plugin } from '@nocobase/server';
 | 
				
			||||||
import { Mutex } from 'async-mutex';
 | 
					 | 
				
			||||||
import lodash from 'lodash';
 | 
					import lodash from 'lodash';
 | 
				
			||||||
import path from 'path';
 | 
					import path from 'path';
 | 
				
			||||||
import { ApplicationModel } from '../server';
 | 
					import { ApplicationModel } from '../server';
 | 
				
			||||||
@ -125,8 +124,6 @@ export class PluginMultiAppManager extends Plugin {
 | 
				
			|||||||
  appOptionsFactory: AppOptionsFactory = defaultAppOptionsFactory;
 | 
					  appOptionsFactory: AppOptionsFactory = defaultAppOptionsFactory;
 | 
				
			||||||
  subAppUpgradeHandler: SubAppUpgradeHandler = defaultSubAppUpgradeHandle;
 | 
					  subAppUpgradeHandler: SubAppUpgradeHandler = defaultSubAppUpgradeHandle;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private beforeGetApplicationMutex = new Mutex();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  static getDatabaseConfig(app: Application): IDatabaseOptions {
 | 
					  static getDatabaseConfig(app: Application): IDatabaseOptions {
 | 
				
			||||||
    let oldConfig =
 | 
					    let oldConfig =
 | 
				
			||||||
      app.options.database instanceof Database
 | 
					      app.options.database instanceof Database
 | 
				
			||||||
@ -136,6 +133,7 @@ export class PluginMultiAppManager extends Plugin {
 | 
				
			|||||||
    if (!oldConfig && app.db) {
 | 
					    if (!oldConfig && app.db) {
 | 
				
			||||||
      oldConfig = app.db.options;
 | 
					      oldConfig = app.db.options;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return lodash.cloneDeep(lodash.omit(oldConfig, ['migrator']));
 | 
					    return lodash.cloneDeep(lodash.omit(oldConfig, ['migrator']));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user