2024-06-03 14:13:02 +00:00
|
|
|
import mysql from 'mysql2/promise';
|
|
|
|
import logger from '../utils/logger.js';
|
2024-05-25 05:38:51 +00:00
|
|
|
|
|
|
|
class Conexion {
|
|
|
|
constructor() {
|
|
|
|
this.conexion = null;
|
|
|
|
this.configuracion = {
|
|
|
|
host: 'localhost',
|
|
|
|
user: 'UserRemoto',
|
|
|
|
password: 'password123',
|
|
|
|
database: 'universidad'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async conectar() {
|
|
|
|
try {
|
|
|
|
this.conexion = await mysql.createConnection(this.configuracion);
|
2024-06-03 04:59:59 +00:00
|
|
|
logger.info('Conexión exitosa a MySQL');
|
2024-05-25 05:38:51 +00:00
|
|
|
return this.conexion;
|
|
|
|
} catch (error) {
|
2024-06-03 04:59:59 +00:00
|
|
|
logger.error(`Error al conectar con la base de datos: ${error.message}`);
|
2024-05-25 05:38:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cerrarConexion() {
|
|
|
|
if (this.conexion) {
|
|
|
|
this.conexion.end((err) => {
|
|
|
|
if (err) {
|
2024-06-03 04:59:59 +00:00
|
|
|
logger.error(`Error al cerrar la conexión con la base de datos: ${err.message}`);
|
2024-05-25 05:38:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-06-03 04:59:59 +00:00
|
|
|
logger.info('Se desconectó de la base de datos');
|
2024-05-25 05:38:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 04:59:59 +00:00
|
|
|
export default Conexion;
|