| 1 | import * as Layer from "effect/Layer"; |
| 2 | import * as Provider from "../Provider.ts"; |
| 3 | import { Schema, SchemaProvider } from "./Schema.ts"; |
| 4 | |
| 5 | export class Providers extends Provider.ProviderCollection<Providers>()( |
| 6 | "Drizzle", |
| 7 | ) {} |
| 8 | |
| 9 | export type ProviderRequirements = Layer.Services<ReturnType<typeof providers>>; |
| 10 | |
| 11 | /** |
| 12 | * Build-time providers for managing Drizzle schemas as Alchemy resources. |
| 13 | * Drizzle.Schema regenerates migration SQL via drizzle-kit's programmatic |
| 14 | * API on every deploy when the source schema changes. |
| 15 | * |
| 16 | * @example |
| 17 | * ```typescript |
| 18 | * import * as Alchemy from "alchemy"; |
| 19 | * import * as Cloudflare from "alchemy/Cloudflare"; |
| 20 | * import * as Drizzle from "alchemy/Drizzle"; |
| 21 | * import * as Neon from "alchemy/Neon"; |
| 22 | * import * as Effect from "effect/Effect"; |
| 23 | * import * as Layer from "effect/Layer"; |
| 24 | * |
| 25 | * export default Alchemy.Stack( |
| 26 | * "MyStack", |
| 27 | * { |
| 28 | * providers: Layer.mergeAll( |
| 29 | * Cloudflare.providers(), |
| 30 | * Drizzle.providers(), |
| 31 | * Neon.providers(), |
| 32 | * ), |
| 33 | * state: Alchemy.localState(), |
| 34 | * }, |
| 35 | * Effect.gen(function* () { |
| 36 | * const schema = yield* Drizzle.Schema("app-schema", { |
| 37 | * schema: "./src/schema.ts", |
| 38 | * }); |
| 39 | * const project = yield* Neon.Project("app-db"); |
| 40 | * const branch = yield* Neon.Branch("app-branch", { |
| 41 | * project, |
| 42 | * migrationsDir: schema.out, |
| 43 | * }); |
| 44 | * return { branchId: branch.branchId }; |
| 45 | * }), |
| 46 | * ); |
| 47 | * ``` |
| 48 | */ |
| 49 | export const providers = () => |
| 50 | Layer.effect(Providers, Provider.collection([Schema])).pipe( |
| 51 | Layer.provide(SchemaProvider()), |
| 52 | Layer.orDie, |
| 53 | ); |