-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
57 lines (52 loc) · 2.53 KB
/
database.js
File metadata and controls
57 lines (52 loc) · 2.53 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const mysql = require('mysql');
const connection = mysql.createConnection({
host: "sg1.inferno.land",
port: "3306",
user: "u54_fm3TFaPONa",
password: "R3l0kQBt=+HSPk!64Q^D9qDk",
database: "s54_dtech_sql"
});
function handleDisconnect() {
// Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
}
console.log('MySQL Database is connected Successfully');
connection.query('create table if not exists user (email varchar(256) primary key ,pass varchar(256),name varchar(256), admin bool,blacklist bool)',(err, result) => {
if (err) throw err;
console.log("User Table created");
})
connection.query('create table if not exists product (name varchar(256),id varchar(256) primary key ,`desc` varchar(256),price int,owner varchar(256),foreign key (owner) references user(email))',(err, result) => {
if (err) throw err;
console.log("Product Table created");
})
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
// connection.connect(function(error){
// if(error)
// {
// throw error;
// }
// else
// {
// console.log('MySQL Database is connected Successfully');
// connection.query('create table if not exists user (email varchar(256) primary key ,pass varchar(256),name varchar(256), admin bool,blacklist bool)',(err, result) => {
// if (err) throw err;
// console.log("Table created");
// })
// }
// });
module.exports = connection;