| 1 | /** |
| 2 | * Utilities for applying Effect SQL migrations to SQLite WASM 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 SQLite WASM `SqlClient`. Use it when a browser, worker, or test |
| 7 | * runtime needs to create or upgrade a local SQLite schema before repositories, |
| 8 | * caches, sync services, or other database-backed services start. |
| 9 | * |
| 10 | * The migrator operates on whichever WASM client is in the environment. With |
| 11 | * `SqliteClient.makeMemory`, migrations update an in-memory database, so the |
| 12 | * resulting schema is transient unless you persist it with the client's |
| 13 | * `export` and `import` operations. With worker-backed OPFS databases, run the |
| 14 | * migrator against the same worker configuration and OPFS database name used by |
| 15 | * the rest of the application, and coordinate startup across tabs or workers so |
| 16 | * only one migrator upgrades a given database at a time. OPFS availability is |
| 17 | * browser- and origin-dependent, and this adapter does not currently write |
| 18 | * SQLite schema dumps for `schemaDirectory`. |
| 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 SQLite WASM 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 SQLite WASM 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)) |