forked from PhlexPlexico/G5API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
86 lines (79 loc) · 2.69 KB
/
db.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
/*Database driver. This should probably be converted to pools.*/
const mysql = require('mysql2/promise');
const config = require('config');
const dbCfg = {
host: config.get(process.env.NODE_ENV+".host"),
port: config.get(process.env.NODE_ENV+".port"),
user: config.get(process.env.NODE_ENV+".user"),
password: config.get(process.env.NODE_ENV+".password"),
database: config.get(process.env.NODE_ENV+".database"),
connectionLimit: config.get(process.env.NODE_ENV+".connectionLimit")
}
const connPool = mysql.createPool(dbCfg);
class Database {
constructor() {
this.setupAdmins();
}
async query(sql, args) {
try {
const connection = await this.getConnection();
let result;
await this.withNewTransaction(connection, async () => {
result = await connection.query(sql, args);
});
return result[0];
} catch (error) {
console.log("SQL ERROR SQL ERROR SQL ERROR SQL ERROR SQL ERROR\n" + error);
throw error;
}
}
async buildUpdateStatement(objValues){
for (let key in objValues) {
if (objValues[key] == null) delete objValues[key];
}
return objValues;
}
async getConnection () {
return await connPool.getConnection();
}
/** Inner function - boilerplate transaction call.
* @name withNewTransaction
* @function
* @inner
* @memberof module:routes/vetoes
* @param {*} singleCall - The connection from the database pool.
* @param {*} callback - The callback function that is operated on, usually a db.query()
*/
async withNewTransaction(singleCall, callback) {
await singleCall.beginTransaction();
let isDestroyed = false;
try {
await callback();
await singleCall.commit();
} catch (err) {
await singleCall.rollback();
isDestroyed = true;
throw err;
} finally {
if (isDestroyed) await singleCall.destroy();
else await singleCall.release();
}
}
async setupAdmins() {
try {
let singleConn = await this.getConnection();
await this.withNewTransaction(singleConn, async () => {
let listOfAdmins = config.get("admins.steam_ids").split(',');
let listofSuperAdmins = config.get("super_admins.steam_ids").split(',');
// Get list of admins from database and compare list and add new admins.
let updateAdmins = "UPDATE user SET admin = 1 WHERE steam_id IN (?)";
let updateSuperAdmins = "UPDATE user SET super_admin = 1 WHERE steam_id in(?)";
await singleConn.query(updateAdmins, [listOfAdmins]);
await singleConn.query(updateSuperAdmins, [listofSuperAdmins]);
});
} catch (err) {
console.log("Failed to import users. Error: " + err);
}
}
}
module.exports = new Database();