Connecting Hono to D1
We have our database and our schema. Now let’s write the API endpoints to use them!
First, we need to initialize Drizzle inside our Hono app. We do this using the DB binding we configured in wrangler.json.
import { Hono } from "hono";import { drizzle } from "drizzle-orm/d1";import { task } from "./db/schema"; // Import your table definitionimport { eq } from "drizzle-orm";
// Define the environment type so TypeScript knows about DBtype Env = { Bindings: { DB: D1Database; };};
const app = new Hono<Env>();Reading Data (SELECT)
Section titled “Reading Data (SELECT)”To fetch all tasks:
app.get("/tasks", async (c) => { const db = drizzle(c.env.DB);
// SELECT * FROM task const result = await db.select().from(task);
return c.json(result);});Filtering
Section titled “Filtering”To fetch a specific task by ID:
app.get("/tasks/:id", async (c) => { const id = c.req.param("id"); const db = drizzle(c.env.DB);
// SELECT * FROM task WHERE id = ? const result = await db.select().from(task).where(eq(task.id, id));
return c.json(result[0]); // Drizzle returns an array});Writing Data (INSERT)
Section titled “Writing Data (INSERT)”To create a new task:
app.post("/tasks", async (c) => { const body = await c.req.json(); const db = drizzle(c.env.DB);
const newTask = { id: crypto.randomUUID(), title: body.title, status: "todo", userId: "user_123", // Hardcoded for now createdAt: new Date(), updatedAt: new Date(), };
// INSERT INTO task VALUES (...) await db.insert(task).values(newTask);
return c.json(newTask, 201);});Updating Data (UPDATE)
Section titled “Updating Data (UPDATE)”To mark a task as completed:
app.patch("/tasks/:id", async (c) => { const id = c.req.param("id"); const db = drizzle(c.env.DB);
// UPDATE task SET status = 'completed' WHERE id = ? await db.update(task).set({ status: "completed" }).where(eq(task.id, id));
return c.json({ message: "Updated" });});Deleting Data (DELETE)
Section titled “Deleting Data (DELETE)”To remove a task:
app.delete("/tasks/:id", async (c) => { const id = c.req.param("id"); const db = drizzle(c.env.DB);
// DELETE FROM task WHERE id = ? await db.delete(task).where(eq(task.id, id));
return c.json({ message: "Deleted" });});Now you have the power to build a full CRUD (Create, Read, Update, Delete) API backed by a real database!