This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
98 lines (85 loc) · 2.25 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
87
88
89
90
91
92
93
94
95
96
97
98
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
// TODO switch to JSONFile which won't block requests
const adapter = new FileSync(process.env.CONFIG_PATH);
const db = low(adapter);
const initServer = async function (guildID) {
// const guildID = `server_${serverID}`;
console.log(db.get(guildID).value());
if(db.get(guildID).keys().value().length > 0){
console.log(`DB for server ${guildID} is already initialized`);
console.log(db.get(guildID).get('hotspots').value());
return null;
}
else {
console.log(`Initializing hash for ${guildID}`);
const defaults = {}
defaults[guildID] = { owners: [], hotspots: [], validators: [] };
db.defaults(defaults).write();
const values = db.get(guildID).value();
console.log("values =>", values);
return values;
}
}
const addHotspotAddress = async function (guildID, address, name) {
db.get(guildID)
.get('hotspots')
.push({
address: address,
name: name
})
.write();
};
const removeHotspotAddress = function (guildID, address) {
db.get(guildID).get('hotspots')
.remove({ address: address })
.write();
};
const addOwnerAddress = function (guildID, address, name) {
db.get(guildID).get('owners')
.push({
address: address,
name: name
})
.write();
};
const removeOwnerAddress = function (guildID, address) {
db.get(guildID).get('owners')
.remove({ address: address })
.write();
};
const addValidator = function (guildID, address, name) {
db.get(guildID).get('validators')
.push({
address: address,
name: name
})
.write();
};
const removeValidator = function (guildID, address) {
db.get(guildID).get('validators')
.remove({ address: address })
.write();
};
const getValidators = function (guildID) {
return db.get(guildID).get('validators').value() || [];
}
const getOwners = function (guildID) {
return db.get(guildID).get('owners').value() || [];
}
const getHotspots = function (guildID) {
return db.get(guildID).get('hotspots').value() || [];
}
module.exports = {
db,
initServer,
addHotspotAddress,
removeHotspotAddress,
addOwnerAddress,
removeOwnerAddress,
getOwners,
getHotspots,
getValidators,
addValidator,
removeValidator
};