feat(M01-B): 建立核心迁移与MySQL连接池
This commit is contained in:
@@ -11,6 +11,7 @@ const configSchema = z.object({
|
||||
QIPAI_MYSQL_DATABASE: z.string().min(1).default('qipai'),
|
||||
QIPAI_MYSQL_USER: z.string().min(1).default('qipai_app'),
|
||||
QIPAI_MYSQL_PASSWORD: z.string().default(''),
|
||||
QIPAI_MYSQL_CONNECTION_LIMIT: z.coerce.number().int().min(1).max(50).default(10),
|
||||
QIPAI_MQTT_URL: z.string().url().default('mqtt://101.42.38.246:1883'),
|
||||
QIPAI_MQTT_USERNAME: z.string().default(''),
|
||||
QIPAI_MQTT_PASSWORD: z.string().default('')
|
||||
@@ -32,7 +33,9 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env) {
|
||||
port: parsed.QIPAI_MYSQL_PORT,
|
||||
database: parsed.QIPAI_MYSQL_DATABASE,
|
||||
user: parsed.QIPAI_MYSQL_USER,
|
||||
passwordConfigured: parsed.QIPAI_MYSQL_PASSWORD.length > 0
|
||||
password: parsed.QIPAI_MYSQL_PASSWORD,
|
||||
passwordConfigured: parsed.QIPAI_MYSQL_PASSWORD.length > 0,
|
||||
connectionLimit: parsed.QIPAI_MYSQL_CONNECTION_LIMIT
|
||||
},
|
||||
mqtt: {
|
||||
url: parsed.QIPAI_MQTT_URL,
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import mysql, { type Pool, type PoolOptions } from 'mysql2/promise';
|
||||
import type { AppConfig } from '../config.js';
|
||||
|
||||
export type MySqlPool = Pool;
|
||||
|
||||
export function createMySqlPool(config: AppConfig): MySqlPool {
|
||||
return mysql.createPool(toPoolOptions(config));
|
||||
}
|
||||
|
||||
export function toPoolOptions(config: AppConfig): PoolOptions {
|
||||
return {
|
||||
host: config.mysql.host,
|
||||
port: config.mysql.port,
|
||||
database: config.mysql.database,
|
||||
user: config.mysql.user,
|
||||
password: config.mysql.password,
|
||||
waitForConnections: true,
|
||||
connectionLimit: config.mysql.connectionLimit,
|
||||
namedPlaceholders: true,
|
||||
timezone: 'Z',
|
||||
dateStrings: false
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user