tachybase_todo/packages/plugins/multi-app-manager/src/server.ts
chenos 249dff16d3
refactor: plugin manager (#965)
* feat: improve code

* chore: update version

* feat: api service

* fix: api services

* feat: improve code

* feat: improve code

* feat: improve code

* feat: pm socket

* fix: test errors

* feat: add built-in plugins before upgrade

* feat: update docs

* feat: improve code

* fix: after load
2022-10-27 13:00:16 +08:00

51 lines
1.5 KiB
TypeScript

import { AppManager, InstallOptions, Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { ApplicationModel } from './models/application';
export class PluginMultiAppManager extends Plugin {
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 });
}
}
},
);
}
}