Skip to content

Commit

Permalink
improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jollytoad committed Jul 4, 2024
1 parent 99211c3 commit e3c6421
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
50 changes: 50 additions & 0 deletions store-common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,53 @@ utility function for use by implementations.
- [the interface](./types.ts)
- [key conversion utils](./key-utils.ts)
- [test utils](./test-storage-module.ts)

## StorageModule

If you need to reference the interface for a storage module:

```ts
import type { StorageModule } from "jsr:@jollytoad/store-common/types";
```

## Test Utils

If you want to implement your own storage modules the test utils can help you
get started with some standard tests...

**Example test case**

```ts
import {
open,
testClearItems,
testGetItem,
testHasItem,
testIsWriteable,
testListItems,
testOrdering,
testRemoveItem,
testSetItem,
testUrl,
} from "@jollytoad/store-common/test-storage-module";

// This is your custom storage module
import * as store from "./mod.ts";

Deno.test("store-my-custom-storage", async (t) => {
try {
await open(t, store);
await testUrl(t, store, "store-my-custom-storage");
await testIsWriteable(t, store);
await testSetItem(t, store);
await testHasItem(t, store);
await testGetItem(t, store);
await testListItems(t, store);
await testRemoveItem(t, store);
await testClearItems(t, store);
await testOrdering(t, store);
} finally {
await store.close();
}
});
```
53 changes: 48 additions & 5 deletions store-deno-kv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ Uses the [Deno KV](https://deno.land/manual/runtime/kv) API for storage.

Import mapping: `"$store": "jsr:@jollytoad/store-deno-kv"`

## Get out of jail free

This package also supplies a utility function to obtained the actual `Deno.Kv`
of any store backed by it (that exports a `getDenoKv` function).

**Example**

```ts
Expand All @@ -27,3 +22,51 @@ assertEquals(await store.getItem(["foo", "hello"]), "world");
await store.clearItems(["foo"]);
assertEquals(await store.hasItem(["foo", "hello"]), false);
```

## Get out of jail free

This package also supplies a utility function to obtained the actual `Deno.Kv`
of any store backed by it (that exports a `getDenoKv` function).

**Examples**

Using `getDenoKv()` with a store that uses `Deno.Kv`:

```ts
import * as store from "jsr:@jollytoad/store";
import { getDenoKv } from "jsr:@jollytoad/store-deno-kv/get-deno-kv";
import { assertEquals, assertInstanceOf } from "jsr:@std/assert";

// Set store-deno-kv
store.setStore(import("jsr:@jollytoad/store-deno-kv"));

// Get the actual Deno.Kv store
const kv = await getDenoKv(store, ["foo"]);

assertInstanceOf(kv, Deno.Kv);

await kv.atomic()
.set(["foo", "hello"], "world")
.commit();

assertEquals(await store.hasItem(["foo", "hello"]), true);
assertEquals(await store.getItem(["foo", "hello"]), "world");

await store.clearItems(["foo"]);
```

Or with a store that doesn't use `Deno.Kv`:

```ts
import * as store from "jsr:@jollytoad/store";
import { getDenoKv } from "jsr:@jollytoad/store-deno-kv/get-deno-kv";
import { assertStrictEquals } from "jsr:@std/assert";

// Set store-deno-kv
store.setStore(import("jsr:@jollytoad/store-deno-fs"));

// Attempt to get the actual Deno.Kv store
const kv = await getDenoKv(store, ["foo"]);

assertStrictEquals(kv, undefined);
```
9 changes: 9 additions & 0 deletions store-deno-kv/get-deno-kv.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* This module provides the {@linkcode getDenoKv} function, to aid
* obtaining of `Deno.Kv` from any storage module if it's available.
*
* @module
*/
import type {
DelegatedStore,
StorageKey,
Expand All @@ -13,6 +19,9 @@ import type { ExposeDenoKv } from "./types.ts";
* import { getDenoKv } from "jsr:@jollytoad/store-deno-kv/get-deno-kv";
* import * as store from "jsr:@jollytoad/store";
*
* // Set the delegate store, try changing this to "jsr:@jollytoad/store-deno-fs"
* store.setStore(import("jsr:@jollytoad/store-deno-kv"));
*
* const kv = await getDenoKv(store, ["foo"]);
*
* const key = ["foo", "counter"];
Expand Down

0 comments on commit e3c6421

Please sign in to comment.