-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (114 loc) · 2.83 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const fs = require('fs').promises;
const path = require('path');
class AikoDB {
constructor(type = 'json', dbPath = 'aikodb.json') {
this.type = type;
this.dbPath = dbPath;
this.data = {};
this._load();
}
async init() {
await this._load();
}
async _load() {
try {
if (this.type === 'json') {
if (await this._fileExists(this.dbPath)) {
const data = await fs.readFile(this.dbPath);
this.data = JSON.parse(data);
} else {
await fs.writeFile(this.dbPath, JSON.stringify(this.data, null, 2));
}
}
} catch (error) {
console.error('Error loading database:', error);
console.error(`If you couldn't solve the error, come here https://discord.gg/AYRDhFpRXE`)
}
}
async _fileExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch (error) {
return false;
}
}
async add(key, value) {
this.data[key] = value;
await this._save();
}
get(key) {
return this.data[key];
}
fetch(key) {
return this.data[key]
}
async set(key, value) {
this.data[key] = value;
await this._save();
}
async delete(key) {
delete this.data[key];
await this._save();
}
all() {
return this.data;
}
async deleteAll() {
this.data = {};
await this._save();
}
async push(key, value) {
if (!this.data[key]) {
this.data[key] = [];
}
if (!Array.isArray(this.data[key])) {
throw new TypeError(`Value at key '${key}' is not an array and cannot be pushed to.`);
}
this.data[key].push(value);
await this._save();
}
filter(callback) {
const result = {};
for (const key in this.data) {
if (callback(this.data[key], key)) {
result[key] = this.data[key];
}
}
return result;
}
search(key, value) {
const result = {};
for (const k in this.data) {
if (this.data[k][key] === value) {
result[k] = this.data[k];
}
}
return result;
}
sort(key, order = 'asc') {
const sorted = Object.entries(this.data).sort((a, b) => {
if (order === 'asc') {
return a[1][key] > b[1][key] ? 1 : -1;
} else {
return a[1][key] < b[1][key] ? 1 : -1;
}
});
const result = {};
for (const [k, v] of sorted) {
result[k] = v;
}
return result;
}
async _save() {
try {
if (this.type === 'json') {
await fs.writeFile(this.dbPath, JSON.stringify(this.data, null, 2));
}
} catch (error) {
console.error('Error saving data:', error);
console.error(`If you couldn't solve the error, come here https://discord.gg/AYRDhFpRXE`)
}
}
}
module.exports = AikoDB;