-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.ts
48 lines (43 loc) · 1.16 KB
/
db.ts
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
import { JSONFile } from "lowdb/node";
import { join } from "node:path";
import { existsSync, mkdirSync } from "node:fs";
import { Low } from "lowdb";
class Database {
private db: Low<any>;
constructor(file: string) {
file = join(import.meta.dirname, "/database/", file);
const adapter = new JSONFile(file);
this.db = new Low(adapter, {});
}
async init() {
await this.db.read();
this.db.data ||= {} as any;
await this.db.write();
}
async get(key: string) {
await this.init();
return this.db.data?.[key];
}
async set(key: string, value: any) {
await this.init();
this.db.data![key] = value;
await this.db.write();
return value;
}
async clear() {
await this.init();
this.db.data = {};
await this.db.write();
}
}
const users = new Database("/users.json");
const requested = new Database("/requested.json");
const links = new Database("/links.json");
const bans = new Database("/bans.json");
async function initDatabase() {
const dir = join(import.meta.dirname, "/database/");
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
}
export { users, requested, links, bans, initDatabase };