prasi-bun/app/srv/ws/sync/user.ts

42 lines
945 B
TypeScript

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