@unikvs/cloudflare-kv
How to use @unikvs/cloudflare-kv, which stores byte sequences in a Cloudflare KV binding.
Overview
@unikvs/cloudflare-kv is storage that stores byte sequences in a Cloudflare Workers KV binding.
This storage prepends a prefix to every key and put / get values as-is as binary-safe ArrayBuffers.
nameisCloudflareKv.isOpenis alwaystrue.open()/close()only check for interruption.deleteis idempotent; missing keys are treated as success.- Streamed reading and writing are not supported.
Install it with the following command:
npm install @unikvs/cloudflare-kvpnpm add @unikvs/cloudflare-kvyarn add @unikvs/cloudflare-kvbun add @unikvs/cloudflare-kvnub add @unikvs/cloudflare-kvaube add @unikvs/cloudflare-kvUsage
CloudflareKv implements IStorage. Pass it to UniKvs via persist().
import { CloudflareKv } from "@unikvs/cloudflare-kv";
CloudflareKv
Storage that runs on a Cloudflare KV binding.
The constructor arguments are as follows:
bindingKVNamespaceLike
The KV binding to inject. Pass an object with get, put, delete, and list.
KVNamespaceLikekeyPrefix?string
The prefix prepended to every key.
string"unikvs:"ttlSeconds?number
The expiration (in seconds) attached on write. Unlimited when omitted. Passed as-is as put expirationTtl.
numberThe following example saves a value and reads it back:
import { CloudflareKv } from "@unikvs/cloudflare-kv";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{
foo: unikvs.Value<Uint8Array>;
}>()
.persist(new CloudflareKv({ binding }))
.build();
await kvs.open();
await kvs.set("foo", new Uint8Array([0, 1, 2]));
const data = await kvs.get("foo");
console.log(data); // Uint8Array(3) [0, 1, 2]
await kvs.close();
Data types
| Operation | Data type |
|---|---|
write / read |
Byte sequence (Uint8Array<ArrayBuffer>) |
- Only byte sequences can be stored. Convert strings or objects with middleware before passing them.
- Values are stored as-is as
ArrayBuffer; the view’sbyteOffsetis not carried over. clearlists the keys under the prefix withlistand then deletes them.- Streamed reading and writing are not supported.
Errors
This package throws the following errors.
InvalidBindingError
Meaning
Thrown at construction when binding is not an object with get, put, delete, and list.
How to fix
Pass a KVNamespace binding or an equivalent object with the four methods.
InvalidKeyPrefixError
Meaning
Thrown at construction when keyPrefix is not a string.
How to fix
Specify a string for keyPrefix.
InvalidTtlSecondsError
Meaning
Thrown at construction when ttlSeconds is neither a positive integer nor undefined.
How to fix
Specify a positive integer for ttlSeconds, or omit it.
KeyNotFoundError
Meaning
Thrown by read when the specified key does not exist. A shared @unikvs/core error.
How to fix
Check with exists before reading.
Example
The following example catches KeyNotFoundError.
import { KeyNotFoundError } from "@unikvs/cloudflare-kv";
try {
const data = await kvs.get("missing-key");
} catch (ex) {
if (ex instanceof KeyNotFoundError) {
console.log("Key not found");
} else {
throw ex;
}
}