35 lines
898 B
JavaScript
35 lines
898 B
JavaScript
import mysql from 'mysql2/promise'
|
|
import { APP_TIMEZONE } from './config/db.js'
|
|
import dotenv from 'dotenv'
|
|
import path from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
|
|
dotenv.config({ path: path.join(path.dirname(fileURLToPath(import.meta.url)), '.env') });
|
|
|
|
const db = mysql.createPool({
|
|
host: process.env.DB_HOST,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME,
|
|
port: process.env.DB_PORT,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0,
|
|
// timezone: '+08:00',
|
|
dateStrings: true,
|
|
});
|
|
|
|
|
|
const originalGetConnection = db.getConnection.bind(db);
|
|
|
|
db.getConnection = async () => {
|
|
const connection = await originalGetConnection();
|
|
// 设置时区
|
|
await connection.execute(`SET time_zone = '${APP_TIMEZONE}'`);
|
|
return connection;
|
|
};
|
|
|
|
export const getConnection = async () => {
|
|
return await db.getConnection()
|
|
}
|