This commit is contained in:
Rizky 2023-10-21 00:13:22 +07:00
parent a03110bab4
commit 76bf7ebf53
4 changed files with 19 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import { dir } from "dir"; import { dir } from "dir";
import { open } from "lmdb"; import { RootDatabase, open } from "lmdb";
import { g } from "utils/global"; import { g } from "utils/global";
const defaultConf = { const defaultConf = {
@ -8,33 +8,38 @@ const defaultConf = {
}; };
export type UserConf = typeof defaultConf; export type UserConf = typeof defaultConf;
const db = open<UserConf, string>({
name: "user-conf",
path: dir.path(`${g.datadir}/lmdb/user-conf.lmdb`),
});
export const user = { export const user = {
conf: { conf: {
_db: null as null | RootDatabase<UserConf>,
get db() {
if (!this._db) {
this._db = open<UserConf, string>({
name: "user-conf",
path: dir.path(`${g.datadir}/lmdb/user-conf.lmdb`),
});
}
return this._db;
},
getOrCreate(user_id: string) { getOrCreate(user_id: string) {
let res = db.get(user_id); let res = this.db.get(user_id);
if (!res) { if (!res) {
db.put(user_id, structuredClone(defaultConf)); this.db.put(user_id, structuredClone(defaultConf));
res = db.get(user_id); res = this.db.get(user_id);
} }
return res as UserConf; return res as UserConf;
}, },
get(user_id: string) { get(user_id: string) {
return db.get(user_id); return this.db.get(user_id);
}, },
set<T extends keyof UserConf>(user_id: string, key: T, value: UserConf[T]) { set<T extends keyof UserConf>(user_id: string, key: T, value: UserConf[T]) {
let current = this.get(user_id); let current = this.get(user_id);
if (!current) { if (!current) {
db.put(user_id, structuredClone(defaultConf)); this.db.put(user_id, structuredClone(defaultConf));
current = this.get(user_id); current = this.get(user_id);
} }
if (current) { if (current) {
db.put(user_id, { ...current, [key]: value }); this.db.put(user_id, { ...current, [key]: value });
} }
}, },
}, },

View File

@ -19,6 +19,8 @@ export default page({
} else if (arr.length === 3) { } else if (arr.length === 3) {
navigate(location.pathname + "/"); navigate(location.pathname + "/");
} }
} else {
navigate("/editor/_/_");
} }
} else { } else {
navigate("/login"); navigate("/login");

Binary file not shown.

Binary file not shown.