39 lines
1.1 KiB
JavaScript
Executable File
39 lines
1.1 KiB
JavaScript
Executable File
// Database Configuration
|
|
import mysql from 'mysql2/promise';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
dotenv.config({ path: path.join(__dirname, '../../.env') });
|
|
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST || 'localhost',
|
|
user: process.env.DB_USER || 'smanabid_jurnal',
|
|
password: process.env.DB_PASSWORD?.replace(/^['"]|['"]$/g, '') || '',
|
|
database: process.env.DB_NAME || 'smanabid_db_jurnal',
|
|
port: process.env.DB_PORT || 3306,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0,
|
|
enableKeepAlive: true,
|
|
keepAliveInitialDelay: 0
|
|
});
|
|
|
|
// Test connection
|
|
const testConnection = async () => {
|
|
try {
|
|
const connection = await pool.getConnection();
|
|
console.log('✅ Database connected successfully!');
|
|
connection.release();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ Database connection failed:', error.message);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export { pool, testConnection };
|