37 lines
1019 B
JavaScript
Executable File
37 lines
1019 B
JavaScript
Executable File
// Database Connection Module
|
|
// Handles MySQL connection with connection pooling and auto-reconnect
|
|
|
|
import mysql from 'mysql2/promise';
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST,
|
|
port: parseInt(process.env.DB_PORT || '3306'),
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASS,
|
|
database: process.env.DB_NAME,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0,
|
|
connectTimeout: 60000,
|
|
enableKeepAlive: true,
|
|
keepAliveInitialDelay: 10000
|
|
});
|
|
|
|
// Test connection on startup
|
|
export const testConnection = async () => {
|
|
try {
|
|
const connection = await pool.getConnection();
|
|
console.log('✅ MySQL Connected Successfully to:', process.env.DB_HOST);
|
|
connection.release();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ MySQL Connection Failed:', error.message);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export default pool;
|