38 lines
888 B
JavaScript
38 lines
888 B
JavaScript
const mysql = require('mysql2/promise');
|
|
|
|
let pool;
|
|
|
|
function initPool() {
|
|
if (pool) {
|
|
pool.end().catch(e => console.error('Error closing pool:', e));
|
|
}
|
|
pool = mysql.createPool({
|
|
host: process.env.MYSQL_HOST || 'localhost',
|
|
port: parseInt(process.env.MYSQL_PORT) || 3306,
|
|
user: process.env.MYSQL_USER || 'root',
|
|
password: process.env.MYSQL_PASSWORD || '',
|
|
database: process.env.MYSQL_DATABASE || 'display_wall',
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0
|
|
});
|
|
}
|
|
|
|
async function checkHealth() {
|
|
try {
|
|
if (!pool) return { status: 'down', error: 'Database pool not initialized' };
|
|
await pool.query('SELECT 1');
|
|
return { status: 'up' };
|
|
} catch (err) {
|
|
return { status: 'down', error: err.message };
|
|
}
|
|
}
|
|
|
|
initPool();
|
|
|
|
module.exports = {
|
|
query: (...args) => pool.query(...args),
|
|
initPool,
|
|
checkHealth
|
|
};
|