In this article I am going to take a look on some of the usage guide for Cloudflare D1 integration with Drizzle. I am going to use Hono Web API with Cloudflare worker for all this example. You can follow the setup guide here: Setting up D1 Database with Drizzle in a Hono Cloudflare Worker App
Add Schema#
I am going to add some schemas for this article as below
ts
import { sqliteTable, int, text } from 'drizzle-orm/sqlite-core';
import { sql } from 'drizzle-orm';
export const todo = sqliteTable("todo_definition", {
id: int().primaryKey({ autoIncrement: true }),
description : text().notNull(),
done: int({mode: "boolean"}).notNull().default(false),
version : text().notNull(),
created_at : text().notNull().default(sql`(current_timestamp)`),
updated_at : text().default(sql`(current_timestamp)`),
metadata: text({ mode: 'json' })
})Then we can proceed with drizzle migration for this
shell
pnpm drizzle-kit generate --config=drizzle.config.ts
wrangler d1 migrations apply "Hono DB"Update Hono#
ts
import {Hono} from "hono";
import {App} from "../types";
import {drizzle} from "drizzle-orm/d1";
import {todo} from "../db/schema";
export const todoApp = new Hono<App>()
todoApp.post('', async(c) => {
const todoData = await c.req.json();
const db = drizzle(c.env.DB);
try {
const [schemaResult] = await db
.insert(todo)
.values({
description: todoData.description,
done: false,
version: "0.0.1",
metadata: todoData.metadata,
})
.returning();
return c.json(schemaResult);
} catch (error) {
return c.json({ error: (error as Error).message }, 400);
}
})References#
https://developers.cloudflare.com/workers/wrangler/commands/#d1-migrations-apply
Related posts
- Setting up D1 Database with Drizzle in a Hono Cloudflare Worker AppThis guide covers configuring a D1 database using Drizzle in a Hono app deployed using Cloudflare Workers.. 🚀cloudflare workercloudflare D1HonoDrizzle
- Lark Suite API in Action with Hono and Cloudlfare worker: Basic GuideLark Suite is a application developed by ByteDance as 1 stop solution for all business need. Lark Suite do provide a very generous free tier with many feature that can compete with the like of Google Workspace, Microsoft 365 or Zoho. Lark Suite also provide a comprehensive API that can be used to integrated with any site. In this guide, we will take a look on how to set up Lark API and integrate it with Sveltekit. 🚀lark suitecloudflare workerHono
- Migrate cloudflare D1 to another cloudflare D1 accountThis guide covers migrating a D1 database to another cloudflare D1 account.. 🚀cloudflare D1