Hono with Drizzle Project Structure
Hono integrated with Drizzle ORM for type-safe queries. Edge-compatible with D1, Turso, and Neon.
Project Directory
myproject/
src/
index.ts
Entry point
app.ts
Hono app setup
db/
Database layer
index.ts
Drizzle client
schema.ts
Table definitions
routes/
index.ts
users.ts
posts.ts
services/
user.service.ts
Drizzle queries
post.service.ts
middleware/
db.ts
Inject db in context
error-handler.ts
types/
index.ts
migrations/
Drizzle migrations
.gitkeep
drizzle.config.ts
Drizzle Kit config
wrangler.toml
D1 binding
package.json
tsconfig.json
.gitignore
Why This Structure?
Drizzle's lightweight runtime is perfect for edge. Schema-as-TypeScript means no code generation. Works with D1, Turso, Neon, and PlanetScale. Middleware injects the database client into Hono's context.
Key Directories
- src/db/schema.ts-Table definitions as TypeScript
- src/db/index.ts-Drizzle client configured for D1 or Turso
- src/middleware/db.ts-Injects db into
c.var.db - migrations/-SQL migrations from Drizzle Kit
Getting Started
npm create hono@latestnpm install drizzle-ormnpm install -D drizzle-kitnpx drizzle-kit generatewrangler d1 migrations apply
D1 Schema
// src/db/schema.ts
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
export const users = sqliteTable('users', {
id: integer('id').primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
email: text('email').unique(),
});
When To Use This
- Edge APIs needing persistent storage
- Cloudflare Workers with D1
- Serverless with Turso or Neon
- Type-safe queries without heavy runtime
- Projects preferring SQL-like syntax
Trade-offs
- D1 limits-SQLite on edge has size and query limits
- Migrations-Need to apply migrations per environment
- Connection handling-Edge has different connection semantics