56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
|
|
class Database {
|
|
constructor() {
|
|
this.pool = null;
|
|
}
|
|
|
|
async connect() {
|
|
try {
|
|
this.pool = mysql.createPool({
|
|
host: 'localhost',
|
|
port: 3306,
|
|
user: 'root',
|
|
password: 'Admin@123',
|
|
database: 'dubai_dld',
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0
|
|
});
|
|
|
|
// Test connection
|
|
const connection = await this.pool.getConnection();
|
|
console.log('✅ Database connected successfully');
|
|
connection.release();
|
|
|
|
return this.pool;
|
|
} catch (error) {
|
|
console.error('❌ Database connection failed:', error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async query(sql, params = []) {
|
|
try {
|
|
if (!this.pool) {
|
|
await this.connect();
|
|
}
|
|
|
|
const [rows] = await this.pool.execute(sql, params);
|
|
return rows;
|
|
} catch (error) {
|
|
console.error('Database query error:', error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async close() {
|
|
if (this.pool) {
|
|
await this.pool.end();
|
|
console.log('Database connection closed');
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new Database();
|