-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.js
104 lines (94 loc) · 2.92 KB
/
nodes.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const mysql = require('mysql2/promise');
const dotenv = require('dotenv');
dotenv.config();
var path = require('path');
var fs = require('fs'); // for reading of certificate for connection
const serverCa = [fs.readFileSync(path.resolve("models/DigiCertGlobalRootCA.crt.pem"))]; // certificate provided by AZURE Database
const node1 = mysql.createPool({
host: process.env.HOSTNAME1,
port: process.env.PORT1,
user: process.env.USERNAME1,
password: process.env.PASSWORD1,
database: process.env.NAME1,
connectTimeout: 5000,
waitForConnections: true,
connectionLimit: 101,
queueLimit: 0,
ssl: {
rejectUnauthorized: true,
ca: serverCa
}
});
const node2 = mysql.createPool({
host: process.env.HOSTNAME2,
port: process.env.PORT2,
user: process.env.USERNAME2,
password: process.env.PASSWORD2,
database: process.env.NAME2,
connectTimeout: 5000,
waitForConnections: true,
connectionLimit: 101,
queueLimit: 0,
ssl: {
rejectUnauthorized: true,
ca: serverCa
}
});
const node3 = mysql.createPool({
host: process.env.HOSTNAME3,
port: process.env.PORT3,
user: process.env.USERNAME3,
password: process.env.PASSWORD3,
database: process.env.NAME3,
connectTimeout: 5000,
waitForConnections: true,
connectionLimit: 101,
queueLimit: 0,
ssl: {
rejectUnauthorized: true,
ca: serverCa
}
});
const nodes_funcs = {
// starting a connection with the nodes
connect_node: async function (node) {
switch (node) {
case 1: return await node1.getConnection();
case 2: return await node2.getConnection();
case 3: return await node3.getConnection();
}
},
ping_node: async function (node) {
switch (node) {
case 1:
try {
let val = await node1.query('SELECT 1 + 1 AS solution');
return val[0][0].solution;
}
catch (error) { console.log('Failed to connect to node 1!'); }
break;
case 2:
try {
let val = await node2.query('SELECT 1 + 1 AS solution');
return val[0][0].solution;
}
catch (error) { console.log('Failed to connect to node 2!'); }
break;
case 3:
try {
let val = await node3.query('SELECT 1 + 1 AS solution');
return val[0][0].solution;
}
catch (error) { console.log('Failed to connect to node 3!'); }
break;
}
},
query_node: async function (node, query) {
switch (node) {
case 1: return await node1.query(query);
case 2: return await node2.query(query);
case 3: return await node3.query(query);
}
}
};
module.exports = nodes_funcs;