mirror of
https://github.com/nocobase/nocobase.git
synced 2026-09-01 14:57:36 +08:00
refactor(database): add pool options from env (#7133)
* refactor(database): add pool options from env * Update packages/core/database/src/helpers.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(database): only inject env configured * fix(server): simplify database options * revert(server): revert create database api --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 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: {},
|
||||
});
|
||||
});
|
||||
|
||||
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
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -15,6 +15,7 @@ import { MysqlDialect } from './dialects/mysql-dialect';
|
||||
import { SqliteDialect } from './dialects/sqlite-dialect';
|
||||
import { MariadbDialect } from './dialects/mariadb-dialect';
|
||||
import { PostgresDialect } from './dialects/postgres-dialect';
|
||||
import { PoolOptions } from 'sequelize';
|
||||
|
||||
function getEnvValue(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> {
|
||||
const databaseOptions: IDatabaseOptions = {
|
||||
logging: process.env.DB_LOGGING == 'on' ? customLogger : false,
|
||||
@@ -87,6 +111,7 @@ export async function parseDatabaseOptionsFromEnv(): Promise<IDatabaseOptions> {
|
||||
tablePrefix: process.env.DB_TABLE_PREFIX,
|
||||
schema: process.env.DB_SCHEMA,
|
||||
underscored: process.env.DB_UNDERSCORED === 'true',
|
||||
pool: getPoolOptions(),
|
||||
};
|
||||
|
||||
const sslOptions = await extractSSLOptionsFromEnv();
|
||||
|
||||
Reference in New Issue
Block a user