forked from PhlexPlexico/G5API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
53 lines (47 loc) · 1.6 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
/*Database driver.*/
import { createPool } from 'mysql2/promise';
import config from '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 = createPool(dbCfg);
class Database {
constructor() {
this.setupAdmins();
}
async query(sql, args) {
try {
let result;
result = await connPool.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 setupAdmins() {
try {
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 connPool.query(updateAdmins, [listOfAdmins]);
await connPool.query(updateSuperAdmins, [listofSuperAdmins]);
} catch (err) {
console.log("Failed to import users. " + err);
}
}
}
export default new Database();