This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
47 lines (37 loc) · 1.41 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
import {Database} from "bun:sqlite";
export class UDB<T = any> {
private db: Database;
constructor() {
this.db = new Database("db.sqlite");
}
init() {
this.db.run(`CREATE TABLE IF NOT EXISTS users ("uuid" varchar(32) NOT NULL, "username" text NOT NULL, "indexBy" varchar(32) NOT NULL, PRIMARY KEY ("uuid"));`);
this.db.run(`CREATE TABLE IF NOT EXISTS keys ("key" varchar(32) NOT NULL, "owner" varchar(32) NOT NULL, PRIMARY KEY ("key"));`);
}
addUser(user: User, indexedBy: string) { // @ts-ignore
this.db.run(`INSERT OR REPLACE INTO users VALUES ($uuid, $username, $indexedBy)`, {$uuid: user.uuid, $username: user.username, $indexedBy: indexedBy});
}
getUsers() { // @ts-ignore
let users = [] as any;
this.db.query("SELECT * FROM users").all().forEach(user => {
users.push(user as User);
});
return users;
}
addKey(owner: string, key: string) { // @ts-ignore
this.db.run(`INSERT OR REPLACE INTO keys VALUES ($key, $owner)`, {$key: key, $owner: owner});
return {key: key, owner: owner};
}
getKey(key: string) {
return this.db.query(`SELECT * FROM keys WHERE key = $key`).get({$key: key});
}
removeKey(key: string) {
this.db.run(`DELETE FROM keys WHERE key = $key`);
return {key: key};
}
}
export type User = {
uuid: string;
username: string;
indexBy: string;
};