chore: install subApp asynchronous (#336)

* chore: install subApp asynchronous

* fix: test

* chore: application handleAppStart

* chore: remove application createBy field

Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
ChengLei Shao 2022-04-29 20:00:50 +08:00 committed by GitHub
parent ebc1a47721
commit 687e1f4bc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import { mockServer, MockServer } from '@nocobase/test';
import { Database } from '@nocobase/database';
import { PluginMultiAppManager } from '../server';
import { ApplicationModel } from '..';
describe('multiple apps create', () => {
let app: MockServer;
@ -90,4 +91,20 @@ describe('multiple apps create', () => {
expect(app.appManager.applications.has('miniApp')).toBeTruthy();
});
it('should change handleAppStart', async () => {
const customHandler = jest.fn();
ApplicationModel.handleAppStart = customHandler;
await db.getRepository('applications').create({
values: {
name: 'miniApp',
options: {
plugins: ['@nocobase/plugin-ui-schema-storage'],
},
},
});
expect(customHandler).toHaveBeenCalledTimes(1);
});
});

View File

@ -6,7 +6,6 @@ export default defineCollection({
autoGenId: false,
title: '{{t("Applications")}}',
sortable: 'sort',
createdBy: true,
filterTargetKey: 'name',
fields: [
{

View File

@ -1 +1,4 @@
import { ApplicationModel, registerAppOptions } from './models/application';
export { PluginMultiAppManager as default } from './server';
export { ApplicationModel, registerAppOptions };

View File

@ -3,7 +3,7 @@ import { Application } from '@nocobase/server';
import lodash from 'lodash';
import * as path from 'path';
interface registerAppOptions extends TransactionAble {
export interface registerAppOptions extends TransactionAble {
skipInstall?: boolean;
}
@ -16,8 +16,17 @@ export class ApplicationModel extends Model {
);
}
static async handleAppStart(app: Application, options: registerAppOptions) {
await app.load();
if (!lodash.get(options, 'skipInstall', false)) {
await app.install();
}
await app.start();
}
async registerToMainApp(mainApp: Application, options: registerAppOptions) {
const { transaction } = options;
const appName = this.get('name') as string;
const appOptions = (this.get('options') as any) || {};
@ -26,6 +35,7 @@ export class ApplicationModel extends Model {
...appOptions,
});
// create database before installation if it not exists
app.on('beforeInstall', async function createDatabase() {
const { host, port, username, password, database, dialect } = ApplicationModel.getDatabaseConfig(app);
@ -56,13 +66,7 @@ export class ApplicationModel extends Model {
}
});
await app.load();
if (!lodash.get(options, 'skipInstall', false)) {
await app.install();
}
await app.start();
await ApplicationModel.handleAppStart(app, options);
}
static initOptions(appName: string, mainApp: Application) {

View File

@ -19,6 +19,7 @@ export class PluginMultiAppManager extends Plugin {
this.db.on('applications.afterCreateWithAssociations', async (model: ApplicationModel, options) => {
const { transaction } = options;
await model.registerToMainApp(this.app, { transaction });
});