A small KV example you can adapt quickly
This example keeps KV boring on purpose: one binding, one fetch handler, one assertion.
The fastest way to trust a binding is to wire one small use case end to end before you hide it behind a bigger app.
- Config focus
- Stable namespace naming
- Runtime shape
- Direct and calls in a fetch handler
- Best use
- A tiny cache or session-marker flow
Start by wiring the binding clearly in config
Minimal KV config
import { defineConfig } from 'devflare/config'
export default defineConfig({
name: 'kv-example',
files: {
fetch: 'src/fetch.ts'
},
bindings: {
kv: {
CACHE: 'cache-kv'
}
}
})
Then use it in one honest runtime path
- Run once the binding exists so is typed in both worker code and tests.
- Prefer a tiny route like this before you wrap KV behind a helper or service layer.
A tiny fetch handler that uses KV
import { env } from 'devflare'
export async function fetch(request: Request): Promise<Response> {
const url = new URL(request.url)
if (url.pathname === '/write') {
await env.CACHE.put('hello', 'from-kv')
return new Response('stored')
}
return new Response((await env.CACHE.get('hello')) ?? 'missing')
}
Lock in the behavior with one small test or smoke path
Start with the boring shape
If the first KV example already feels abstract, it is probably hiding the actual binding semantics instead of teaching them.
One tiny test is enough to trust the first version
import { afterAll, beforeAll, expect, test } from 'bun:test'
import { createTestContext, cf } from 'devflare/test'
import { env } from 'devflare'
beforeAll(() => createTestContext())
afterAll(() => env.dispose())
test('writes and reads through the worker', async () => {
await cf.worker.get('/write')
const response = await cf.worker.get('/')
expect(await response.text()).toBe('from-kv')
})
Previous
Testing KV
Use the default test harness first. KV is one of the bindings Devflare supports best in local tests.
Next
D1
D1 gets the same stable-name authoring story as KV, but the runtime shape is relational: , , , and prepared statements.