2023-02-14 07:30:58 +00:00
|
|
|
import { Model, Transactionable } from '@nocobase/database';
|
2022-03-28 14:01:10 +00:00
|
|
|
import { Application } from '@nocobase/server';
|
2023-02-14 07:30:58 +00:00
|
|
|
import { AppDbCreator, AppOptionsFactory } from '../server';
|
2022-03-28 14:01:10 +00:00
|
|
|
|
2022-05-22 00:48:19 +00:00
|
|
|
export interface registerAppOptions extends Transactionable {
|
2022-03-28 14:01:10 +00:00
|
|
|
skipInstall?: boolean;
|
2023-02-14 07:30:58 +00:00
|
|
|
dbCreator: AppDbCreator;
|
|
|
|
appOptionsFactory: AppOptionsFactory;
|
2022-03-28 14:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ApplicationModel extends Model {
|
2023-02-16 01:46:23 +00:00
|
|
|
static async handleAppStart(mainApp: Application, app: Application, options: registerAppOptions) {
|
2023-02-24 02:44:23 +00:00
|
|
|
await mainApp.emitAsync('beforeSubAppLoad', {
|
2023-02-16 01:46:23 +00:00
|
|
|
mainApp,
|
|
|
|
subApp: app,
|
|
|
|
});
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
await app.load();
|
2023-02-14 07:30:58 +00:00
|
|
|
|
|
|
|
if (!(await app.isInstalled())) {
|
2023-02-26 00:04:29 +00:00
|
|
|
await app.db.sync();
|
2023-03-10 11:16:00 +00:00
|
|
|
|
|
|
|
await mainApp.emitAsync('beforeSubAppInstall', {
|
|
|
|
subApp: app,
|
|
|
|
mainApp,
|
|
|
|
});
|
|
|
|
|
2022-04-29 12:00:50 +00:00
|
|
|
await app.install();
|
2023-02-26 00:04:29 +00:00
|
|
|
|
|
|
|
// emit an event on mainApp
|
|
|
|
// current if you add listener on subApp through `subApp.on('afterInstall')` , it will be clear after subApp installed
|
|
|
|
await mainApp.emitAsync('afterSubAppInstalled', {
|
|
|
|
mainApp,
|
|
|
|
subApp: app,
|
|
|
|
});
|
2022-04-29 12:00:50 +00:00
|
|
|
}
|
2023-02-14 07:30:58 +00:00
|
|
|
|
2022-04-29 12:00:50 +00:00
|
|
|
await app.start();
|
|
|
|
}
|
|
|
|
|
2022-03-28 14:01:10 +00:00
|
|
|
async registerToMainApp(mainApp: Application, options: registerAppOptions) {
|
|
|
|
const appName = this.get('name') as string;
|
2022-04-24 12:22:50 +00:00
|
|
|
const appOptions = (this.get('options') as any) || {};
|
2022-03-28 14:01:10 +00:00
|
|
|
|
2022-04-30 15:41:01 +00:00
|
|
|
const AppModel = this.constructor as typeof ApplicationModel;
|
|
|
|
|
2023-02-14 07:30:58 +00:00
|
|
|
const app = mainApp.appManager.createApplication(appName, {
|
|
|
|
...options.appOptionsFactory(appName, mainApp),
|
|
|
|
...appOptions,
|
2023-02-16 01:46:23 +00:00
|
|
|
name: appName,
|
2023-02-14 07:30:58 +00:00
|
|
|
});
|
2022-03-28 14:01:10 +00:00
|
|
|
|
2023-02-14 07:30:58 +00:00
|
|
|
const isInstalled = await (async () => {
|
|
|
|
try {
|
|
|
|
return await app.isInstalled();
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message.includes('does not exist') || e.message.includes('Unknown database')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw e;
|
2022-03-28 14:01:10 +00:00
|
|
|
}
|
2023-02-14 07:30:58 +00:00
|
|
|
})();
|
2022-09-18 06:10:01 +00:00
|
|
|
|
2023-02-14 07:30:58 +00:00
|
|
|
if (!isInstalled) {
|
|
|
|
await options.dbCreator(app);
|
2022-09-18 06:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 01:46:23 +00:00
|
|
|
await AppModel.handleAppStart(mainApp, app, options);
|
2022-04-30 15:41:01 +00:00
|
|
|
|
|
|
|
await AppModel.update(
|
|
|
|
{
|
|
|
|
status: 'running',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
transaction: options.transaction,
|
|
|
|
where: {
|
|
|
|
[AppModel.primaryKeyAttribute]: this.get(AppModel.primaryKeyAttribute),
|
|
|
|
},
|
|
|
|
hooks: false,
|
|
|
|
},
|
|
|
|
);
|
2022-03-28 14:01:10 +00:00
|
|
|
}
|
|
|
|
}
|