Kv.cloudflare
Adapts a Cloudflare Workers KV namespace (or compatible binding) into a Kv. Uses the underlying store's native expirationTtl for TTL — Cloudflare KV's minimum TTL is 60 seconds, enforced by the platform independent of what's passed here.
Usage
import { Kv } from 'accounts/server'
const kv = Kv.cloudflare(env.MY_KV_NAMESPACE)Wire the binding in wrangler.jsonc:
{
"kv_namespaces": [{ "binding": "MY_KV_NAMESPACE", "id": "<namespace-id>" }]
}Parameters
namespace
- Type:
Kv.cloudflare.Parameters - Required
A Cloudflare Workers KV namespace binding (or anything matching the same shape):
type Parameters = {
get: <value = unknown>(key: string, format: 'json') => Promise<value | null>
put: (
key: string,
value: string,
options?: { expirationTtl?: number } | undefined,
) => Promise<void>
delete: (key: string) => Promise<void>
}import { Handler, Kv } from 'accounts/server'
export default {
fetch(req: Request, env: { KV: KVNamespace }) {
const handler = Handler.exchange({
kv: Kv.cloudflare(env.KV),
})
return handler.fetch(req)
},
}Return Type
A Kv instance with get, set, and delete. take and create are intentionally omitted — handlers that require strict atomicity should be paired with Kv.durableObject instead.