| 1 | /** |
| 2 | * Runs database migrations for PGlite projects that use Effect SQL. |
| 3 | * |
| 4 | * This module re-exports the shared migration loaders and errors, then provides |
| 5 | * `run` and `layer` helpers that apply pending migration files with the current |
| 6 | * `SqlClient`. It does not require a separate PGlite service; the active SQL |
| 7 | * client supplies the database connection. |
| 8 | * |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | import type * as Effect from "effect/Effect" |
| 12 | import * as Layer from "effect/Layer" |
| 13 | import * as Migrator from "effect/sql/Migrator" |
| 14 | import type * as Client from "effect/sql/SqlClient" |
| 15 | import type { SqlError } from "effect/sql/SqlError" |
| 16 | |
| 17 | /** |
| 18 | * @since 4.0.0 |
| 19 | */ |
| 20 | export * from "effect/sql/Migrator" |
| 21 | |
| 22 | /** |
| 23 | * Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied. |
| 24 | * |
| 25 | * @category running |
| 26 | * @since 4.0.0 |
| 27 | */ |
| 28 | export const run: <R2 = never>( |
| 29 | options: Migrator.MigratorOptions<R2> |
| 30 | ) => Effect.Effect< |
| 31 | ReadonlyArray<readonly [id: number, name: string]>, |
| 32 | Migrator.MigrationError | SqlError, |
| 33 | Client.SqlClient | R2 |
| 34 | > = Migrator.make({}) |
| 35 | |
| 36 | /** |
| 37 | * Creates a layer that runs the configured SQL migrations during layer construction. |
| 38 | * |
| 39 | * @category layers |
| 40 | * @since 4.0.0 |
| 41 | */ |
| 42 | export const layer = <R>( |
| 43 | options: Migrator.MigratorOptions<R> |
| 44 | ): Layer.Layer<never, Migrator.MigrationError | SqlError, Client.SqlClient | R> => Layer.effectDiscard(run(options)) |