2022-04-30 23:41:01 +08:00
|
|
|
import { AppManager, InstallOptions, Plugin } from '@nocobase/server';
|
2022-03-28 22:01:10 +08:00
|
|
|
import { resolve } from 'path';
|
|
|
|
import { ApplicationModel } from './models/application';
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-04-30 23:41:01 +08:00
|
|
|
async install(options?: InstallOptions) {
|
|
|
|
const repo = this.db.getRepository<any>('collections');
|
|
|
|
if (repo) {
|
|
|
|
await repo.db2cm('applications');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|