| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import { makeEntrypointLayer } from "../../Runtime.ts"; |
| 4 | import { Self } from "../../Self.ts"; |
| 5 | import type { MigrationRecord } from "../../SQL/Migrations/Format.ts"; |
| 6 | |
| 7 | /** SQL files captured during construction and embedded in the Worker bundle. */ |
| 8 | export interface SqlMigrationSnapshot { |
| 9 | readonly _tag: "Cloudflare.SqlMigrations"; |
| 10 | /** The per-instance applied-migrations table. */ |
| 11 | readonly table: string; |
| 12 | /** Ordered SQL records, including their content hashes. */ |
| 13 | readonly records: ReadonlyArray<MigrationRecord>; |
| 14 | } |
| 15 | |
| 16 | /** @internal */ |
| 17 | export interface SqlMigrationsExport { |
| 18 | readonly kind: "sqlMigrations"; |
| 19 | readonly snapshot: SqlMigrationSnapshot; |
| 20 | } |
| 21 | |
| 22 | export class SqlMigrationsRuntime extends Context.Service< |
| 23 | SqlMigrationsRuntime, |
| 24 | Readonly<Record<string, SqlMigrationSnapshot>> |
| 25 | >()("Cloudflare.SqlMigrationsRuntime") {} |
| 26 | |
| 27 | /** Supply the generated migration records to every entrypoint and object activation. */ |
| 28 | export const withSqlMigrations = ( |
| 29 | entrypoint: unknown, |
| 30 | migrations: Readonly<Record<string, SqlMigrationSnapshot>>, |
| 31 | ) => |
| 32 | makeEntrypointLayer(Self, entrypoint).pipe( |
| 33 | Layer.provideMerge(Layer.succeed(SqlMigrationsRuntime, migrations)), |
| 34 | ); |