|
| 1 | +import Npm from "@mdx/Npm.astro"; |
| 2 | +import Callout from '@mdx/Callout.astro'; |
| 3 | +import AnchorCards from '@mdx/AnchorCards.astro'; |
| 4 | +import Steps from '@mdx/Steps.astro'; |
| 5 | +import WhatsNextPostgres from "@mdx/WhatsNextPostgres.astro"; |
| 6 | +import Prerequisites from "@mdx/Prerequisites.astro"; |
| 7 | +import CodeTabs from "@mdx/CodeTabs.astro"; |
| 8 | + |
| 9 | +# Drizzle \<\> Cloudflare D1 |
| 10 | + |
| 11 | +<Prerequisites> |
| 12 | +- Database [connection basics](/docs/connect-overview) with Drizzle |
| 13 | +- **Cloudflare SQLite Durable Objects** - SQLite database embedded within a Durable Object - [read here](https://developers.cloudflare.com/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend) |
| 14 | +</Prerequisites> |
| 15 | + |
| 16 | +Drizzle ORM fully supports the Cloudflare Durable Objects database and Cloudflare Workers environment. |
| 17 | +We embrace SQL dialects and dialect specific drivers and syntax and mirror most popular |
| 18 | +SQLite-like `all`, `get`, `values` and `run` query methods syntax. |
| 19 | + |
| 20 | +To setup project for your Cloudflare Durable Objects please refer to **[official docs.](https://developers.cloudflare.com/durable-objects)** |
| 21 | + |
| 22 | +#### Step 1 - Install packages |
| 23 | +<Npm> |
| 24 | +drizzle-orm |
| 25 | +-D drizzle-kit |
| 26 | +</Npm> |
| 27 | + |
| 28 | +#### Step 2 - Initialize the driver and make a query |
| 29 | + |
| 30 | +You would need to have a `wrangler.toml` file for Durable Objects database and will look something like this: |
| 31 | +```toml {16-18,21-24} |
| 32 | +#:schema node_modules/wrangler/config-schema.json |
| 33 | +name = "sqlite-durable-objects" |
| 34 | +main = "src/index.ts" |
| 35 | +compatibility_date = "2024-11-12" |
| 36 | +compatibility_flags = [ "nodejs_compat" ] |
| 37 | + |
| 38 | +# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model. |
| 39 | +# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps. |
| 40 | +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects |
| 41 | +[[durable_objects.bindings]] |
| 42 | +name = "MY_DURABLE_OBJECT" |
| 43 | +class_name = "MyDurableObject" |
| 44 | + |
| 45 | +# Durable Object migrations. |
| 46 | +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations |
| 47 | +[[migrations]] |
| 48 | +tag = "v1" |
| 49 | +new_sqlite_classes = ["MyDurableObject"] |
| 50 | + |
| 51 | +# We need rules so we can import migrations in the next steps |
| 52 | +[[rules]] |
| 53 | +type = "Text" |
| 54 | +globs = ["**/*.sql"] |
| 55 | +fallthrough = true |
| 56 | +``` |
| 57 | + |
| 58 | +Make your first Durable Objects SQLite query: |
| 59 | +```typescript copy |
| 60 | +/// <reference types="@cloudflare/workers-types" /> |
| 61 | +import { drizzle, DrizzleSqliteDODatabase } from 'drizzle-orm/durable-sqlite'; |
| 62 | +import { DurableObject } from 'cloudflare:workers' |
| 63 | +import { migrate } from 'drizzle-orm/durable-sqlite/migrator'; |
| 64 | +import migrations from '../drizzle/migrations'; |
| 65 | +import { usersTable } from './db/schema'; |
| 66 | + |
| 67 | +export class MyDurableObject extends DurableObject { |
| 68 | + storage: DurableObjectStorage; |
| 69 | + db: DrizzleSqliteDODatabase<any>; |
| 70 | + |
| 71 | + constructor(ctx: DurableObjectState, env: Env) { |
| 72 | + super(ctx, env); |
| 73 | + this.storage = ctx.storage; |
| 74 | + this.db = drizzle(this.storage, { logger: false }); |
| 75 | + } |
| 76 | + |
| 77 | + async migrate() { |
| 78 | + migrate(this.db, migrations); |
| 79 | + } |
| 80 | + |
| 81 | + async insert(user: typeof usersTable.$inferInsert) { |
| 82 | + await this.db.insert(usersTable).values(user); |
| 83 | + } |
| 84 | + |
| 85 | + async select() { |
| 86 | + return this.db.select().from(usersTable); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export default { |
| 91 | + /** |
| 92 | + * This is the standard fetch handler for a Cloudflare Worker |
| 93 | + * |
| 94 | + * @param request - The request submitted to the Worker from the client |
| 95 | + * @param env - The interface to reference bindings declared in wrangler.toml |
| 96 | + * @param ctx - The execution context of the Worker |
| 97 | + * @returns The response to be sent back to the client |
| 98 | + */ |
| 99 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 100 | + const id: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName('durable-object'); |
| 101 | + const stub = env.MY_DURABLE_OBJECT.get(id); |
| 102 | + await stub.migrate(); |
| 103 | + |
| 104 | + await stub.insert({ |
| 105 | + name: 'John', |
| 106 | + age: 30, |
| 107 | + |
| 108 | + }); |
| 109 | + console.log('New user created!'); |
| 110 | + |
| 111 | + const users = await stub.select(); |
| 112 | + console.log('Getting all users from the database: ', users); |
| 113 | + |
| 114 | + return new Response(); |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +#### What's next? |
| 120 | + |
| 121 | +<WhatsNextPostgres/> |
0 commit comments