-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·141 lines (121 loc) · 3.12 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* global R5 */
module.exports = Database;
if (!global.R5) {
global.R5 = {
out: console
};
}
const mysql = require('mysql2/promise');
// Constructor
function Database (read_config, write_config = false) {
this.IN = new Host(read_config);
if (write_config && read_config['host'] !== write_config['host']) {
this.OUT = new Host(write_config);
}
else {
this.OUT = this.IN;
}
}
function Host (config) {
this.connect_retries = 0;
this.query_retries = 0;
this.error_timeout = 10000;
this.config = config;
}
// Public Methods
Database.prototype = {
connect: async function () {
await this.IN.connect();
if (this.OUT !== this.IN) {
await this.OUT.connect();
}
},
disconnect: function () {
this.IN.destroy();
if (this.OUT !== this.IN) {
this.OUT.destroy();
}
},
query: async function (query) {
let host = this.IN;
if (
(typeof query === 'object' && is_update(query.sql)) ||
(typeof query === 'string' && is_update(query))
) {
host = this.OUT;
}
return host.query(query);
}
};
Host.prototype = {
connect: async function () {
const host = this;
try {
this.connection = await mysql.createConnection(host.config);
}
catch (err) {
if (host.connect_retries++ < 10) {
R5.out.error(`MySQL connecting (retrying [${host.connect_retries}]): ${err.code}`);
return host.retry();
}
R5.out.error(`MySQL connecting: ${err.stack}`);
throw err;
}
R5.out.log(`MySQL connected (conn: ${host.connection.connection.threadId })`);
this.connect_retries = 0;
host.connection.on('error', async function (err) {
R5.out.error((err.fatal === true ? '(FATAL) ' : '') + err);
if (err.code === 'PROTOCOL_CONNECTION_LOST' || err.fatal === true) {
host.destroy();
return host.connect();
}
else {
throw err;
}
});
},
destroy: function () {
const host = this;
if ((host.connection || {}).destroy) {
host.connection.destroy();
}
},
query: async function (query) {
const host = this;
try {
let result, fields;
if (typeof query === 'object' && query.values && query.values.length > 0) {
query.values = query.values.map(item => item === undefined ? null : item);
[result, fields] = await host.connection.execute(query);
}
else {
[result, fields] = await host.connection.query(query);
}
this.query_retries = 0;
return result;
}
catch (err) {
if (err.fatal && host.query_retries++ < 10) {
return host.retry(query);
}
R5.out.error(`MySQL query error: ${err}\n${query}\n`);
throw err;
}
},
retry: async function (query) {
const host = this;
host.destroy();
await delay(host.error_timeout * (host.connect_retries + host.query_retries + 1));
await host.connect();
if (query) {
return host.query(query);
}
},
};
// Private Methods
function is_update (str) {
return str.substring(0, 6).toUpperCase() !== 'SELECT';
}
function delay (ms) {
return new Promise((res) => setTimeout(res, ms));
}