chore: create sub app db with context (#2891)

This commit is contained in:
ChengLei Shao 2023-10-21 22:50:40 +08:00 committed by GitHub
parent 5fad821eab
commit ff22db5e5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,11 +3,9 @@ import Application, { AppSupervisor, Gateway, Plugin } from '@nocobase/server';
import { Mutex } from 'async-mutex'; import { Mutex } from 'async-mutex';
import lodash from 'lodash'; import lodash from 'lodash';
import path, { resolve } from 'path'; import path, { resolve } from 'path';
import qs from 'qs';
import { parse } from 'url';
import { ApplicationModel } from '../server'; import { ApplicationModel } from '../server';
export type AppDbCreator = (app: Application, transaction?: Transactionable) => Promise<void>; export type AppDbCreator = (app: Application, options?: Transactionable & { context?: any }) => Promise<void>;
export type AppOptionsFactory = (appName: string, mainApp: Application) => any; export type AppOptionsFactory = (appName: string, mainApp: Application) => any;
export type SubAppUpgradeHandler = (mainApp: Application) => Promise<void>; export type SubAppUpgradeHandler = (mainApp: Application) => Promise<void>;
@ -158,22 +156,28 @@ export class PluginMultiAppManager extends Plugin {
}); });
// after application created // after application created
this.db.on('applications.afterCreateWithAssociations', async (model: ApplicationModel, options) => { this.db.on(
const { transaction } = options; 'applications.afterCreateWithAssociations',
async (model: ApplicationModel, options: Transactionable & { context?: any }) => {
const { transaction } = options;
const subApp = model.registerToSupervisor(this.app, { const subApp = model.registerToSupervisor(this.app, {
appOptionsFactory: this.appOptionsFactory, appOptionsFactory: this.appOptionsFactory,
}); });
// create database // create database
await this.appDbCreator(subApp, transaction); await this.appDbCreator(subApp, {
transaction,
context: options.context,
});
const startPromise = subApp.runAsCLI(['start', '--quickstart'], { from: 'user' }); const startPromise = subApp.runAsCLI(['start', '--quickstart'], { from: 'user' });
if (options?.context?.waitSubAppInstall) { if (options?.context?.waitSubAppInstall) {
await startPromise; await startPromise;
} }
}); },
);
this.db.on('applications.afterDestroy', async (model: ApplicationModel) => { this.db.on('applications.afterDestroy', async (model: ApplicationModel) => {
await AppSupervisor.getInstance().removeApp(model.get('name') as string); await AppSupervisor.getInstance().removeApp(model.get('name') as string);