-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
50 lines (41 loc) · 1.31 KB
/
db.js
File metadata and controls
50 lines (41 loc) · 1.31 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
const mysql = require("mysql");
const config = require("./config");
function pack(value) {
if(typeof(value) == 'string')
return `'${value}'`
else
return value
}
class DB {
constructor(table_name) {
this.connection = mysql.createConnection(config.mysql)
this.table_name = table_name
}
query(sql) {
return new Promise((resolve,reject) => {
this.connection.query(sql, (err,res,fields) => {
if(err)
reject(err)
resolve(res)
})
});
}
set(key,val,id) {
return this.query(`UPDATE ${this.table_name} SET ${key}=${pack(val)} WHERE id='${id}';`)
}
get(key,id) {
return this.query(`SELECT ${key} FROM ${this.table_name} WHERE id='${id}';`)
.then((ret) => {return ret[0][key]})
}
insert(id) {
return this.query(`INSERT INTO ${this.table_name} VALUES (0,'${id}','done',DEFAULT,DEFAULT,DEFAULT,DEFAULT, "error");`)
}
getStatus() {
return this.query(`SELECT id,state,lastSyncTime,nextSyncTime,diskUsage FROM ${this.table_name};`)
}
exists(id) {
return this.query(`SELECT 1 FROM ${this.table_name} WHERE id='${id}';`)
.then((res) => {return res.length});
}
}
module.exports = new DB('status')