Skip to content

Getting started

  1. Install the package (view on npm)

    Terminal window
    bun add drizzle-cloudflare-kv-cache-adapter

    drizzle-orm >= 0.44.0 is a peer dependency.

  2. Bind a KV namespace in your Wrangler config:

    [[kv_namespaces]]
    binding = "CACHE"
    id = "<your-kv-namespace-id>"

    Don’t have one yet? Create it:

    Terminal window
    wrangler kv namespace create CACHE
  3. Wire it into Drizzle

    import { drizzle } from 'drizzle-orm/d1'
    import { cloudflareKVCache } from 'drizzle-cloudflare-kv-cache-adapter'
    export default {
    async fetch(request: Request, env: Env) {
    const db = drizzle(env.DB, {
    cache: cloudflareKVCache(env.CACHE),
    })
    const users = await db.select().from(usersTable).$withCache()
    return Response.json(users)
    },
    }
  4. Deploy

    Terminal window
    wrangler deploy

That’s it. Continue to Usage for caching strategies and TTLs.