| 1 | import * as Cloudflare from "alchemy/Cloudflare"; |
| 2 | import * as Drizzle from "alchemy/Drizzle"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | /** |
| 6 | * A Drizzle schema + D1 database. The database's `migrations` prop is wired |
| 7 | * to the schema resource, so the provider order becomes: |
| 8 | * |
| 9 | * 1. `Drizzle.Schema` regenerates pending migration SQL files whenever |
| 10 | * `src/schema.ts` drifts from the latest checked-in snapshot. |
| 11 | * 2. `Cloudflare.D1.Database` scans the directory and applies any new |
| 12 | * migrations into the `drizzle_migrations` table. |
| 13 | */ |
| 14 | export const Database = Effect.gen(function* () { |
| 15 | const schema = yield* Drizzle.Schema("app-schema", { |
| 16 | schema: "./src/schema.ts", |
| 17 | out: "./migrations", |
| 18 | dialect: "sqlite", |
| 19 | }); |
| 20 | |
| 21 | return yield* Cloudflare.D1.Database("app-db", { |
| 22 | migrations: schema, |
| 23 | }); |
| 24 | }); |