@effect/sql-sqlite-wasm 4.0.0-beta.7 → 4.0.0-beta.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/OpfsWorker.d.ts +7 -3
- package/dist/OpfsWorker.d.ts.map +1 -1
- package/dist/OpfsWorker.js +50 -6
- package/dist/OpfsWorker.js.map +1 -1
- package/dist/SqliteClient.d.ts +49 -21
- package/dist/SqliteClient.d.ts.map +1 -1
- package/dist/SqliteClient.js +80 -36
- package/dist/SqliteClient.js.map +1 -1
- package/dist/SqliteMigrator.d.ts +28 -6
- package/dist/SqliteMigrator.d.ts.map +1 -1
- package/dist/SqliteMigrator.js +9 -5
- package/dist/SqliteMigrator.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/package.json +3 -3
- package/src/OpfsWorker.ts +52 -6
- package/src/SqliteClient.ts +108 -45
- package/src/SqliteMigrator.ts +28 -6
- package/src/index.ts +3 -3
- package/src/sqlite-wasm.d.ts +30 -2
package/dist/SqliteMigrator.d.ts
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
|
3
21
|
*/
|
|
4
22
|
import type * as Effect from "effect/Effect";
|
|
5
23
|
import * as Layer from "effect/Layer";
|
|
@@ -7,17 +25,21 @@ import * as Migrator from "effect/unstable/sql/Migrator";
|
|
|
7
25
|
import type * as Client from "effect/unstable/sql/SqlClient";
|
|
8
26
|
import type { SqlError } from "effect/unstable/sql/SqlError";
|
|
9
27
|
/**
|
|
10
|
-
* @since
|
|
28
|
+
* @since 4.0.0
|
|
11
29
|
*/
|
|
12
30
|
export * from "effect/unstable/sql/Migrator";
|
|
13
31
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
32
|
+
* Runs SQL migrations for a SQLite WASM database using the shared `Migrator` implementation and the current `SqlClient`.
|
|
33
|
+
*
|
|
34
|
+
* @category constructors
|
|
35
|
+
* @since 4.0.0
|
|
16
36
|
*/
|
|
17
37
|
export declare const run: <R>(options: Migrator.MigratorOptions<R>) => Effect.Effect<ReadonlyArray<readonly [id: number, name: string]>, SqlError | Migrator.MigrationError, Client.SqlClient | R>;
|
|
18
38
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
39
|
+
* Creates a layer that runs the configured SQLite WASM migrations during layer construction and provides no services.
|
|
40
|
+
*
|
|
41
|
+
* @category constructors
|
|
42
|
+
* @since 4.0.0
|
|
21
43
|
*/
|
|
22
44
|
export declare const layer: <R>(options: Migrator.MigratorOptions<R>) => Layer.Layer<never, SqlError | Migrator.MigrationError, R | Client.SqlClient>;
|
|
23
45
|
//# sourceMappingURL=SqliteMigrator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SqliteMigrator.d.ts","sourceRoot":"","sources":["../src/SqliteMigrator.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"SqliteMigrator.d.ts","sourceRoot":"","sources":["../src/SqliteMigrator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAA;AAC5C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,QAAQ,MAAM,8BAA8B,CAAA;AACxD,OAAO,KAAK,KAAK,MAAM,MAAM,+BAA+B,CAAA;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAE5D;;GAEG;AACH,cAAc,8BAA8B,CAAA;AAE5C;;;;;GAKG;AACH,eAAO,MAAM,GAAG,EAAE,CAAC,CAAC,EAClB,OAAO,EAAE,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,KACjC,MAAM,CAAC,MAAM,CAChB,aAAa,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,EAClD,QAAQ,GAAG,QAAQ,CAAC,cAAc,EAClC,MAAM,CAAC,SAAS,GAAG,CAAC,CACD,CAAA;AAErB;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAAI,CAAC,EACrB,SAAS,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,KACnC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,GAAG,QAAQ,CAAC,cAAc,EAAE,CAAC,GAAG,MAAM,CAAC,SAAS,CAAsC,CAAA"}
|
package/dist/SqliteMigrator.js
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
import * as Layer from "effect/Layer";
|
|
2
2
|
import * as Migrator from "effect/unstable/sql/Migrator";
|
|
3
3
|
/**
|
|
4
|
-
* @since
|
|
4
|
+
* @since 4.0.0
|
|
5
5
|
*/
|
|
6
6
|
export * from "effect/unstable/sql/Migrator";
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Runs SQL migrations for a SQLite WASM database using the shared `Migrator` implementation and the current `SqlClient`.
|
|
9
|
+
*
|
|
10
|
+
* @category constructors
|
|
11
|
+
* @since 4.0.0
|
|
10
12
|
*/
|
|
11
13
|
export const run = /*#__PURE__*/Migrator.make({});
|
|
12
14
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
+
* Creates a layer that runs the configured SQLite WASM migrations during layer construction and provides no services.
|
|
16
|
+
*
|
|
17
|
+
* @category constructors
|
|
18
|
+
* @since 4.0.0
|
|
15
19
|
*/
|
|
16
20
|
export const layer = options => Layer.effectDiscard(run(options));
|
|
17
21
|
//# sourceMappingURL=SqliteMigrator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SqliteMigrator.js","names":["Layer","Migrator","run","make","layer","options","effectDiscard"],"sources":["../src/SqliteMigrator.ts"],"sourcesContent":[null],"mappings":"
|
|
1
|
+
{"version":3,"file":"SqliteMigrator.js","names":["Layer","Migrator","run","make","layer","options","effectDiscard"],"sources":["../src/SqliteMigrator.ts"],"sourcesContent":[null],"mappings":"AAsBA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,QAAQ,MAAM,8BAA8B;AAIxD;;;AAGA,cAAc,8BAA8B;AAE5C;;;;;;AAMA,OAAO,MAAMC,GAAG,gBAMZD,QAAQ,CAACE,IAAI,CAAC,EAAE,CAAC;AAErB;;;;;;AAMA,OAAO,MAAMC,KAAK,GAChBC,OAAoC,IAC6CL,KAAK,CAACM,aAAa,CAACJ,GAAG,CAACG,OAAO,CAAC,CAAC","ignoreList":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
export * as OpfsWorker from "./OpfsWorker.ts";
|
|
5
5
|
/**
|
|
6
|
-
* @since
|
|
6
|
+
* @since 4.0.0
|
|
7
7
|
*/
|
|
8
8
|
export * as SqliteClient from "./SqliteClient.ts";
|
|
9
9
|
/**
|
|
10
|
-
* @since
|
|
10
|
+
* @since 4.0.0
|
|
11
11
|
*/
|
|
12
12
|
export * as SqliteMigrator from "./SqliteMigrator.ts";
|
|
13
13
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
export * as OpfsWorker from "./OpfsWorker.js";
|
|
5
5
|
/**
|
|
6
|
-
* @since
|
|
6
|
+
* @since 4.0.0
|
|
7
7
|
*/
|
|
8
8
|
export * as SqliteClient from "./SqliteClient.js";
|
|
9
9
|
/**
|
|
10
|
-
* @since
|
|
10
|
+
* @since 4.0.0
|
|
11
11
|
*/
|
|
12
12
|
export * as SqliteMigrator from "./SqliteMigrator.js";
|
|
13
13
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/sql-sqlite-wasm",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.71",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A SQLite toolkit for Effect",
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@effect/wa-sqlite": "^0.2.1",
|
|
47
|
-
"effect": "^4.0.0-beta.
|
|
47
|
+
"effect": "^4.0.0-beta.71"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@effect/wa-sqlite": "^0.1.2",
|
|
51
|
-
"effect": "^4.0.0-beta.
|
|
51
|
+
"effect": "^4.0.0-beta.71"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"codegen": "effect-utils codegen",
|
package/src/OpfsWorker.ts
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Worker-side entry point for SQLite WASM databases stored in the browser's
|
|
3
|
+
* Origin Private File System (OPFS).
|
|
4
|
+
*
|
|
5
|
+
* This module opens `@effect/wa-sqlite` with the OPFS access-handle VFS and
|
|
6
|
+
* serves the message protocol used by the worker-backed SQLite WASM client.
|
|
7
|
+
* Run it from a dedicated worker, or from a `SharedWorker` connection port, to
|
|
8
|
+
* keep durable local SQLite storage off the main thread while preserving the
|
|
9
|
+
* database in OPFS.
|
|
10
|
+
*
|
|
11
|
+
* **Mental model**
|
|
12
|
+
*
|
|
13
|
+
* - {@link run} owns one SQLite connection for one OPFS database name.
|
|
14
|
+
* - The worker posts `ready` after opening the database and then handles the
|
|
15
|
+
* client protocol for SQL statements, import, export, update hooks, and
|
|
16
|
+
* close.
|
|
17
|
+
* - The port in {@link OpfsWorkerConfig} is the only communication channel
|
|
18
|
+
* between the client and the worker loop.
|
|
19
|
+
* - Closing the port, sending `close`, or interrupting the surrounding scope
|
|
20
|
+
* releases the SQLite database handle.
|
|
21
|
+
*
|
|
22
|
+
* **Common tasks**
|
|
23
|
+
*
|
|
24
|
+
* Use this module for local-first browser data, offline caches,
|
|
25
|
+
* client-side migrations, and import/export workflows that need OPFS
|
|
26
|
+
* durability. Start {@link run} in the worker script, then connect the
|
|
27
|
+
* application through the worker-backed `SqliteClient` constructor.
|
|
28
|
+
*
|
|
29
|
+
* **Gotchas**
|
|
30
|
+
*
|
|
31
|
+
* OPFS requires browser support and a secure origin. Coordinate multiple tabs
|
|
32
|
+
* or workers before opening or migrating the same database, because this module
|
|
33
|
+
* owns a single connection and does not provide cross-tab locking. Close unused
|
|
34
|
+
* ports so access handles are released promptly.
|
|
35
|
+
*
|
|
36
|
+
* **See also**
|
|
37
|
+
*
|
|
38
|
+
* - {@link OpfsWorkerConfig} for the required worker port and database name.
|
|
39
|
+
* - {@link run} for starting the worker message loop.
|
|
40
|
+
*
|
|
41
|
+
* @since 4.0.0
|
|
3
42
|
*/
|
|
4
43
|
/// <reference lib="webworker" />
|
|
5
44
|
// oxlint-disable-next-line effect/no-import-from-barrel-package
|
|
@@ -7,12 +46,17 @@ import * as WaSqlite from "@effect/wa-sqlite"
|
|
|
7
46
|
import SQLiteESMFactory from "@effect/wa-sqlite/dist/wa-sqlite.mjs"
|
|
8
47
|
import { AccessHandlePoolVFS } from "@effect/wa-sqlite/src/examples/AccessHandlePoolVFS.js"
|
|
9
48
|
import * as Effect from "effect/Effect"
|
|
10
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
49
|
+
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"
|
|
11
50
|
import type { OpfsWorkerMessage } from "./internal/opfsWorker.ts"
|
|
12
51
|
|
|
52
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
53
|
+
classifySqliteError(cause, { message, operation })
|
|
54
|
+
|
|
13
55
|
/**
|
|
56
|
+
* Configuration for the SQLite OPFS worker, including the message port used for the client protocol and the OPFS database name to open.
|
|
57
|
+
*
|
|
14
58
|
* @category models
|
|
15
|
-
* @since
|
|
59
|
+
* @since 4.0.0
|
|
16
60
|
*/
|
|
17
61
|
export interface OpfsWorkerConfig {
|
|
18
62
|
readonly port: EventTarget & Pick<MessagePort, "postMessage" | "close">
|
|
@@ -20,8 +64,10 @@ export interface OpfsWorkerConfig {
|
|
|
20
64
|
}
|
|
21
65
|
|
|
22
66
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
67
|
+
* Runs the SQLite OPFS worker loop, opening the configured database, posting a ready message, handling query/import/export/update-hook messages, and closing when a close message is received.
|
|
68
|
+
*
|
|
69
|
+
* @category constructors
|
|
70
|
+
* @since 4.0.0
|
|
25
71
|
*/
|
|
26
72
|
export const run = (
|
|
27
73
|
options: OpfsWorkerConfig
|
|
@@ -34,7 +80,7 @@ export const run = (
|
|
|
34
80
|
const db = yield* Effect.acquireRelease(
|
|
35
81
|
Effect.try({
|
|
36
82
|
try: () => sqlite3.open_v2(options.dbName, undefined, "opfs"),
|
|
37
|
-
catch: (cause) => new SqlError({ cause,
|
|
83
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to open database", "openDatabase") })
|
|
38
84
|
}),
|
|
39
85
|
(db) => Effect.sync(() => sqlite3.close(db))
|
|
40
86
|
)
|
package/src/SqliteClient.ts
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* SQLite WASM client implementation for Effect SQL, backed by `@effect/wa-sqlite`.
|
|
3
|
+
*
|
|
4
|
+
* This module exposes constructors and layers for providing both the
|
|
5
|
+
* SQLite-specific `SqliteClient` service and the generic Effect `SqlClient`
|
|
6
|
+
* service in browser, worker, and test runtimes. Use it for local-first
|
|
7
|
+
* browser storage, offline caches, client-side migrations, sandboxed test
|
|
8
|
+
* databases, and import/export workflows that snapshot a SQLite database as a
|
|
9
|
+
* `Uint8Array`.
|
|
10
|
+
*
|
|
11
|
+
* `makeMemory` opens an in-memory database through the WASM memory VFS, so data
|
|
12
|
+
* is transient unless the client `export` result is persisted and later passed
|
|
13
|
+
* back to `import`. `make` talks to a scoped `Worker`, `SharedWorker`, or
|
|
14
|
+
* `MessagePort`, which is the path used by the OPFS worker helper for persistent
|
|
15
|
+
* browser storage. Worker-backed queries cross a message boundary, transferable
|
|
16
|
+
* buffers can be supplied with `withTransferables`, and imports transfer the
|
|
17
|
+
* supplied `Uint8Array` buffer to the worker.
|
|
18
|
+
*
|
|
19
|
+
* Both client variants serialize access through a single connection. A
|
|
20
|
+
* transaction holds that connection for the lifetime of its scope, so keep
|
|
21
|
+
* transactions short and use them for multi-statement writes that must commit
|
|
22
|
+
* atomically. OPFS availability depends on the browser and origin, and multiple
|
|
23
|
+
* tabs or workers opening the same OPFS database should coordinate migrations
|
|
24
|
+
* and writes outside this module. Worker-backed clients restart their scoped
|
|
25
|
+
* connection on worker errors, `executeStream` is not implemented there, and
|
|
26
|
+
* SQLite does not support `updateValues`.
|
|
27
|
+
*
|
|
28
|
+
* @since 4.0.0
|
|
3
29
|
*/
|
|
4
30
|
// oxlint-disable-next-line effect/no-import-from-barrel-package
|
|
5
31
|
import * as WaSqlite from "@effect/wa-sqlite"
|
|
6
32
|
import SQLiteESMFactory from "@effect/wa-sqlite/dist/wa-sqlite.mjs"
|
|
7
33
|
import { MemoryVFS } from "@effect/wa-sqlite/src/examples/MemoryVFS.js"
|
|
8
34
|
import * as Config from "effect/Config"
|
|
35
|
+
import * as Context from "effect/Context"
|
|
9
36
|
import * as Deferred from "effect/Deferred"
|
|
10
37
|
import * as Effect from "effect/Effect"
|
|
11
38
|
import * as Exit from "effect/Exit"
|
|
@@ -15,32 +42,40 @@ import * as Layer from "effect/Layer"
|
|
|
15
42
|
import * as Scope from "effect/Scope"
|
|
16
43
|
import * as ScopedRef from "effect/ScopedRef"
|
|
17
44
|
import * as Semaphore from "effect/Semaphore"
|
|
18
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
19
45
|
import * as Stream from "effect/Stream"
|
|
20
46
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
21
47
|
import * as Client from "effect/unstable/sql/SqlClient"
|
|
22
48
|
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
23
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
49
|
+
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"
|
|
24
50
|
import * as Statement from "effect/unstable/sql/Statement"
|
|
25
51
|
import type { OpfsWorkerMessage } from "./internal/opfsWorker.ts"
|
|
26
52
|
|
|
27
53
|
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
28
54
|
|
|
55
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
56
|
+
classifySqliteError(cause, { message, operation })
|
|
57
|
+
|
|
29
58
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
59
|
+
* Runtime identifier attached to SQLite WASM client values.
|
|
60
|
+
*
|
|
61
|
+
* @category type IDs
|
|
62
|
+
* @since 4.0.0
|
|
32
63
|
*/
|
|
33
64
|
export const TypeId: TypeId = "~@effect/sql-sqlite-wasm/SqliteClient"
|
|
34
65
|
|
|
35
66
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
67
|
+
* Type-level identifier for SQLite WASM client values.
|
|
68
|
+
*
|
|
69
|
+
* @category type IDs
|
|
70
|
+
* @since 4.0.0
|
|
38
71
|
*/
|
|
39
72
|
export type TypeId = "~@effect/sql-sqlite-wasm/SqliteClient"
|
|
40
73
|
|
|
41
74
|
/**
|
|
75
|
+
* SQLite WASM client service interface, extending `SqlClient` with database `export` and `import` operations and marking `updateValues` as unsupported for SQLite.
|
|
76
|
+
*
|
|
42
77
|
* @category models
|
|
43
|
-
* @since
|
|
78
|
+
* @since 4.0.0
|
|
44
79
|
*/
|
|
45
80
|
export interface SqliteClient extends Client.SqlClient {
|
|
46
81
|
readonly [TypeId]: TypeId
|
|
@@ -53,14 +88,18 @@ export interface SqliteClient extends Client.SqlClient {
|
|
|
53
88
|
}
|
|
54
89
|
|
|
55
90
|
/**
|
|
91
|
+
* Context service tag for the SQLite WASM client.
|
|
92
|
+
*
|
|
56
93
|
* @category tags
|
|
57
|
-
* @since
|
|
94
|
+
* @since 4.0.0
|
|
58
95
|
*/
|
|
59
|
-
export const SqliteClient =
|
|
96
|
+
export const SqliteClient = Context.Service<SqliteClient>("@effect/sql-sqlite-wasm/SqliteClient")
|
|
60
97
|
|
|
61
98
|
/**
|
|
99
|
+
* Configuration for an in-memory SQLite WASM client, including optional reactivity hooks, span attributes, and query/result name transforms.
|
|
100
|
+
*
|
|
62
101
|
* @category models
|
|
63
|
-
* @since
|
|
102
|
+
* @since 4.0.0
|
|
64
103
|
*/
|
|
65
104
|
export interface SqliteClientMemoryConfig {
|
|
66
105
|
readonly installReactivityHooks?: boolean
|
|
@@ -70,8 +109,10 @@ export interface SqliteClientMemoryConfig {
|
|
|
70
109
|
}
|
|
71
110
|
|
|
72
111
|
/**
|
|
112
|
+
* Configuration for a worker-backed SQLite WASM client, including the scoped worker or message port, optional reactivity hooks, span attributes, and query/result name transforms.
|
|
113
|
+
*
|
|
73
114
|
* @category models
|
|
74
|
-
* @since
|
|
115
|
+
* @since 4.0.0
|
|
75
116
|
*/
|
|
76
117
|
export interface SqliteClientConfig {
|
|
77
118
|
readonly worker: Effect.Effect<Worker | SharedWorker | MessagePort, never, Scope.Scope>
|
|
@@ -97,8 +138,10 @@ const initEffect = Effect.runSync(
|
|
|
97
138
|
const registered = new Set<string>()
|
|
98
139
|
|
|
99
140
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
141
|
+
* Creates a scoped in-memory SQLite WASM client using the memory VFS, serializing access through a semaphore and exposing database `export` and `import` operations.
|
|
142
|
+
*
|
|
143
|
+
* @category constructors
|
|
144
|
+
* @since 4.0.0
|
|
102
145
|
*/
|
|
103
146
|
export const makeMemory = (
|
|
104
147
|
options: SqliteClientMemoryConfig
|
|
@@ -125,7 +168,7 @@ export const makeMemory = (
|
|
|
125
168
|
const db = yield* Effect.acquireRelease(
|
|
126
169
|
Effect.try({
|
|
127
170
|
try: () => sqlite3.open_v2(":memory:", undefined, "memory-vfs"),
|
|
128
|
-
catch: (cause) => new SqlError({ cause,
|
|
171
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to open database", "openDatabase") })
|
|
129
172
|
}),
|
|
130
173
|
(db) => Effect.sync(() => sqlite3.close(db))
|
|
131
174
|
)
|
|
@@ -165,7 +208,7 @@ export const makeMemory = (
|
|
|
165
208
|
}
|
|
166
209
|
return results
|
|
167
210
|
},
|
|
168
|
-
catch: (cause) => new SqlError({ cause,
|
|
211
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
169
212
|
})
|
|
170
213
|
|
|
171
214
|
return identity<SqliteConnection>({
|
|
@@ -203,17 +246,19 @@ export const makeMemory = (
|
|
|
203
246
|
transformRows
|
|
204
247
|
? Stream.mapArray((chunk) => transformRows(chunk) as any)
|
|
205
248
|
: identity,
|
|
206
|
-
Stream.mapError((cause) =>
|
|
249
|
+
Stream.mapError((cause) =>
|
|
250
|
+
new SqlError({ reason: classifyError(cause, "Failed to execute statement", "stream") })
|
|
251
|
+
)
|
|
207
252
|
)
|
|
208
253
|
},
|
|
209
254
|
export: Effect.try({
|
|
210
255
|
try: () => sqlite3.serialize(db, "main"),
|
|
211
|
-
catch: (cause) => new SqlError({ cause,
|
|
256
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to export database", "export") })
|
|
212
257
|
}),
|
|
213
258
|
import(data) {
|
|
214
259
|
return Effect.try({
|
|
215
260
|
try: () => sqlite3.deserialize(db, "main", data, data.length, data.length, 1 | 2),
|
|
216
|
-
catch: (cause) => new SqlError({ cause,
|
|
261
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to import database", "import") })
|
|
217
262
|
})
|
|
218
263
|
}
|
|
219
264
|
})
|
|
@@ -225,7 +270,7 @@ export const makeMemory = (
|
|
|
225
270
|
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
|
226
271
|
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
|
227
272
|
const fiber = Fiber.getCurrent()!
|
|
228
|
-
const scope =
|
|
273
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
|
229
274
|
return Effect.as(
|
|
230
275
|
Effect.tap(
|
|
231
276
|
restore(semaphore.take(1)),
|
|
@@ -258,8 +303,10 @@ export const makeMemory = (
|
|
|
258
303
|
})
|
|
259
304
|
|
|
260
305
|
/**
|
|
261
|
-
*
|
|
262
|
-
*
|
|
306
|
+
* Creates a scoped worker-backed SQLite WASM client, communicating with the configured worker or message port, restarting the scoped connection on worker errors, and exposing database `export` and `import` operations.
|
|
307
|
+
*
|
|
308
|
+
* @category constructors
|
|
309
|
+
* @since 4.0.0
|
|
263
310
|
*/
|
|
264
311
|
export const make = (
|
|
265
312
|
options: SqliteClientConfig
|
|
@@ -297,7 +344,11 @@ export const make = (
|
|
|
297
344
|
if (!resume) return
|
|
298
345
|
pending.delete(id)
|
|
299
346
|
if (error) {
|
|
300
|
-
resume(
|
|
347
|
+
resume(
|
|
348
|
+
Exit.fail(
|
|
349
|
+
new SqlError({ reason: classifyError(error as string, "Failed to execute statement", "execute") })
|
|
350
|
+
)
|
|
351
|
+
)
|
|
301
352
|
} else {
|
|
302
353
|
resume(Exit.succeed(results))
|
|
303
354
|
}
|
|
@@ -383,7 +434,7 @@ export const make = (
|
|
|
383
434
|
const acquirer = semaphore.withPermits(1)(ScopedRef.get(connectionRef))
|
|
384
435
|
const transactionAcquirer = Effect.uninterruptibleMask(Effect.fnUntraced(function*(restore) {
|
|
385
436
|
const fiber = Fiber.getCurrent()!
|
|
386
|
-
const scope =
|
|
437
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
|
387
438
|
yield* restore(semaphore.take(1))
|
|
388
439
|
yield* Scope.addFinalizer(scope, semaphore.release(1))
|
|
389
440
|
return yield* ScopedRef.get(connectionRef)
|
|
@@ -422,81 +473,93 @@ const extractObject = (rows: [Array<string>, Array<any>]) => rows[1].map((row) =
|
|
|
422
473
|
const extractRows = (rows: [Array<string>, Array<any>]) => rows[1]
|
|
423
474
|
|
|
424
475
|
/**
|
|
476
|
+
* Fiber-local list of transferables to include with worker-backed SQLite WASM query messages.
|
|
477
|
+
*
|
|
425
478
|
* @category tranferables
|
|
426
|
-
* @since
|
|
479
|
+
* @since 4.0.0
|
|
427
480
|
*/
|
|
428
|
-
export const Transferables =
|
|
481
|
+
export const Transferables = Context.Reference<ReadonlyArray<Transferable>>(
|
|
429
482
|
"@effect/sql-sqlite-wasm/currentTransferables",
|
|
430
483
|
{ defaultValue: () => [] }
|
|
431
484
|
)
|
|
432
485
|
|
|
433
486
|
/**
|
|
487
|
+
* Runs an effect with the supplied transferables attached to worker-backed SQLite WASM query messages.
|
|
488
|
+
*
|
|
434
489
|
* @category tranferables
|
|
435
|
-
* @since
|
|
490
|
+
* @since 4.0.0
|
|
436
491
|
*/
|
|
437
492
|
export const withTransferables =
|
|
438
493
|
(transferables: ReadonlyArray<Transferable>) => <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
|
|
439
494
|
Effect.provideService(effect, Transferables, transferables)
|
|
440
495
|
|
|
441
496
|
/**
|
|
497
|
+
* Builds a layer from an Effect `Config` value, providing both the in-memory SQLite WASM `SqliteClient` service and the generic `SqlClient` service.
|
|
498
|
+
*
|
|
442
499
|
* @category layers
|
|
443
|
-
* @since
|
|
500
|
+
* @since 4.0.0
|
|
444
501
|
*/
|
|
445
502
|
export const layerMemoryConfig = (
|
|
446
503
|
config: Config.Wrap<SqliteClientMemoryConfig>
|
|
447
504
|
): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
448
|
-
Layer.
|
|
449
|
-
Config.unwrap(config).
|
|
505
|
+
Layer.effectContext(
|
|
506
|
+
Config.unwrap(config).pipe(
|
|
450
507
|
Effect.flatMap(makeMemory),
|
|
451
508
|
Effect.map((client) =>
|
|
452
|
-
|
|
453
|
-
|
|
509
|
+
Context.make(SqliteClient, client).pipe(
|
|
510
|
+
Context.add(Client.SqlClient, client)
|
|
454
511
|
)
|
|
455
512
|
)
|
|
456
513
|
)
|
|
457
514
|
).pipe(Layer.provide(Reactivity.layer))
|
|
458
515
|
|
|
459
516
|
/**
|
|
517
|
+
* Builds a layer from an in-memory SQLite WASM client configuration, providing both `SqliteClient` and the generic `SqlClient` service.
|
|
518
|
+
*
|
|
460
519
|
* @category layers
|
|
461
|
-
* @since
|
|
520
|
+
* @since 4.0.0
|
|
462
521
|
*/
|
|
463
522
|
export const layerMemory = (
|
|
464
523
|
config: SqliteClientMemoryConfig
|
|
465
524
|
): Layer.Layer<SqliteClient | Client.SqlClient, SqlError> =>
|
|
466
|
-
Layer.
|
|
525
|
+
Layer.effectContext(
|
|
467
526
|
Effect.map(makeMemory(config), (client) =>
|
|
468
|
-
|
|
469
|
-
|
|
527
|
+
Context.make(SqliteClient, client).pipe(
|
|
528
|
+
Context.add(Client.SqlClient, client)
|
|
470
529
|
))
|
|
471
530
|
).pipe(Layer.provide(Reactivity.layer))
|
|
472
531
|
|
|
473
532
|
/**
|
|
533
|
+
* Builds a layer from a worker-backed SQLite WASM client configuration, providing both `SqliteClient` and the generic `SqlClient` service.
|
|
534
|
+
*
|
|
474
535
|
* @category layers
|
|
475
|
-
* @since
|
|
536
|
+
* @since 4.0.0
|
|
476
537
|
*/
|
|
477
538
|
export const layer = (
|
|
478
539
|
config: SqliteClientConfig
|
|
479
540
|
): Layer.Layer<SqliteClient | Client.SqlClient, SqlError> =>
|
|
480
|
-
Layer.
|
|
541
|
+
Layer.effectContext(
|
|
481
542
|
Effect.map(make(config), (client) =>
|
|
482
|
-
|
|
483
|
-
|
|
543
|
+
Context.make(SqliteClient, client).pipe(
|
|
544
|
+
Context.add(Client.SqlClient, client)
|
|
484
545
|
))
|
|
485
546
|
).pipe(Layer.provide(Reactivity.layer))
|
|
486
547
|
|
|
487
548
|
/**
|
|
549
|
+
* Builds a layer from an Effect `Config` value, providing both the worker-backed SQLite WASM `SqliteClient` service and the generic `SqlClient` service.
|
|
550
|
+
*
|
|
488
551
|
* @category layers
|
|
489
|
-
* @since
|
|
552
|
+
* @since 4.0.0
|
|
490
553
|
*/
|
|
491
554
|
export const layerConfig = (
|
|
492
555
|
config: Config.Wrap<SqliteClientConfig>
|
|
493
556
|
): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
494
|
-
Layer.
|
|
495
|
-
Config.unwrap(config).
|
|
557
|
+
Layer.effectContext(
|
|
558
|
+
Config.unwrap(config).pipe(
|
|
496
559
|
Effect.flatMap(make),
|
|
497
560
|
Effect.map((client) =>
|
|
498
|
-
|
|
499
|
-
|
|
561
|
+
Context.make(SqliteClient, client).pipe(
|
|
562
|
+
Context.add(Client.SqlClient, client)
|
|
500
563
|
)
|
|
501
564
|
)
|
|
502
565
|
)
|
package/src/SqliteMigrator.ts
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
|
3
21
|
*/
|
|
4
22
|
import type * as Effect from "effect/Effect"
|
|
5
23
|
import * as Layer from "effect/Layer"
|
|
@@ -8,13 +26,15 @@ import type * as Client from "effect/unstable/sql/SqlClient"
|
|
|
8
26
|
import type { SqlError } from "effect/unstable/sql/SqlError"
|
|
9
27
|
|
|
10
28
|
/**
|
|
11
|
-
* @since
|
|
29
|
+
* @since 4.0.0
|
|
12
30
|
*/
|
|
13
31
|
export * from "effect/unstable/sql/Migrator"
|
|
14
32
|
|
|
15
33
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
34
|
+
* Runs SQL migrations for a SQLite WASM database using the shared `Migrator` implementation and the current `SqlClient`.
|
|
35
|
+
*
|
|
36
|
+
* @category constructors
|
|
37
|
+
* @since 4.0.0
|
|
18
38
|
*/
|
|
19
39
|
export const run: <R>(
|
|
20
40
|
options: Migrator.MigratorOptions<R>
|
|
@@ -25,8 +45,10 @@ export const run: <R>(
|
|
|
25
45
|
> = Migrator.make({})
|
|
26
46
|
|
|
27
47
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
48
|
+
* Creates a layer that runs the configured SQLite WASM migrations during layer construction and provides no services.
|
|
49
|
+
*
|
|
50
|
+
* @category constructors
|
|
51
|
+
* @since 4.0.0
|
|
30
52
|
*/
|
|
31
53
|
export const layer = <R>(
|
|
32
54
|
options: Migrator.MigratorOptions<R>
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
export * as OpfsWorker from "./OpfsWorker.ts"
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* @since
|
|
7
|
+
* @since 4.0.0
|
|
8
8
|
*/
|
|
9
9
|
export * as SqliteClient from "./SqliteClient.ts"
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* @since
|
|
12
|
+
* @since 4.0.0
|
|
13
13
|
*/
|
|
14
14
|
export * as SqliteMigrator from "./SqliteMigrator.ts"
|