feat: support for the USE_DB_SCHEMA_IN_SUBAPP environment variable (#5481)

This commit is contained in:
chenos 2024-10-22 14:24:34 +08:00 committed by GitHub
parent b08f364555
commit e39d014b67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -61,7 +61,7 @@ const defaultSubAppUpgradeHandle: SubAppUpgradeHandler = async (mainApp: Applica
const defaultDbCreator = async (app: Application) => {
const databaseOptions = app.options.database as any;
const { host, port, username, password, dialect, database } = databaseOptions;
const { host, port, username, password, dialect, database, schema } = databaseOptions;
if (dialect === 'mysql') {
const mysql = require('mysql2/promise');
@ -91,7 +91,11 @@ const defaultDbCreator = async (app: Application) => {
await client.connect();
try {
if (process.env.USE_DB_SCHEMA_IN_SUBAPP === 'true') {
await client.query(`CREATE SCHEMA IF NOT EXISTS ${schema}`);
} else {
await client.query(`CREATE DATABASE "${database}"`);
}
} catch (e) {
console.log(e);
}
@ -109,6 +113,8 @@ const defaultAppOptionsFactory = (appName: string, mainApp: Application) => {
const mainStorageDir = path.dirname(mainAppStorage);
rawDatabaseOptions.storage = path.join(mainStorageDir, `${appName}.sqlite`);
}
} else if (process.env.USE_DB_SCHEMA_IN_SUBAPP === 'true' && rawDatabaseOptions.dialect === 'postgres') {
rawDatabaseOptions.schema = appName;
} else {
rawDatabaseOptions.database = appName;
}