2022-05-22 08:48:19 +08:00
|
|
|
import Database, { IDatabaseOptions, Model, Transactionable } from '@nocobase/database';
|
2022-03-28 22:01:10 +08:00
|
|
|
import { Application } from '@nocobase/server';
|
|
|
|
import lodash from 'lodash';
|
|
|
|
import * as path from 'path';
|
|
|
|
|
2022-05-22 08:48:19 +08:00
|
|
|
export interface registerAppOptions extends Transactionable {
|
2022-03-28 22:01:10 +08:00
|
|
|
skipInstall?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ApplicationModel extends Model {
|
|
|
|
static getDatabaseConfig(app: Application): IDatabaseOptions {
|
|
|
|
return lodash.cloneDeep(
|
|
|
|
lodash.isPlainObject(app.options.database)
|
|
|
|
? (app.options.database as IDatabaseOptions)
|
|
|
|
: (app.options.database as Database).options,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-29 20:00:50 +08:00
|
|
|
static async handleAppStart(app: Application, options: registerAppOptions) {
|
2022-09-18 14:10:01 +08:00
|
|
|
if (!options?.skipInstall) {
|
2022-04-29 20:00:50 +08:00
|
|
|
await app.install();
|
2022-09-18 14:10:01 +08:00
|
|
|
} else {
|
|
|
|
await app.load();
|
2022-04-29 20:00:50 +08:00
|
|
|
}
|
|
|
|
await app.start();
|
|
|
|
}
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
async registerToMainApp(mainApp: Application, options: registerAppOptions) {
|
|
|
|
const appName = this.get('name') as string;
|
2022-04-24 20:22:50 +08:00
|
|
|
const appOptions = (this.get('options') as any) || {};
|
2022-03-28 22:01:10 +08:00
|
|
|
|
2022-04-30 23:41:01 +08:00
|
|
|
const AppModel = this.constructor as typeof ApplicationModel;
|
|
|
|
|
2022-09-18 14:10:01 +08:00
|
|
|
const createDatabase = async () => {
|
|
|
|
const database = appName;
|
|
|
|
const { host, port, username, password, dialect } = mainApp.db.options;
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
if (dialect === 'mysql') {
|
|
|
|
const mysql = require('mysql2/promise');
|
|
|
|
const connection = await mysql.createConnection({ host, port, user: username, password });
|
|
|
|
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
|
|
|
|
await connection.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dialect === 'postgres') {
|
|
|
|
const { Client } = require('pg');
|
|
|
|
|
|
|
|
const client = new Client({
|
|
|
|
host,
|
|
|
|
port,
|
2022-06-08 13:20:01 +08:00
|
|
|
user: username,
|
|
|
|
password,
|
2022-09-18 14:10:01 +08:00
|
|
|
database: 'postgres',
|
2022-03-28 22:01:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await client.connect();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await client.query(`CREATE DATABASE "${database}"`);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
await client.end();
|
|
|
|
}
|
2022-09-18 14:10:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!options?.skipInstall) {
|
|
|
|
await createDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
const app = mainApp.appManager.createApplication(appName, {
|
|
|
|
...AppModel.initOptions(appName, mainApp),
|
|
|
|
...appOptions,
|
2022-03-28 22:01:10 +08:00
|
|
|
});
|
|
|
|
|
2022-04-30 23:41:01 +08:00
|
|
|
await AppModel.handleAppStart(app, options);
|
|
|
|
|
|
|
|
await AppModel.update(
|
|
|
|
{
|
|
|
|
status: 'running',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
transaction: options.transaction,
|
|
|
|
where: {
|
|
|
|
[AppModel.primaryKeyAttribute]: this.get(AppModel.primaryKeyAttribute),
|
|
|
|
},
|
|
|
|
hooks: false,
|
|
|
|
},
|
|
|
|
);
|
2022-03-28 22:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static initOptions(appName: string, mainApp: Application) {
|
2022-04-30 23:41:01 +08:00
|
|
|
const rawDatabaseOptions = this.getDatabaseConfig(mainApp);
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
if (rawDatabaseOptions.dialect === 'sqlite') {
|
|
|
|
const mainAppStorage = rawDatabaseOptions.storage;
|
|
|
|
if (mainAppStorage !== ':memory:') {
|
|
|
|
const mainStorageDir = path.dirname(mainAppStorage);
|
|
|
|
rawDatabaseOptions.storage = path.join(mainStorageDir, `${appName}.sqlite`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rawDatabaseOptions.database = appName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-08-04 21:27:55 +08:00
|
|
|
database: {
|
|
|
|
...rawDatabaseOptions,
|
|
|
|
tablePrefix: '',
|
|
|
|
},
|
2022-03-28 22:01:10 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|