-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
174 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { assertInstanceOf, assertStrictEquals } from "@std/assert"; | ||
import { getDenoKv } from "./get-deno-kv.ts"; | ||
import * as denoKvStore from "./mod.ts"; | ||
import * as denoFsStore from "../store-deno-fs/mod.ts"; | ||
import * as store from "../store/mod.ts"; | ||
|
||
Deno.test("getDenoKv() returns Deno.Kv when given store-deno-kv directly", async () => { | ||
try { | ||
const kv = await getDenoKv(denoKvStore, []); | ||
|
||
assertInstanceOf(kv, Deno.Kv); | ||
} finally { | ||
await denoKvStore.close(); | ||
} | ||
}); | ||
|
||
Deno.test("getDenoKv() returns undefined when given store-deno-fs directly", async () => { | ||
try { | ||
const kv = await getDenoKv(denoFsStore, []); | ||
|
||
assertStrictEquals(kv, undefined); | ||
} finally { | ||
await denoFsStore.close(); | ||
} | ||
}); | ||
|
||
Deno.test("getDenoKv() returns Deno.Kv when given the delegate store backed by store-deno-kv", async () => { | ||
try { | ||
store.setStore(denoKvStore); | ||
|
||
const kv = await getDenoKv(store, []); | ||
|
||
assertInstanceOf(kv, Deno.Kv); | ||
} finally { | ||
await store.close(); | ||
} | ||
}); | ||
|
||
Deno.test("getDenoKv() returns Deno.Kv when given the delegate store backed by store-deno-fs", async () => { | ||
try { | ||
store.setStore(denoFsStore); | ||
|
||
const kv = await getDenoKv(store, []); | ||
|
||
assertStrictEquals(kv, undefined); | ||
} finally { | ||
await store.close(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { | ||
DelegatedStore, | ||
StorageKey, | ||
StorageModule, | ||
} from "@jollytoad/store-common/types"; | ||
import type { ExposeDenoKv } from "./types.ts"; | ||
|
||
/** | ||
* Utility to attempt to get the underlying `Deno.Kv` if the store is backed by it. | ||
* | ||
* @example | ||
* ```ts | ||
* import { getDenoKv } from "jsr:@jollytoad/store-deno-kv/get-deno-kv"; | ||
* import * as store from "jsr:@jollytoad/store"; | ||
* | ||
* const kv = await getDenoKv(store, ["foo"]); | ||
* | ||
* const key = ["foo", "counter"]; | ||
* | ||
* if (kv) { | ||
* // we can do an atomic increment... | ||
* kv.atomic() | ||
* .mutate({ type: "sum", key, value: new Deno.KvU64(1n) }) | ||
* .commit(); | ||
* } else { | ||
* // otherwise fallback to a risky, get and set... | ||
* await store.setItem(key, (await store.getItem<number>(key) ?? 0) + 1); | ||
* } | ||
* ``` | ||
*/ | ||
export async function getDenoKv( | ||
store: StorageModule & Partial<ExposeDenoKv> & Partial<DelegatedStore>, | ||
key: StorageKey, | ||
): Promise<Deno.Kv | undefined> { | ||
if ("getDenoKv" in store && typeof store.getDenoKv === "function") { | ||
return (store.getDenoKv)(key); | ||
} else if ("getStore" in store && typeof store.getStore === "function") { | ||
return getDenoKv(await store.getStore(), key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { StorageKey } from "@jollytoad/store-common/types"; | ||
|
||
/** | ||
* Additional functions for a store that is backed by `Deno.Kv` | ||
*/ | ||
export interface ExposeDenoKv { | ||
/** | ||
* Get the underlying `Deno.Kv` connection of the store. | ||
* | ||
* @param key supply the common prefix for any ops you wish to perform | ||
*/ | ||
getDenoKv(key: StorageKey): Promise<Deno.Kv>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters