TDocs/
search
Engineering··schedule4 min read

Building a Blog with D1 and Drizzle ORM

How to use Cloudflare D1 with Drizzle ORM for a type-safe, edge-native database layer.

Why D1?

Cloudflare D1 is a serverless SQLite database that runs at the edge. Combined with Drizzle ORM, it provides a type-safe, performant data layer.

Schema Design

import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core"

export const posts = sqliteTable("posts", {
  id: text("id").primaryKey(),
  title: text("title").notNull(),
  content: text("content").notNull(),
  status: text("status").default("draft"),
})

Querying with Drizzle

const published = await db.query.posts.findMany({
  where: eq(posts.status, "published"),
  orderBy: [desc(posts.createdAt)],
})

Benefits

  1. Zero egress fees: Unlike traditional databases
  2. SQLite compatibility: Battle-tested SQL engine
  3. Global replication: Data close to your users