config.ts•949 B
import dotenv from 'dotenv';
import { MySQLConfig } from './types.js';
// 加载环境变量
dotenv.config();
export function getConfig(): MySQLConfig {
return {
host: process.env.MYSQL_HOST || 'localhost',
port: parseInt(process.env.MYSQL_PORT || '3306'),
user: process.env.MYSQL_USER || 'root',
password: process.env.MYSQL_PASSWORD || '',
database: process.env.MYSQL_DATABASE,
connectionLimit: parseInt(process.env.MYSQL_CONNECTION_LIMIT || '10'),
timeout: parseInt(process.env.MYSQL_TIMEOUT || '60000')
};
}
export function validateConfig(config: MySQLConfig): void {
if (!config.host) {
throw new Error('MySQL host is required');
}
if (!config.user) {
throw new Error('MySQL user is required');
}
if (!config.password) {
throw new Error('MySQL password is required');
}
if (config.port < 1 || config.port > 65535) {
throw new Error('MySQL port must be between 1 and 65535');
}
}