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