Fixes poor typing on get

Adds ensureFile
This commit is contained in:
2024-10-18 19:30:05 -06:00
parent 0c34d521bc
commit fd7b218dc3
3 changed files with 12 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@bearmetal/store",
"version": "0.0.1",
"version": "0.0.2",
"description": "A simple store for storing data in a JSON file.",
"files": [
"mod.ts",
@@ -9,6 +9,7 @@
"exports": "./mod.ts",
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.6",
"@std/fs": "jsr:@std/fs@^1.0.4",
"@std/testing": "jsr:@std/testing@^1.0.3"
}
}

1
deno.lock generated
View File

@@ -44,6 +44,7 @@
"workspace": {
"dependencies": [
"jsr:@std/assert@^1.0.6",
"jsr:@std/fs@^1.0.4",
"jsr:@std/testing@^1.0.3"
]
}

View File

@@ -1,5 +1,9 @@
import { ensureFileSync } from "@std/fs";
const REFRESH_EVENT = "bm:refresh-store";
type StoreValue = string | number | boolean;
/**
* A no-dep, lightweight, simple store for storing data in a JSON file.
* usage:
@@ -37,6 +41,8 @@ export class BearMetalStore {
constructor(storePath?: string) {
this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") ||
"./BearMetal/store.json";
ensureFileSync(this.storePath);
this.EVENT_NAME = REFRESH_EVENT + this.storePath;
this.readIn();
@@ -47,8 +53,8 @@ export class BearMetalStore {
* @param key
* @returns The value stored at the key, or undefined if the key doesn't exist.
*/
public get(key: string): string | number | boolean {
return this.store[key];
public get<T = StoreValue>(key: string): T {
return this.store[key] as T;
}
/**
@@ -56,7 +62,7 @@ export class BearMetalStore {
* @param key
* @param value
*/
public set(key: string, value: string | number | boolean): void {
public set(key: string, value: StoreValue): void {
this.store[key] = value;
this.writeOut();
}