Setting up D1 with Drizzle

via CLI:

npx wrangler d1 create <DATABASE_NAME>

returns toml config which is placed in your wrangler.toml file.

binding = "DB" # i.e. available in your Worker on env.DB
database_name = "test-db"
database_id = "d61ce790-55fd-4ec1-8b43-7d3617742a82"

Alternative its via the Cloudflare workers dashbord

setup drizzle schema then setup script to generate

        "generate": "drizzle-kit generate:sqlite --schema=./src/schema.ts",

This creates SQL scripts to create the relevant tables. It does not execute it on the table. to do that we run:

npx wrangler d1 execute email-worker --local --file=./drizzle/<DRIZZLE-GENERATED-FILE>>.sql

We not have a fully setup D1 database running locally which we can access in next.js like so:

import { getRequestContext } from "@cloudflare/next-on-pages";
import { drizzle } from "drizzle-orm/d1";

const DB = getRequestContext().env.APP_DB;
const db = drizzle(DB);

export default async function Page(){

  const todos = await;

      { => (
        <div key={}>{todo.text}</div>