-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbhbl-db.js
72 lines (72 loc) · 1.57 KB
/
bhbl-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
const fs = require("fs");
const err = 'Error: If you need help? https://bhlist.co.in/dc'
module.exports = class BHBL {
constructor(filePath){
this.json = filePath || "./bhbldb.json";
this.db = {};
if(!fs.existsSync(this.json)){
fs.writeFileSync(this.json, "{}", "utf-8");
} else {
this.file();
}
}
file(){
const savedData = JSON.parse(fs.readFileSync(this.json));
if(typeof savedData === "object"){
this.db = savedData;
}
}
save(){
return fs.writeFileSync(this.json, JSON.stringify(this.db, null, 2), "utf-8");
}
get(key){
if(!key) return console.error(err)
return this.db[key];
}
fetch(key){
if(!key) return console.error(err)
return this.db[key];
}
has(key){
if(!key) return console.error(err)
return Boolean(this.db[key]);
}
set(key, value){
if(!key) return console.error(err)
if(!value) return console.error(err)
this.db[key] = value;
return this.save();
}
delete(key){
if(!key) return console.error(err)
delete this.db[key];
return this.save();
}
add(key, count){
if(!key) return console.error(err)
if(!count) return console.error(err)
if(!this.db[key]) this.db[key] = 0;
this.db[key] += count;
return this.save();
}
sub(key, count){
if(!key) return console.error(err)
if(!count) return console.error(err)
if(!this.db[key]) this.db[key] = 0;
this.db[key] -= count;
return this.save();
}
push(key, element){
if(!key) return console.error(err)
if(!element) return console.error(err)
if (!this.db[key]) this.db[key] = [];
this.db[key].push(element);
return this.save();
}
clear(){
this.db = {};
this.save();
}
all() {
return this.db
}};