The best way to control Claude Code, Codex and more from any computer or phone
| 1 | # @alchemy.run/drizzle |
| 2 | |
| 3 | [Drizzle ORM](https://orm.drizzle.team) for [Alchemy](https://alchemy.run). Generate migrations from your schema at deploy time, and query D1, Postgres, MySQL, or libSQL from a Worker or Lambda with a typed Effect client. |
| 4 | |
| 5 | ```sh |
| 6 | npm install alchemy @alchemy.run/drizzle @alchemy.run/cloudflare drizzle-orm drizzle-kit effect |
| 7 | ``` |
| 8 | |
| 9 | A schema: |
| 10 | |
| 11 | ```typescript |
| 12 | // src/schema.ts |
| 13 | import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; |
| 14 | |
| 15 | export const Users = sqliteTable("users", { |
| 16 | id: integer("id").primaryKey({ autoIncrement: true }), |
| 17 | email: text("email").notNull().unique(), |
| 18 | name: text("name").notNull(), |
| 19 | }); |
| 20 | ``` |
| 21 | |
| 22 | A D1 database whose migrations are generated from that schema on deploy, and a Worker that queries it: |
| 23 | |
| 24 | ```typescript |
| 25 | // src/api.ts |
| 26 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 27 | import * as Drizzle from "@alchemy.run/drizzle"; |
| 28 | import * as Effect from "effect/Effect"; |
| 29 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 30 | import { Users } from "./schema.ts"; |
| 31 | |
| 32 | export const Database = Effect.gen(function* () { |
| 33 | const schema = yield* Drizzle.Schema("Schema", { |
| 34 | schema: "./src/schema.ts", |
| 35 | out: "./migrations", |
| 36 | dialect: "sqlite", |
| 37 | }); |
| 38 | return yield* Cloudflare.D1.Database("Db", { migrations: schema }); |
| 39 | }); |
| 40 | |
| 41 | export default class Api extends Cloudflare.Worker<Api>()( |
| 42 | "Api", |
| 43 | { main: import.meta.url }, |
| 44 | Effect.gen(function* () { |
| 45 | const d1 = yield* Cloudflare.D1.QueryDatabase(yield* Database); |
| 46 | const db = yield* Drizzle.D1(d1); |
| 47 | |
| 48 | return { |
| 49 | fetch: Effect.gen(function* () { |
| 50 | const users = yield* db.select().from(Users); |
| 51 | return yield* HttpServerResponse.json({ users }); |
| 52 | }).pipe(Effect.orDie), |
| 53 | }; |
| 54 | }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseBinding)), |
| 55 | ) {} |
| 56 | ``` |
| 57 | |
| 58 | ```typescript |
| 59 | // alchemy.run.ts |
| 60 | import * as Alchemy from "alchemy"; |
| 61 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 62 | import * as Drizzle from "@alchemy.run/drizzle"; |
| 63 | import * as Effect from "effect/Effect"; |
| 64 | import * as Layer from "effect/Layer"; |
| 65 | import Api from "./src/api.ts"; |
| 66 | |
| 67 | export default Alchemy.Stack( |
| 68 | "MyApp", |
| 69 | { |
| 70 | providers: Layer.mergeAll(Cloudflare.providers(), Drizzle.providers()), |
| 71 | state: Cloudflare.state(), |
| 72 | }, |
| 73 | Effect.gen(function* () { |
| 74 | const api = yield* Api; |
| 75 | return { url: api.url }; |
| 76 | }), |
| 77 | ); |
| 78 | ``` |
| 79 | |
| 80 | `Drizzle.Postgres`, `Drizzle.MySQL`, and `Drizzle.LibSQL` work the same way for the other databases. |
| 81 | |
| 82 | ## Learn more |
| 83 | |
| 84 | - [Drizzle with D1](https://alchemy.run/sql/drizzle/d1) |
| 85 | - [Drizzle with Postgres](https://alchemy.run/sql/drizzle/postgres) |
| 86 | - [Drizzle migrations](https://alchemy.run/sql/drizzle/migrations) |
| 87 | - [SQL on alchemy.run](https://alchemy.run/sql) |