Store/store.ts
2024-10-18 17:26:03 -06:00

33 lines
789 B
TypeScript

export class BearMetalStore {
private store: Record<string, string | number | boolean> = {};
private storePath: string;
constructor(storePath?: string) {
this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") ||
"./BearMetal/store.json";
this.readIn();
}
public get(key: string): string | number | boolean {
return this.store[key];
}
public set(key: string, value: string | number | boolean): void {
this.store[key] = value;
this.writeOut();
}
public delete(key: string): void {
delete this.store[key];
this.writeOut();
}
private readIn() {
this.store = JSON.parse(Deno.readTextFileSync(this.storePath));
}
private writeOut() {
Deno.writeTextFileSync(this.storePath, JSON.stringify(this.store));
}
}