@kitsonk/kv-toolbox@0.29.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Utilities for working with Deno KV. Encrypted values, batching atomic transactions, handling blobs, querying/filtering, and more.
A set of APIs for storing arbitrarily sized blobs in Deno KV. Currently Deno KV has a limit of key values being 64k.
The set
function breaks down a blob into chunks and manages
sub-keys to store the complete value, including preserving meta data
associated with Blob
and File
instances.
The get
, getAsBlob
, getAsStream
, and
getAsJSON
functions reverse that process. get
resolves with a standard Deno.KvEntryMaybe
, while the other
get*
methods just return or resolve with the value.
The getAsResponse
will resolve a blob entry as a
Response
using meta information to set appropriate headers. If
the entry is not found, the response will be set to a 404 Not Found
.
remove
function will delete the key, sub-keys and values.
getMeta
resolves with the meta data associated with a blob entry
stored in Deno KV. This information is useful for understanding the blob
value without having to read the blob out of the datastore. If the blob does
not exist null
is resolved.
toValue
and toJSON
are functions which allow
serializing blob like values to and from JSON.
toBlob
is a convenience function to convert string values into
Blob
s for storage via set
.
Basic usage
Basic usage
import { get, remove, set } from "@kitsonk/kv-toolbox/blob"; const kv = await Deno.openKv(); const data = new TextEncoder().encode("hello deno!"); await set(kv, ["hello"], data); const maybeAb = await get(kv, ["hello"]); // do something with maybeAb await remove(kv, ["hello"]); await kv.close();
Setting and getting File
s
Setting and getting File
s
import { getAsBlob, remove, set } from "@kitsonk/kv-toolbox/blob"; const kv = await Deno.openKv(); // assume this is form data submitted as a `Request` const body = new FormData(); for (const [name, value] of body) { if (value instanceof File) { await set(kv, ["files", name], value); } } // and then later const file = await getAsBlob(kv, ["file", "image"]); // now the `File` is restored and can be processed await remove(kv, ["file", "image"]); await kv.close();
Retrieve a binary object from the store as an object which which be safely converted into a JSON string.
Retrieve a blob value as a Response
which is suitable for sending
as a response to an HTTP request. This will read the blob out of the KV store
as a stream and set information in the response based on what is available
from the source.
Retrieve a binary object from the store as a byte ReadableStream
.
Retrieve a binary object's meta data from the store as a
Deno.KvEntryMaybe
.
Retrieve a list of keys in the database. The returned list is an
Deno.KvListIterator
which can be used to iterate over the blob
entries in the database, returning a ReadableStream
for each
matching blob entry. Any other values in the database will be ignored.
Set the blob value in the provided Deno.Kv
with the provided
key. The blob can be any array buffer like structure, a byte
ReadableStream
, or a Blob
.
When there are parts of a blob, this key will be set as a sub-key of the blob
blob entry, which will have additional sub-keys with the parts of the blob
stored as Uint8Array
with a key of an incrementing number.
Add Package
deno add jsr:@kitsonk/kv-toolbox
Import symbol
import * as mod from "@kitsonk/kv-toolbox/blob";
Import directly with a jsr specifier
import * as mod from "jsr:@kitsonk/kv-toolbox/blob";
Add Package
pnpm i jsr:@kitsonk/kv-toolbox
pnpm dlx jsr add @kitsonk/kv-toolbox
Import symbol
import * as mod from "@kitsonk/kv-toolbox/blob";
Add Package
yarn add jsr:@kitsonk/kv-toolbox
yarn dlx jsr add @kitsonk/kv-toolbox
Import symbol
import * as mod from "@kitsonk/kv-toolbox/blob";
Add Package
vlt install jsr:@kitsonk/kv-toolbox
Import symbol
import * as mod from "@kitsonk/kv-toolbox/blob";
Add Package
npx jsr add @kitsonk/kv-toolbox
Import symbol
import * as mod from "@kitsonk/kv-toolbox/blob";
Add Package
bunx jsr add @kitsonk/kv-toolbox
Import symbol
import * as mod from "@kitsonk/kv-toolbox/blob";