mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
refactor(database): add pool options from env
This commit is contained in:
parent
3c555e9fc0
commit
3be05a537b
@ -48,6 +48,14 @@ DB_PASSWORD=nocobase
|
||||
# DB_LOGGING=on
|
||||
# DB_UNDERSCORED=false
|
||||
|
||||
# @see https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-constructor-constructor
|
||||
# DB_POOL_MAX=5
|
||||
# DB_POOL_MIN=0
|
||||
# DB_POOL_IDLE=10000
|
||||
# DB_POOL_ACQUIRE=60000
|
||||
# DB_POOL_EVICT=1000
|
||||
# DB_POOL_MAX_USES=0
|
||||
|
||||
# sqlite only
|
||||
# DB_STORAGE=storage/db/nocobase.sqlite
|
||||
|
||||
|
47
packages/core/database/src/__tests__/helpers.test.ts
Normal file
47
packages/core/database/src/__tests__/helpers.test.ts
Normal file
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* This file is part of the NocoBase (R) project.
|
||||
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
||||
* Authors: NocoBase Team.
|
||||
*
|
||||
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { parseDatabaseOptionsFromEnv } from '@nocobase/database';
|
||||
|
||||
describe('database helpers', () => {
|
||||
describe('parseDatabaseOptionsFromEnv()', () => {
|
||||
it('undefined pool options', async () => {
|
||||
const options1 = await parseDatabaseOptionsFromEnv();
|
||||
expect(options1).toMatchObject({
|
||||
pool: {
|
||||
max: 5,
|
||||
min: 0,
|
||||
idle: 10000,
|
||||
acquire: 60000,
|
||||
evict: 1000,
|
||||
maxUses: Number.POSITIVE_INFINITY, // Default value
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('custom pool options', async () => {
|
||||
process.env.DB_POOL_MAX = '10';
|
||||
process.env.DB_POOL_MIN = '1';
|
||||
process.env.DB_POOL_IDLE = '5000';
|
||||
process.env.DB_POOL_ACQUIRE = '30000';
|
||||
process.env.DB_POOL_EVICT = '2000';
|
||||
process.env.DB_POOL_MAX_USES = '0'; // Set to 0 to test default behavior
|
||||
|
||||
const options2 = await parseDatabaseOptionsFromEnv();
|
||||
expect(options2.pool).toMatchObject({
|
||||
max: 10,
|
||||
min: 1,
|
||||
idle: 5000,
|
||||
acquire: 30000,
|
||||
evict: 2000,
|
||||
maxUses: Number.POSITIVE_INFINITY, // Default value
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -87,6 +87,16 @@ export async function parseDatabaseOptionsFromEnv(): Promise<IDatabaseOptions> {
|
||||
tablePrefix: process.env.DB_TABLE_PREFIX,
|
||||
schema: process.env.DB_SCHEMA,
|
||||
underscored: process.env.DB_UNDERSCORED === 'true',
|
||||
pool: {
|
||||
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
|
||||
? Number.parseInt(process.env.DB_POOL_MAX_USES, 10) || Number.POSITIVE_INFINITY
|
||||
: Number.POSITIVE_INFINITY,
|
||||
},
|
||||
};
|
||||
|
||||
const sslOptions = await extractSSLOptionsFromEnv();
|
||||
|
Loading…
x
Reference in New Issue
Block a user