-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.js
39 lines (31 loc) · 920 Bytes
/
connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const mysql = require('mysql');
const configDB = {
host: 'tcs-database.catwxo5necez.us-east-1.rds.amazonaws.com',
user: 'admin',
password: 'admin123',
port: '3306',
database: 'empleados',
debug: true
};
function initializeConnection(config) {
function addDisconnectHandler(connection) {
connection.on("error", function (error) {
if (error instanceof Error) {
if (error.code === "PROTOCOL_CONNECTION_LOST") {
console.error(error.stack);
console.log("Lost connection. Reconnecting...");
initializeConnection(connection.config);
} else if (error.fatal) {
throw error;
}
}
});
}
const connection = mysql.createConnection(config);
// Add handlers.
addDisconnectHandler(connection);
connection.connect();
return connection;
}
const connection = initializeConnection(configDB);
module.exports = connection;