This commit is contained in:
2023-02-16 22:57:46 +08:00
parent 4bbdac8abe
commit 4ebe5026ed
6 changed files with 99 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
import fs from "fs";
import fs, { readFileSync } from "fs";
import { MongoClient } from "mongodb";
import util from "util";
@@ -6,9 +6,15 @@ const write = util.promisify(fs.writeFile);
const read = util.promisify(fs.readFile);
class Store {
filename: string;
record: Record<string, string>;
constructor() {
this.record = {};
constructor(filename: string) {
this.filename = filename;
try {
this.record = JSON.parse(readFileSync(filename, "utf8"));
} catch {
this.record = {};
}
}
public get() {
return this.record;
@@ -19,15 +25,8 @@ class Store {
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];
}
}
public async update(record: Record<string, string>) {
await write(this.filename, JSON.stringify(this.record), "utf8");
}
}
@@ -52,4 +51,5 @@ class HTML {
}
export const html = new HTML();
export const store = new Store();
export const store = new Store("store.json");
export const regular = new Store("regular.json");