2022-03-28 22:01:10 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
|
|
|
import { resolve } from 'path';
|
|
|
|
import { ApplicationModel } from './models/application';
|
|
|
|
import { AppManager } from '@nocobase/server';
|
|
|
|
|
2022-04-24 20:22:50 +08:00
|
|
|
export class PluginMultiAppManager extends Plugin {
|
2022-03-28 22:01:10 +08:00
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
this.db.registerModels({
|
|
|
|
ApplicationModel,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.db.import({
|
|
|
|
directory: resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.db.on('applications.afterCreateWithAssociations', async (model: ApplicationModel, options) => {
|
|
|
|
const { transaction } = options;
|
2022-04-29 20:00:50 +08:00
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
await model.registerToMainApp(this.app, { transaction });
|
|
|
|
});
|
|
|
|
|
|
|
|
this.db.on('applications.afterDestroy', async (model: ApplicationModel) => {
|
|
|
|
await this.app.appManager.removeApplication(model.get('name') as string);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.appManager.on(
|
|
|
|
'beforeGetApplication',
|
|
|
|
async function lazyLoadApplication({ appManager, name }: { appManager: AppManager; name: string }) {
|
|
|
|
if (!appManager.applications.has(name)) {
|
|
|
|
const existsApplication = (await this.app.db.getRepository('applications').findOne({
|
|
|
|
filter: {
|
|
|
|
name,
|
|
|
|
},
|
|
|
|
})) as ApplicationModel | null;
|
|
|
|
|
|
|
|
if (existsApplication) {
|
|
|
|
await existsApplication.registerToMainApp(this.app, { skipInstall: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|