This commit is contained in:
Emmaline Autumn 2024-10-18 17:09:41 -06:00
parent 2c697b0de9
commit d43f5e71db
4 changed files with 46 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"deno.enable": true
}

10
deno.json Normal file
View File

@ -0,0 +1,10 @@
{
"name": "@bearmetal/store",
"version": "0.0.1",
"description": "A simple store for storing data in a JSON file.",
"files": [
"mod.ts",
"store.ts"
],
"exports": "./mod.ts"
}

1
mod.ts Normal file
View File

@ -0,0 +1 @@
export { BearMetalStore } from "./store.ts";

32
store.ts Normal file
View File

@ -0,0 +1,32 @@
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));
}
}