tachybase_todo/packages/plugins/multi-app-manager/src/server.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-04-30 23:41:01 +08:00
import { AppManager, InstallOptions, Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { ApplicationModel } from './models/application';
export class PluginMultiAppManager extends Plugin {
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');
}
}
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;
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 });
}
}
},
);
}
}