From d43f5e71db13b205b0445aa79f67102a3257434e Mon Sep 17 00:00:00 2001 From: Emma Date: Fri, 18 Oct 2024 17:09:41 -0600 Subject: [PATCH] init --- .vscode/settings.json | 3 +++ deno.json | 10 ++++++++++ mod.ts | 1 + store.ts | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 deno.json create mode 100644 mod.ts create mode 100644 store.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cbac569 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "deno.enable": true +} diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..c513337 --- /dev/null +++ b/deno.json @@ -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" +} diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..9dad161 --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export { BearMetalStore } from "./store.ts"; diff --git a/store.ts b/store.ts new file mode 100644 index 0000000..da824d8 --- /dev/null +++ b/store.ts @@ -0,0 +1,32 @@ +export class BearMetalStore { + private store: Record = {}; + 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)); + } +}