-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdb.js
More file actions
60 lines (51 loc) · 1.33 KB
/
Copy pathdb.js
File metadata and controls
60 lines (51 loc) · 1.33 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
51
52
53
54
55
56
57
58
59
60
const fetch = (...args) => import("node-fetch").then(({default: fetch}) => fetch(...args));
class db {
constructor(){
this.db_url = process.env["db_url"];
}
async get(key){
return await fetch(`${this.db_url}/${key}`)
.then(e => e.text())
.then(str => {
let value = str;
try {
value = JSON.parse(str);
} catch(err){
console.log(err)
}
if(!value) return null;
return value;
});
}
async set(key, value){
const strValue = JSON.stringify(value);
await fetch(`${this.db_url}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ key: key, value: strValue })
});
return this;
}
async delete(key){
await fetch(`${this.db_url}/${key}`, { method: "DELETE" });
return this;
}
async player(name, data){
data = JSON.stringify(data);
await fetch(`${this.db_url}/player`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, data })
});
return this;
}
async score(name){
await fetch(`${this.db_url}/player/score`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ "name": name })
});
return this;
}
}
module.exports = db;