@vite-hub/kv
v0.6.1
Published
Vite-first key-value storage primitives and integration for ViteHub.
Maintainers
Readme
@vite-hub/kv
@vite-hub/kv gives server code one small-state API for settings, flags, cache entries, and JSON-like records.
Install
pnpm add @vite-hub/kvAdd @upstash/redis when you use the Upstash driver. The redis driver uses ioredis, which KV installs, and needs Redis 6 or later.
Minimal API
// server/api/settings.put.ts
import { kv } from "@vite-hub/kv"
import { defineEventHandler, readBody } from "h3"
export default defineEventHandler(async (event) => {
const [writeError] = await kv.set("settings", await readBody(event))
if (writeError) throw writeError
const [readError, settings] = await kv.get("settings")
if (readError) throw readError
return settings
})// vite.config.ts
import { hubKV } from "@vite-hub/kv/vite"
import { defineConfig } from "vite"
export default defineConfig({
plugins: [hubKV({
driver: "fs-lite",
base: ".vitehub/data/kv",
})],
})Vite Integration
Use hubKV() in Vite to resolve KV config and expose the kv runtime helper to server code.
An fs-lite store without base keeps the default store in .vitehub/data/kv and each named store in .vitehub/data/kv-stores/<store>, so named stores never share keys.
Providers include local fs-lite, Deno KV, Cloudflare Workers KV, Upstash Redis, Redis over TCP (redis), and a table in the app's Database (database). The storage layer is built on unstorage.
Shared stores on Node
Several Node processes share keys only through a shared store. Without a driver, a Node build picks:
databasewhen the app has a Default Database on Postgres (the@vite-hub/databaseplugin with adialect: "postgresql"Definition).fs-litein.vitehub/data/kvotherwise.
{ driver: "redis", url?, base? } reads url at runtime when it is env:<NAME>, and env:REDIS_URL is the default. A literal URL goes into the build output. base prefixes every key and defaults to vitehub:kv:<store>, so keys that are not ViteHub's stay apart. The generated store segment escapes %, :, /, \, and ?; an empty store name uses %. This keeps store names separate after key normalization. Two ViteHub apps on one Redis database share the default, so give each app its own database or base. A Redis listing cursor holds its whole state, so another server process can read the next page.
{ driver: "upstash", url?, token?, base? } uses the same base rule as redis: every key gets the prefix vitehub:kv:<store> unless base sets another one, so kv.clear() and kv.list() stay inside the store. Upstash takes whole seconds, so set() rounds a TTL up. Its listing cursor also holds its whole state, so the next page works in another serverless invocation.
{ driver: "database", database? } keeps keys in the table vitehub_kv of the Database database (default "default"), with one namespace per store. The store creates the table when it is missing; on Postgres, an advisory lock lets several servers start at once. Every atomic operation is one SQL statement, and expiry uses the clock of the Database. Writes start background cleanup of at most 1000 expired rows in their namespace. A full batch lets the next write start another batch; once a batch is not full, cleanup waits a minute. Postgres sweeps skip rows locked by another process. It works on Postgres and libSQL, not on D1.
The driver is selected at build time. Redis requires kv: { driver: "redis" }; every process must supply its URL, and a missing URL fails. An unrelated REDIS_URL never changes the selected driver. A build without a driver that has REDIS_URL, or a KV_URL that starts with rediss://, reports the hint KV_REDIS_URL_UNUSED in the build output and in .vitehub/hints.json, with the fix kv: { driver: "redis" }. On Vercel without Upstash variables, the same hint says that the inferred Upstash store fails at runtime. The kv.list operation (vitehub kv list, the Console) reports that driver.
This changes the default of a Node build with a Postgres Database from fs-lite to database. Keys in .vitehub/data/kv are not copied. Set kv: { driver: "fs-lite" } to keep the old store.
fs-lite writes each key as one file in base. The file name starts with key-, followed by the key with % and : escaped as in a URL, so signups and signups:reset-at are the files key-signups and key-signups%3Areset-at. This prefix keeps application keys separate from migration metadata and temporary files. Most file systems limit a file name to 255 bytes, so an escaped key longer than 251 bytes fails with KV_OPERATION_FAILED. Earlier versions wrote a:b as the file a/b. The store moves those files to their new names when it opens the folder and preserves literal % characters in old key names. It keeps a hidden layout record and metadata folder in base. The migration lock stays in that folder, so only base needs write access. Stop all processes that use the store before moving it. Copies and backups must preserve hidden files and hard links. Use GNU cp -a and verify the copied layout record before opening the copy; see Move an fs-lite store. Ordinary cp -R breaks the layout identity and can make existing keys inaccessible.
kv.set(key, value, { ttl }) expires a value on every driver. ttl is a duration such as "1h", or a number of seconds. Omitting ttl keeps the value until it is deleted or replaced. ttl is the only option; other options return KV_INVALID_OPTION. Cloudflare KV keeps a value at least 60 seconds. fs-lite keeps the expiry time of each key in its metadata folder and treats an expired key as missing. Initialization failures do not disable a store for the life of the process. The next operation can retry initialization.
Development stays local
In vite dev (and nuxt dev), a KV store is local unless it sets remote: true. When ViteHub infers the driver, Upstash env vars and the Vercel host do not select Upstash in development: the store is fs-lite in .vitehub/data/kv. On the Cloudflare host, a store uses the KV binding of the dev runtime when there is one, for example on the workerd runner. Without one, for example on Nitro's default Node runner, it uses fs-lite in .vitehub/data/kv-cloudflare/<binding> with the Workers KV limits.
kv: { remote: true } lets development infer the hosted store as a build does. On Cloudflare, ViteHub then writes remote: true on the generated Wrangler KV binding. The first operation prints KV store "<name>" is remote, and vitehub inspect definitions --kind kv shows Development: Remote. Writes change real data.
Development reads Upstash credentials from Vite's .env files for the current mode and envDir, with process environment values taking precedence. The credentials stay in the development server runtime.
An explicit driver is honored as before; an upstash store prints the same notice. Builds ignore remote. A remote value that is not a boolean fails with KV_INVALID_REMOTE. remote: true with fs-lite or deno-kv, remote: false with upstash, and remote: true in development without a hosted store fail with KV_REMOTE_CONFLICT.
Nitro storage
During Nuxt prerender, the Node worker uses the configured KV stores and ignores the development remote option. Cloudflare stores use their local folders, and an app with a Deno KV store keeps the runtime fallback.
When KV is enabled in a Nitro app, the default store is the Nitro storage mount kv, and a named store is the mount kv-<name>. useStorage("kv").getItem("a") reads the same value as kv.get("a"), and clear() on a mount clears only that store. Store names escape %, :, /, \\, and ? as percent-encoded characters; an empty name uses % (for example, a/b mounts at kv-a%2Fb). A mount with the same name in nitro.kv (or the deprecated nitro.storage) wins. Nuxt modules that take a mount name, such as nuxt-og-image's runtimeCacheStorage: "kv", store their data in KV this way.
kvStorageDriver from @vite-hub/kv/vite is the file path of the unstorage driver behind these mounts. Its options are store (default: the default store) and base, a key prefix. Pass it to a module that takes a driver name, or mount it yourself. The driver opens the store on each call, so on Cloudflare it uses the binding of the current request. Nitro's ttl goes to kv.set().
With vitehub(), cache: { driver: "kv" } mounts the same driver at Nitro's cache mount, with the nitro-cache: prefix or the base option.
Listing keys
kv.list({ prefix }) returns every key that starts with prefix. Add limit when the caller needs bounded work: kv.list({ prefix, limit, cursor }) reads one page. limit must be a positive integer. A page returns { keys, cursor? }; an omitted cursor means the listing is complete. On Cloudflare KV, each binding request returns at most 1,000 keys: with limit above 1,000, kv.list() caps the request at 1,000 keys and returns a cursor only when more keys remain; without limit, it reads every page and returns no cursor. kv.clear({ prefix }) deletes the keys that kv.list({ prefix }) lists.
Prefixes match text, and use the same key normalization as reads and writes. For example, users/al matches the stored key users:alice, and user matches users:alice and user-old:1. A trailing separator, such as users/ or users:, limits the prefix to children and does not match users-other.
Cursors are opaque and provider-specific. Pass a returned cursor back to the same store with the same prefix. Cloudflare KV, Upstash, Deno KV, and fs-lite stop enumeration when the requested page is full. Cloudflare KV and Deno KV cursors carry the provider cursor, so a request in another isolate can continue the listing. An fs-lite cursor retains a directory iterator in the current process for up to 15 minutes. Upstash uses the same retention window when the provider returns more keys than requested. These process-local cursors can expire sooner when more than 32 listings are left unfinished. Calling disposeKVStores() from the application shutdown hook releases these process-local cursors, closes retained directory iterators, and clears their expiry timers. A pending listing cannot retain a new cursor after disposal. Callers must restart listing without a cursor when continuation fails.
KV operations return [error, value]. error.code is one of the codes of the method, for example KV_INVALID_CURSOR for list(), and error.why, error.fix, and error.docs explain it. Provider failures use the code KV_OPERATION_FAILED and keep the provider failure in cause. A Cloudflare KV store checks the Workers KV limits before it calls the binding, in production and in vite dev: a key above 512 bytes returns KV_KEY_TOO_LONG, a value above 25 MiB returns KV_VALUE_TOO_LARGE, and a ttl below 60 seconds becomes 60 seconds. See KV limits and errors. Configuration errors, for example an unknown named store, throw.
kv.getMany(keys) reads many keys in one call and returns one value or null per key, in the order of keys. kv.setMany([{ key, value }], options) writes many keys; the last entry of a key wins, and it is not atomic. Redis uses MGET and one pipeline, the database driver one statement per 200 keys, Upstash MGET, and Cloudflare KV one bulk get() per 100 keys. Other reads and writes run one key at a time, 16 at once. Cloudflare KV checks every entry against the Workers KV limits before it writes any.
kv.setIfAbsent(key, value, { ttl }) writes the value only when the key has no value, in one atomic step. Its value is true for the one call that wrote it and false for the others. Use it for upload tickets, invite codes, and nonces instead of get() then set(), which two requests can race. Redis runs SET NX with PX, the database driver one INSERT ... ON CONFLICT that also replaces an expired key, and Upstash SET NX. Deno KV commits an atomic check that the key is missing; it deletes expired keys some time after the TTL. fs-lite creates the file exclusively, which is atomic on one disk, and expires keys with ttl on time. Cloudflare KV returns KV_UNSUPPORTED_OPERATION, because Workers KV is eventually consistent; use D1 or a Durable Object there.
redis, database, and Upstash also support kv.getAndDelete(key), kv.increment(key, ttl), and kv.deleteIfEquals(key, expected). These operations are atomic in the provider. deleteIfEquals() deletes the key only when its stored text equals expected serialized as set() stores it, and returns true when this call deleted it. Deno KV, Cloudflare KV, and fs-lite return KV_UNSUPPORTED_OPERATION because they cannot provide the same contract without extra storage or non-atomic calls. The type KVDriverAtomicOperations lists the atomic operations of each driver, and KVDriversWith<"deleteIfEquals"> the drivers of one operation.
Locks
kv.lock(key, { ttl, wait }) gives a lock that one runner holds at a time across the processes that share a store. run(fn) takes the lock, calls fn, and releases the lock, also when fn throws:
const [error, result] = await kv.lock("cron:daily", { ttl: "1m" }).run(() => sendDailyReport())
if (error) throw error
if (!result.acquired) return // Another runner holds the lock.ttl (a duration or seconds) is required, so a crashed runner never keeps the lock. wait (seconds) polls with backoff until the lock is free. Each lock writes a random owner token with setIfAbsent() and frees it with deleteIfEquals(), so a runner whose lock expired cannot delete the next runner's lock. acquire() returns a release() function for code that does not fit in one callback. KV does not renew a lock: when the work runs longer than ttl, another runner can take it.
Only the drivers in KVDriversWith<"deleteIfEquals"> (redis, database, upstash) support locks. Deno KV, Cloudflare KV, and fs-lite return KV_UNSUPPORTED_OPERATION without calling fn. App tests use fs-lite, so they return this code too.
Read-only handles
kv.store(name, { mode: "read" }) returns a ReadonlyKVStorage handle with get(), getMany(), has(), and list(). Write methods are a type error and absent at runtime. mode defaults to "write", and another value throws KV_INVALID_STORE_MODE.
import { kv } from "@vite-hub/kv"
const flags = kv.store("default", { mode: "read" })
const [error, value] = await flags.get("flags")The mode limits this API, not the host. The KV binding or provider token keeps full access, and code can still import the full kv export, so it is not a security boundary against code that reads the binding directly.
Application teardown
Call disposeKVStores() from @vite-hub/kv (or vite-hub/kv) after stopping new application work. It disposes every initialized default and named store, waits for pending store initialization and cleanup, and reports cleanup failures with an AggregateError. Concurrent teardown calls share one promise. Later operations create fresh stores; old process-local cursors cannot resume.
For an explicit Redis store or an inferred Node store, hubKV() registers this cleanup on Nitro's close hook, so the Redis connection does not keep the process alive after a stop signal. For other stores, register the cleanup in a server plugin:
// server/plugins/kv-cleanup.ts
import { disposeKVStores } from "@vite-hub/kv"
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("close", disposeKVStores)
})Failures in logs
A failed delete of expired rows in a database store does not fail the write. With the evlog Nitro module registered, KV logs each streak of failures once, as its own evlog event with kv: { op: "expiry-sweep", driver, namespace, code }. Without evlog, it goes to the console. See Limits and errors.
CLI
hubKV() contributes the vitehub kv CLI namespace: list [--prefix] [--limit] [--cursor], get <key>, has <key>, set <key> <value|@file> [--ttl <seconds>] [--json-value], and delete <key>. Each command accepts --store <name> and --json. get writes stored strings unchanged, without adding a newline. JSON input rejects underflow and decimal values that change magnitude during parsing; use strings to preserve them. Write commands print what they changed. There is no clear command.
vitehub seed writes each file in server/seed/kv/ to the default store: habits.json is the JSON value of the key habits, and any other extension is a string.
Each command runs a kv.* operation of the app through the Console RPC. On the dev server it needs no sign-in. With --url it calls a deployed app. The same operations serve the Console KV page, which edits and deletes keys, and agents.
Learn more at vitehub.dev.
