Skip to main content
Home
This release is 3 versions behind 0.30.0 — the latest version of @kitsonk/kv-toolbox. Jump to latest

Built and signed on GitHub Actions

Utilities for working with Deno KV. Encrypted values, batching atomic transactions, handling blobs, querying/filtering, and more.

This package works with Node.js, Deno, Bun, Browsers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
4 weeks ago (0.29.0)

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 Blobs for storage via set.

Examples

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 Files

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();

Functions

f
get

Retrieve a binary object entry from the store with a given key that has been set.

f
getAsBlob

Retrieve a binary object from the store as a Blob or File that has been previously set.

f
getAsJSON

Retrieve a binary object from the store as an object which which be safely converted into a JSON string.

f
getAsResponse

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.

f
getAsStream

Retrieve a binary object from the store as a byte ReadableStream.

f
getMeta

Retrieve a binary object's meta data from the store as a Deno.KvEntryMaybe.

f
list

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.

f
remove

Remove/delete a binary object from the store with a given key that has been set.

f
set

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.

f
toBlob

A convenience function which converts a string value to a Blob which can be stored via set. The function optionally takes a type which represents the media type of the string (e.g. "text/plain" or "text/html"). type defaults to "text/plain".

f
toJSON

Convert a typed array, array buffer, Blob or File into a form that can be converted into a JSON string.

f
toValue

Convert a previously encoded object into an instance of File.

Interfaces

I
BlobBlobJSON

An interface to represent a Blob value as JSON.

I
BlobBufferJSON

An interface to represent a array buffer or typed array value as JSON.

I
BlobFileJSON

An interface to represent a File value as JSON.

I
BlobKvListOptions

Options which can be used when calling list.

Type Aliases

T
BlobJSON

An interface to represent a blob value as JSON.

T
BlobMeta

When a blob entry was originally a Blob or File a sub-entry will be set with the value of this meta data.

Variables

v
BLOB_KEY

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.

v
BLOB_META_KEY

If there is meta data associated with a blob entry, like for something that was originally a Blob or File, then this will be set as a sub-key of that blob key with a value of the meta data.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@kitsonk/kv-toolbox

Import symbol

import * as mod from "@kitsonk/kv-toolbox/blob";
or

Import directly with a jsr specifier

import * as mod from "jsr:@kitsonk/kv-toolbox/blob";

Add Package

pnpm i jsr:@kitsonk/kv-toolbox
or (using pnpm 10.8 or older)
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
or (using Yarn 4.8 or older)
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";