Updated docs
This commit is contained in:
parent
900e70c7f6
commit
0c34d521bc
24
README.md
24
README.md
@ -1,17 +1,19 @@
|
|||||||
# BearMetalStore
|
# BearMetalStore
|
||||||
|
|
||||||
A no-dep, lightweight, simple store for storing data in a JSON file.
|
A no-dep, lightweight, synchronous store for storing data in a JSON file.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { BearMetalStore } from "https://deno.land/x/bearmetalstore@v0.0.1/mod.ts";
|
import { BearMetalStore } from "@bearmetal/store";
|
||||||
|
|
||||||
const store = new BearMetalStore();
|
const store = new BearMetalStore();
|
||||||
|
|
||||||
store.set("key", "value");
|
store.set("key", "value");
|
||||||
|
|
||||||
console.log(store.get("key"));
|
console.log(store.get("key"));
|
||||||
|
|
||||||
|
store.close();
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
@ -25,3 +27,21 @@ Creates a new store.
|
|||||||
- `storePath?: string`
|
- `storePath?: string`
|
||||||
|
|
||||||
The path to the store file.
|
The path to the store file.
|
||||||
|
|
||||||
|
### Environment Variables
|
||||||
|
|
||||||
|
- `BEAR_METAL_STORE_PATH`
|
||||||
|
|
||||||
|
The path to the store file. Can be used instead of the `storePath` parameter.
|
||||||
|
If neither is provided, the store will be stored in `./BearMetal/store.json`.
|
||||||
|
|
||||||
|
### Events
|
||||||
|
|
||||||
|
The store will dispatch a custom event with the name
|
||||||
|
`"bm:refresh-store" + storePath` when the store is updated. This can be used to
|
||||||
|
trigger a refresh of the store in other parts of the application.
|
||||||
|
|
||||||
|
## Permisions
|
||||||
|
|
||||||
|
Read and write access to the store file is required. Environment access to the
|
||||||
|
"BEAR_METAL_STORE_PATH" variable is required.
|
||||||
|
48
store.ts
48
store.ts
@ -1,9 +1,37 @@
|
|||||||
const REFRESH_EVENT = "bm:refresh-store";
|
const REFRESH_EVENT = "bm:refresh-store";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A no-dep, lightweight, simple store for storing data in a JSON file.
|
||||||
|
* usage:
|
||||||
|
* ```ts
|
||||||
|
* import { BearMetalStore } from "@bearmetal/store";
|
||||||
|
*
|
||||||
|
* const store = new BearMetalStore();
|
||||||
|
*
|
||||||
|
* store.set("key", "value");
|
||||||
|
*
|
||||||
|
* console.log(store.get("key"));
|
||||||
|
*
|
||||||
|
* store.close();
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* It's recommended to use the `using` syntax to ensure the store is disposed
|
||||||
|
* of when it's no longer needed.
|
||||||
|
* ```ts
|
||||||
|
* using store = new BearMetalStore();
|
||||||
|
*
|
||||||
|
* store.set("key", "value");
|
||||||
|
*
|
||||||
|
* console.log(store.get("key"));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
export class BearMetalStore {
|
export class BearMetalStore {
|
||||||
private store: Record<string, string | number | boolean> = {};
|
private store: Record<string, string | number | boolean> = {};
|
||||||
private storePath: string;
|
private storePath: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description The name of the event that is dispatched when the store is updated.
|
||||||
|
*/
|
||||||
public readonly EVENT_NAME: string;
|
public readonly EVENT_NAME: string;
|
||||||
|
|
||||||
constructor(storePath?: string) {
|
constructor(storePath?: string) {
|
||||||
@ -15,15 +43,28 @@ export class BearMetalStore {
|
|||||||
globalThis.addEventListener(this.EVENT_NAME, this.readIn);
|
globalThis.addEventListener(this.EVENT_NAME, this.readIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key
|
||||||
|
* @returns The value stored at the key, or undefined if the key doesn't exist.
|
||||||
|
*/
|
||||||
public get(key: string): string | number | boolean {
|
public get(key: string): string | number | boolean {
|
||||||
return this.store[key];
|
return this.store[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Sets the value of the key to the value.
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
public set(key: string, value: string | number | boolean): void {
|
public set(key: string, value: string | number | boolean): void {
|
||||||
this.store[key] = value;
|
this.store[key] = value;
|
||||||
this.writeOut();
|
this.writeOut();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Deletes the key and its value.
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
public delete(key: string): void {
|
public delete(key: string): void {
|
||||||
delete this.store[key];
|
delete this.store[key];
|
||||||
this.writeOut();
|
this.writeOut();
|
||||||
@ -42,4 +83,11 @@ export class BearMetalStore {
|
|||||||
this.writeOut();
|
this.writeOut();
|
||||||
globalThis.removeEventListener(this.EVENT_NAME, this.readIn);
|
globalThis.removeEventListener(this.EVENT_NAME, this.readIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Closes the store and disposes of the event listener.
|
||||||
|
*/
|
||||||
|
public close() {
|
||||||
|
this[Symbol.dispose]();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user