Documentation
SQLite
Cloudflare D1

Cloudflare D1

According to their official website (opens in a new tab), D1 is Cloudflare's first queryable relational database.

Drizzle ORM fully supports D1 database and Cloudflare Workers environment, we embrace SQL dialects and dialect specific drivers and syntax and mirror most popular SQLite-like all, get, values and run query methods syntax.

To setup project for your Cloudflare D1 - please refer to official docs (opens in a new tab)

## your wrangler.toml will look something like this
 
name = "YOUR PROJECT NAME"
main = "src/index.ts"
compatibility_date = "2022-11-07"
node_compat = true
 
[[ d1_databases ]]
binding = "DB"
database_name = "YOUR DB NAME"
database_id = "YOUR DB ID"

To init local database and run server locally

wrangler d1 execute <DATABASE_NAME> --local --file=./drizzle/0000_short_lockheed.sql
wrangler dev --local --persist

Install Drizzle ORM

npm install drizzle-orm
npm install -D drizzle-kit 

Make your first D1 query

import { drizzle } from 'drizzle-orm/d1';
 
export interface Env {
  <BINDING_NAME>: D1Database;
}
 
export default {
  async fetch(request: Request, env: Env) {
    const db = drizzle(env.<BINDING_NAME>);
    const result = await db.select().from(users).all()
    return Response.json(results);
  },
};