Files
itsc-timetable/store/index.ts
2023-02-16 03:01:04 +08:00

56 lines
1.1 KiB
TypeScript

import fs from "fs";
import { MongoClient } from "mongodb";
import util from "util";
const write = util.promisify(fs.writeFile);
const read = util.promisify(fs.readFile);
class Store {
record: Record<string, string>;
constructor() {
this.record = {};
}
public get() {
return this.record;
}
public set(key: string, val: string) {
this.record[key] = val;
}
public delete(key: string) {
delete this.record[key];
}
public update(record: Record<string, string>) {
for (const key in record) {
this.record[key] = record[key];
}
for (const key in this.record) {
if (record[key] === undefined) {
delete this.record[key];
}
}
}
}
class HTML {
html: string;
constructor() {
// load from file
try {
this.html = fs.readFileSync("./html.html", "utf8");
} catch {
this.html = "";
}
}
public get() {
return this.html;
}
public async set(html: string) {
this.html = html;
// store into file
await write("./html.html", html, "utf8");
}
}
export const html = new HTML();
export const store = new Store();