| 1 | /** |
| 2 | * Utilities for applying Effect SQL migrations to React Native SQLite databases. |
| 3 | * |
| 4 | * This module re-exports the shared `Migrator` loaders and error types, then |
| 5 | * provides `run` and `layer` helpers that execute ordered migrations through the |
| 6 | * current React Native SQLite `SqlClient`. Use it when a mobile app needs to |
| 7 | * bring its on-device database schema up to date during startup, before opening |
| 8 | * repositories or sync services, or in integration tests that create app-local |
| 9 | * database files. |
| 10 | * |
| 11 | * React Native SQLite databases are scoped by the client configuration, so the |
| 12 | * migrator should be run with the same `filename`, `location`, and encryption |
| 13 | * key as the rest of the application. Migrations run through the package's |
| 14 | * single serialized connection; by default statements use the synchronous |
| 15 | * driver API and can block the JS thread, so long migration sets may want to run |
| 16 | * under `SqliteClient.withAsyncQuery`. Mobile upgrades can be interrupted by app |
| 17 | * suspension or process death, so keep migrations transaction-aware and avoid |
| 18 | * assuming a fresh database on every launch. |
| 19 | * |
| 20 | * @since 4.0.0 |
| 21 | */ |
| 22 | import type * as Effect from "effect/Effect" |
| 23 | import * as Layer from "effect/Layer" |
| 24 | import * as Migrator from "effect/sql/Migrator" |
| 25 | import type * as Client from "effect/sql/SqlClient" |
| 26 | import type { SqlError } from "effect/sql/SqlError" |
| 27 | |
| 28 | /** |
| 29 | * @since 4.0.0 |
| 30 | */ |
| 31 | export * from "effect/sql/Migrator" |
| 32 | |
| 33 | /** |
| 34 | * Runs SQL migrations for a React Native SQLite database using the shared `Migrator` implementation and the current `SqlClient`. |
| 35 | * |
| 36 | * @category running |
| 37 | * @since 4.0.0 |
| 38 | */ |
| 39 | export const run: <R>( |
| 40 | options: Migrator.MigratorOptions<R> |
| 41 | ) => Effect.Effect< |
| 42 | ReadonlyArray<readonly [id: number, name: string]>, |
| 43 | SqlError | Migrator.MigrationError, |
| 44 | Client.SqlClient | R |
| 45 | > = Migrator.make({}) |
| 46 | |
| 47 | /** |
| 48 | * Creates a layer that runs the configured React Native SQLite migrations during layer construction and provides no services. |
| 49 | * |
| 50 | * @category layers |
| 51 | * @since 4.0.0 |
| 52 | */ |
| 53 | export const layer = <R>( |
| 54 | options: Migrator.MigratorOptions<R> |
| 55 | ): Layer.Layer<never, SqlError | Migrator.MigrationError, R | Client.SqlClient> => Layer.effectDiscard(run(options)) |