43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
// Database Connection Module - MySQL
|
|
import mysql from 'mysql2/promise';
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
// Create connection pool
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST || 'localhost',
|
|
user: process.env.DB_USER || 'root',
|
|
password: process.env.DB_PASSWORD || '',
|
|
database: process.env.DB_NAME || 'sarpras',
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0,
|
|
enableKeepAlive: true,
|
|
keepAliveInitialDelay: 0
|
|
});
|
|
|
|
// Test connection
|
|
export 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;
|
|
}
|
|
};
|
|
|
|
// Helper: Generate UUID
|
|
export const generateId = () => {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
const r = Math.random() * 16 | 0;
|
|
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
return v.toString(16);
|
|
});
|
|
};
|
|
|
|
export default pool;
|