34 lines
820 B
TypeScript
34 lines
820 B
TypeScript
import mysql from 'mysql2/promise';
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
const pool = mysql.createPool({
|
|
host: process.env.MYSQL_HOST,
|
|
user: process.env.MYSQL_USER,
|
|
password: process.env.MYSQL_PASSWOWD,
|
|
database: process.env.MYSQL_DB,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0
|
|
});
|
|
|
|
export async function query(sql: string, params: any[] = []) {
|
|
const [rows] = await pool.execute(sql, params);
|
|
return rows;
|
|
}
|
|
|
|
export async function testConnection() {
|
|
try {
|
|
const connection = await pool.getConnection();
|
|
console.log('Successfully connected to the database.');
|
|
connection.release();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Failed to connect to the database:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export default pool;
|