mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
fix(database): only inject env configured
This commit is contained in:
parent
463c8bc8bc
commit
8218e453c1
@ -14,14 +14,7 @@ describe('database helpers', () => {
|
|||||||
it('undefined pool options', async () => {
|
it('undefined pool options', async () => {
|
||||||
const options1 = await parseDatabaseOptionsFromEnv();
|
const options1 = await parseDatabaseOptionsFromEnv();
|
||||||
expect(options1).toMatchObject({
|
expect(options1).toMatchObject({
|
||||||
pool: {
|
pool: {},
|
||||||
max: 5,
|
|
||||||
min: 0,
|
|
||||||
idle: 10000,
|
|
||||||
acquire: 60000,
|
|
||||||
evict: 1000,
|
|
||||||
maxUses: Number.POSITIVE_INFINITY, // Default value
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ import { MysqlDialect } from './dialects/mysql-dialect';
|
|||||||
import { SqliteDialect } from './dialects/sqlite-dialect';
|
import { SqliteDialect } from './dialects/sqlite-dialect';
|
||||||
import { MariadbDialect } from './dialects/mariadb-dialect';
|
import { MariadbDialect } from './dialects/mariadb-dialect';
|
||||||
import { PostgresDialect } from './dialects/postgres-dialect';
|
import { PostgresDialect } from './dialects/postgres-dialect';
|
||||||
|
import { PoolOptions } from 'sequelize';
|
||||||
|
|
||||||
function getEnvValue(key, defaultValue?) {
|
function getEnvValue(key, defaultValue?) {
|
||||||
return process.env[key] || defaultValue;
|
return process.env[key] || defaultValue;
|
||||||
@ -73,6 +74,29 @@ function extractSSLOptionsFromEnv() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPoolOptions(): PoolOptions {
|
||||||
|
const options: PoolOptions = {};
|
||||||
|
if (process.env.DB_POOL_MAX) {
|
||||||
|
options.max = Number.parseInt(process.env.DB_POOL_MAX, 10);
|
||||||
|
}
|
||||||
|
if (process.env.DB_POOL_MIN) {
|
||||||
|
options.min = Number.parseInt(process.env.DB_POOL_MIN, 10);
|
||||||
|
}
|
||||||
|
if (process.env.DB_POOL_IDLE) {
|
||||||
|
options.idle = Number.parseInt(process.env.DB_POOL_IDLE, 10);
|
||||||
|
}
|
||||||
|
if (process.env.DB_POOL_ACQUIRE) {
|
||||||
|
options.acquire = Number.parseInt(process.env.DB_POOL_ACQUIRE, 10);
|
||||||
|
}
|
||||||
|
if (process.env.DB_POOL_EVICT) {
|
||||||
|
options.evict = Number.parseInt(process.env.DB_POOL_EVICT, 10);
|
||||||
|
}
|
||||||
|
if (process.env.DB_POOL_MAX_USES) {
|
||||||
|
options.maxUses = Number.parseInt(process.env.DB_POOL_MAX_USES, 10) || Number.POSITIVE_INFINITY;
|
||||||
|
}
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
export async function parseDatabaseOptionsFromEnv(): Promise<IDatabaseOptions> {
|
export async function parseDatabaseOptionsFromEnv(): Promise<IDatabaseOptions> {
|
||||||
const databaseOptions: IDatabaseOptions = {
|
const databaseOptions: IDatabaseOptions = {
|
||||||
logging: process.env.DB_LOGGING == 'on' ? customLogger : false,
|
logging: process.env.DB_LOGGING == 'on' ? customLogger : false,
|
||||||
@ -87,17 +111,7 @@ export async function parseDatabaseOptionsFromEnv(): Promise<IDatabaseOptions> {
|
|||||||
tablePrefix: process.env.DB_TABLE_PREFIX,
|
tablePrefix: process.env.DB_TABLE_PREFIX,
|
||||||
schema: process.env.DB_SCHEMA,
|
schema: process.env.DB_SCHEMA,
|
||||||
underscored: process.env.DB_UNDERSCORED === 'true',
|
underscored: process.env.DB_UNDERSCORED === 'true',
|
||||||
pool: {
|
pool: getPoolOptions(),
|
||||||
max: process.env.DB_POOL_MAX ? Number.parseInt(process.env.DB_POOL_MAX, 10) : 5,
|
|
||||||
min: process.env.DB_POOL_MIN ? Number.parseInt(process.env.DB_POOL_MIN, 10) : 0,
|
|
||||||
idle: process.env.DB_POOL_IDLE ? Number.parseInt(process.env.DB_POOL_IDLE, 10) : 10_000,
|
|
||||||
acquire: process.env.DB_POOL_ACQUIRE ? Number.parseInt(process.env.DB_POOL_ACQUIRE, 10) : 60_000,
|
|
||||||
evict: process.env.DB_POOL_EVICT ? Number.parseInt(process.env.DB_POOL_EVICT, 10) : 1000,
|
|
||||||
maxUses: process.env.DB_POOL_MAX_USES
|
|
||||||
// If DB_POOL_MAX_USES is '0', it is treated as falsy and defaults to Number.POSITIVE_INFINITY.
|
|
||||||
? Number.parseInt(process.env.DB_POOL_MAX_USES, 10) || Number.POSITIVE_INFINITY
|
|
||||||
: Number.POSITIVE_INFINITY,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const sslOptions = await extractSSLOptionsFromEnv();
|
const sslOptions = await extractSSLOptionsFromEnv();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user