@effect/sql-sqlite-wasm 4.0.0-beta.1 → 4.0.0-beta.100
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/README.md +1 -1
- package/dist/OpfsWorker.d.ts +7 -3
- package/dist/OpfsWorker.d.ts.map +1 -1
- package/dist/OpfsWorker.js +20 -6
- package/dist/OpfsWorker.js.map +1 -1
- package/dist/SqliteClient.d.ts +52 -24
- package/dist/SqliteClient.d.ts.map +1 -1
- package/dist/SqliteClient.js +77 -41
- 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 +5 -6
- package/src/OpfsWorker.ts +22 -6
- package/src/SqliteClient.ts +105 -50
- 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,WACZ,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,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/sql-sqlite-wasm",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.100",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A SQLite toolkit for Effect",
|
|
7
7
|
"homepage": "https://effect.website",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/Effect-TS/effect
|
|
10
|
+
"url": "https://github.com/Effect-TS/effect.git",
|
|
11
11
|
"directory": "packages/sql/sqlite-wasm"
|
|
12
12
|
},
|
|
13
13
|
"bugs": {
|
|
14
|
-
"url": "https://github.com/Effect-TS/effect
|
|
14
|
+
"url": "https://github.com/Effect-TS/effect/issues"
|
|
15
15
|
},
|
|
16
16
|
"tags": [
|
|
17
17
|
"typescript",
|
|
@@ -44,16 +44,15 @@
|
|
|
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.100"
|
|
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.100"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"codegen": "effect-utils codegen",
|
|
55
55
|
"build": "tsc -b tsconfig.json && pnpm babel",
|
|
56
|
-
"build:tsgo": "tsgo -b tsconfig.json && pnpm babel",
|
|
57
56
|
"babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps",
|
|
58
57
|
"check": "tsc -b tsconfig.json",
|
|
59
58
|
"test": "vitest",
|
package/src/OpfsWorker.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Runs the worker side of the browser SQLite WASM client that stores data in
|
|
3
|
+
* OPFS.
|
|
4
|
+
*
|
|
5
|
+
* This module opens `@effect/wa-sqlite` with the OPFS access-handle VFS, then
|
|
6
|
+
* listens on a `MessagePort`-compatible port for the protocol used by
|
|
7
|
+
* `SqliteClient`. It sends a ready message, executes SQL messages, imports and
|
|
8
|
+
* exports database bytes, forwards update-hook notifications, and closes when
|
|
9
|
+
* requested. It is meant to run in a dedicated worker or a `SharedWorker`.
|
|
10
|
+
*
|
|
11
|
+
* @since 4.0.0
|
|
3
12
|
*/
|
|
4
13
|
/// <reference lib="webworker" />
|
|
5
14
|
// oxlint-disable-next-line effect/no-import-from-barrel-package
|
|
@@ -7,12 +16,17 @@ import * as WaSqlite from "@effect/wa-sqlite"
|
|
|
7
16
|
import SQLiteESMFactory from "@effect/wa-sqlite/dist/wa-sqlite.mjs"
|
|
8
17
|
import { AccessHandlePoolVFS } from "@effect/wa-sqlite/src/examples/AccessHandlePoolVFS.js"
|
|
9
18
|
import * as Effect from "effect/Effect"
|
|
10
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
19
|
+
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"
|
|
11
20
|
import type { OpfsWorkerMessage } from "./internal/opfsWorker.ts"
|
|
12
21
|
|
|
22
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
23
|
+
classifySqliteError(cause, { message, operation })
|
|
24
|
+
|
|
13
25
|
/**
|
|
26
|
+
* Configuration for the SQLite OPFS worker, including the message port used for the client protocol and the OPFS database name to open.
|
|
27
|
+
*
|
|
14
28
|
* @category models
|
|
15
|
-
* @since
|
|
29
|
+
* @since 4.0.0
|
|
16
30
|
*/
|
|
17
31
|
export interface OpfsWorkerConfig {
|
|
18
32
|
readonly port: EventTarget & Pick<MessagePort, "postMessage" | "close">
|
|
@@ -20,8 +34,10 @@ export interface OpfsWorkerConfig {
|
|
|
20
34
|
}
|
|
21
35
|
|
|
22
36
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
37
|
+
* 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.
|
|
38
|
+
*
|
|
39
|
+
* @category constructors
|
|
40
|
+
* @since 4.0.0
|
|
25
41
|
*/
|
|
26
42
|
export const run = (
|
|
27
43
|
options: OpfsWorkerConfig
|
|
@@ -34,7 +50,7 @@ export const run = (
|
|
|
34
50
|
const db = yield* Effect.acquireRelease(
|
|
35
51
|
Effect.try({
|
|
36
52
|
try: () => sqlite3.open_v2(options.dbName, undefined, "opfs"),
|
|
37
|
-
catch: (cause) => new SqlError({ cause,
|
|
53
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to open database", "openDatabase") })
|
|
38
54
|
}),
|
|
39
55
|
(db) => Effect.sync(() => sqlite3.close(db))
|
|
40
56
|
)
|
package/src/SqliteClient.ts
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Connects Effect SQL to SQLite compiled to WebAssembly with
|
|
3
|
+
* `@effect/wa-sqlite`.
|
|
4
|
+
*
|
|
5
|
+
* This module can create an in-memory SQLite database in the current runtime or
|
|
6
|
+
* connect to a worker-backed database, such as the OPFS worker from
|
|
7
|
+
* `OpfsWorker`. Both clients are exposed as `SqliteClient` and the generic
|
|
8
|
+
* Effect SQL client, serialize access, support database import and export, and
|
|
9
|
+
* can install reactivity hooks from SQLite update notifications. In-memory
|
|
10
|
+
* clients can stream query rows; worker-backed clients cannot. `updateValues`
|
|
11
|
+
* is not supported by this driver.
|
|
12
|
+
*
|
|
13
|
+
* @since 4.0.0
|
|
3
14
|
*/
|
|
4
15
|
// oxlint-disable-next-line effect/no-import-from-barrel-package
|
|
5
16
|
import * as WaSqlite from "@effect/wa-sqlite"
|
|
6
17
|
import SQLiteESMFactory from "@effect/wa-sqlite/dist/wa-sqlite.mjs"
|
|
7
18
|
import { MemoryVFS } from "@effect/wa-sqlite/src/examples/MemoryVFS.js"
|
|
8
19
|
import * as Config from "effect/Config"
|
|
20
|
+
import * as Context from "effect/Context"
|
|
9
21
|
import * as Deferred from "effect/Deferred"
|
|
10
22
|
import * as Effect from "effect/Effect"
|
|
11
23
|
import * as Exit from "effect/Exit"
|
|
@@ -14,32 +26,41 @@ import { identity } from "effect/Function"
|
|
|
14
26
|
import * as Layer from "effect/Layer"
|
|
15
27
|
import * as Scope from "effect/Scope"
|
|
16
28
|
import * as ScopedRef from "effect/ScopedRef"
|
|
17
|
-
import * as
|
|
29
|
+
import * as Semaphore from "effect/Semaphore"
|
|
18
30
|
import * as Stream from "effect/Stream"
|
|
19
31
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
20
32
|
import * as Client from "effect/unstable/sql/SqlClient"
|
|
21
33
|
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
22
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
34
|
+
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"
|
|
23
35
|
import * as Statement from "effect/unstable/sql/Statement"
|
|
24
36
|
import type { OpfsWorkerMessage } from "./internal/opfsWorker.ts"
|
|
25
37
|
|
|
26
38
|
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
27
39
|
|
|
40
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
41
|
+
classifySqliteError(cause, { message, operation })
|
|
42
|
+
|
|
28
43
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
44
|
+
* Runtime identifier attached to SQLite WASM client values.
|
|
45
|
+
*
|
|
46
|
+
* @category type IDs
|
|
47
|
+
* @since 4.0.0
|
|
31
48
|
*/
|
|
32
49
|
export const TypeId: TypeId = "~@effect/sql-sqlite-wasm/SqliteClient"
|
|
33
50
|
|
|
34
51
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
52
|
+
* Type-level identifier for SQLite WASM client values.
|
|
53
|
+
*
|
|
54
|
+
* @category type IDs
|
|
55
|
+
* @since 4.0.0
|
|
37
56
|
*/
|
|
38
57
|
export type TypeId = "~@effect/sql-sqlite-wasm/SqliteClient"
|
|
39
58
|
|
|
40
59
|
/**
|
|
60
|
+
* SQLite WASM client service interface, extending `SqlClient` with database `export` and `import` operations and marking `updateValues` as unsupported for SQLite.
|
|
61
|
+
*
|
|
41
62
|
* @category models
|
|
42
|
-
* @since
|
|
63
|
+
* @since 4.0.0
|
|
43
64
|
*/
|
|
44
65
|
export interface SqliteClient extends Client.SqlClient {
|
|
45
66
|
readonly [TypeId]: TypeId
|
|
@@ -52,14 +73,18 @@ export interface SqliteClient extends Client.SqlClient {
|
|
|
52
73
|
}
|
|
53
74
|
|
|
54
75
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
76
|
+
* Service tag for the SQLite WASM client.
|
|
77
|
+
*
|
|
78
|
+
* @category services
|
|
79
|
+
* @since 4.0.0
|
|
57
80
|
*/
|
|
58
|
-
export const SqliteClient =
|
|
81
|
+
export const SqliteClient = Context.Service<SqliteClient>("@effect/sql-sqlite-wasm/SqliteClient")
|
|
59
82
|
|
|
60
83
|
/**
|
|
84
|
+
* Configuration for an in-memory SQLite WASM client, including optional reactivity hooks, span attributes, and query/result name transforms.
|
|
85
|
+
*
|
|
61
86
|
* @category models
|
|
62
|
-
* @since
|
|
87
|
+
* @since 4.0.0
|
|
63
88
|
*/
|
|
64
89
|
export interface SqliteClientMemoryConfig {
|
|
65
90
|
readonly installReactivityHooks?: boolean
|
|
@@ -69,8 +94,10 @@ export interface SqliteClientMemoryConfig {
|
|
|
69
94
|
}
|
|
70
95
|
|
|
71
96
|
/**
|
|
97
|
+
* 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.
|
|
98
|
+
*
|
|
72
99
|
* @category models
|
|
73
|
-
* @since
|
|
100
|
+
* @since 4.0.0
|
|
74
101
|
*/
|
|
75
102
|
export interface SqliteClientConfig {
|
|
76
103
|
readonly worker: Effect.Effect<Worker | SharedWorker | MessagePort, never, Scope.Scope>
|
|
@@ -96,8 +123,10 @@ const initEffect = Effect.runSync(
|
|
|
96
123
|
const registered = new Set<string>()
|
|
97
124
|
|
|
98
125
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
126
|
+
* Creates a scoped in-memory SQLite WASM client using the memory VFS, serializing access through a semaphore and exposing database `export` and `import` operations.
|
|
127
|
+
*
|
|
128
|
+
* @category constructors
|
|
129
|
+
* @since 4.0.0
|
|
101
130
|
*/
|
|
102
131
|
export const makeMemory = (
|
|
103
132
|
options: SqliteClientMemoryConfig
|
|
@@ -124,7 +153,7 @@ export const makeMemory = (
|
|
|
124
153
|
const db = yield* Effect.acquireRelease(
|
|
125
154
|
Effect.try({
|
|
126
155
|
try: () => sqlite3.open_v2(":memory:", undefined, "memory-vfs"),
|
|
127
|
-
catch: (cause) => new SqlError({ cause,
|
|
156
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to open database", "openDatabase") })
|
|
128
157
|
}),
|
|
129
158
|
(db) => Effect.sync(() => sqlite3.close(db))
|
|
130
159
|
)
|
|
@@ -164,7 +193,7 @@ export const makeMemory = (
|
|
|
164
193
|
}
|
|
165
194
|
return results
|
|
166
195
|
},
|
|
167
|
-
catch: (cause) => new SqlError({ cause,
|
|
196
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
168
197
|
})
|
|
169
198
|
|
|
170
199
|
return identity<SqliteConnection>({
|
|
@@ -179,6 +208,9 @@ export const makeMemory = (
|
|
|
179
208
|
executeValues(sql, params) {
|
|
180
209
|
return run(sql, params, "array")
|
|
181
210
|
},
|
|
211
|
+
executeValuesUnprepared(sql, params) {
|
|
212
|
+
return run(sql, params, "array")
|
|
213
|
+
},
|
|
182
214
|
executeUnprepared(sql, params, transformRows) {
|
|
183
215
|
return this.execute(sql, params, transformRows)
|
|
184
216
|
},
|
|
@@ -202,29 +234,31 @@ export const makeMemory = (
|
|
|
202
234
|
transformRows
|
|
203
235
|
? Stream.mapArray((chunk) => transformRows(chunk) as any)
|
|
204
236
|
: identity,
|
|
205
|
-
Stream.mapError((cause) =>
|
|
237
|
+
Stream.mapError((cause) =>
|
|
238
|
+
new SqlError({ reason: classifyError(cause, "Failed to execute statement", "stream") })
|
|
239
|
+
)
|
|
206
240
|
)
|
|
207
241
|
},
|
|
208
242
|
export: Effect.try({
|
|
209
243
|
try: () => sqlite3.serialize(db, "main"),
|
|
210
|
-
catch: (cause) => new SqlError({ cause,
|
|
244
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to export database", "export") })
|
|
211
245
|
}),
|
|
212
246
|
import(data) {
|
|
213
247
|
return Effect.try({
|
|
214
248
|
try: () => sqlite3.deserialize(db, "main", data, data.length, data.length, 1 | 2),
|
|
215
|
-
catch: (cause) => new SqlError({ cause,
|
|
249
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to import database", "import") })
|
|
216
250
|
})
|
|
217
251
|
}
|
|
218
252
|
})
|
|
219
253
|
})
|
|
220
254
|
|
|
221
|
-
const semaphore = yield*
|
|
255
|
+
const semaphore = yield* Semaphore.make(1)
|
|
222
256
|
const connection = yield* makeConnection
|
|
223
257
|
|
|
224
258
|
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
|
225
259
|
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
|
226
260
|
const fiber = Fiber.getCurrent()!
|
|
227
|
-
const scope =
|
|
261
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
|
228
262
|
return Effect.as(
|
|
229
263
|
Effect.tap(
|
|
230
264
|
restore(semaphore.take(1)),
|
|
@@ -257,8 +291,10 @@ export const makeMemory = (
|
|
|
257
291
|
})
|
|
258
292
|
|
|
259
293
|
/**
|
|
260
|
-
*
|
|
261
|
-
*
|
|
294
|
+
* 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.
|
|
295
|
+
*
|
|
296
|
+
* @category constructors
|
|
297
|
+
* @since 4.0.0
|
|
262
298
|
*/
|
|
263
299
|
export const make = (
|
|
264
300
|
options: SqliteClientConfig
|
|
@@ -296,7 +332,11 @@ export const make = (
|
|
|
296
332
|
if (!resume) return
|
|
297
333
|
pending.delete(id)
|
|
298
334
|
if (error) {
|
|
299
|
-
resume(
|
|
335
|
+
resume(
|
|
336
|
+
Exit.fail(
|
|
337
|
+
new SqlError({ reason: classifyError(error as string, "Failed to execute statement", "execute") })
|
|
338
|
+
)
|
|
339
|
+
)
|
|
300
340
|
} else {
|
|
301
341
|
resume(Exit.succeed(results))
|
|
302
342
|
}
|
|
@@ -357,6 +397,9 @@ export const make = (
|
|
|
357
397
|
executeValues(sql, params) {
|
|
358
398
|
return run(sql, params, "array")
|
|
359
399
|
},
|
|
400
|
+
executeValuesUnprepared(sql, params) {
|
|
401
|
+
return run(sql, params, "array")
|
|
402
|
+
},
|
|
360
403
|
executeUnprepared(sql, params, transformRows) {
|
|
361
404
|
return this.execute(sql, params, transformRows)
|
|
362
405
|
},
|
|
@@ -378,11 +421,11 @@ export const make = (
|
|
|
378
421
|
|
|
379
422
|
const connectionRef = yield* ScopedRef.fromAcquire(makeConnection)
|
|
380
423
|
|
|
381
|
-
const semaphore = yield*
|
|
424
|
+
const semaphore = yield* Semaphore.make(1)
|
|
382
425
|
const acquirer = semaphore.withPermits(1)(ScopedRef.get(connectionRef))
|
|
383
426
|
const transactionAcquirer = Effect.uninterruptibleMask(Effect.fnUntraced(function*(restore) {
|
|
384
427
|
const fiber = Fiber.getCurrent()!
|
|
385
|
-
const scope =
|
|
428
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
|
386
429
|
yield* restore(semaphore.take(1))
|
|
387
430
|
yield* Scope.addFinalizer(scope, semaphore.release(1))
|
|
388
431
|
return yield* ScopedRef.get(connectionRef)
|
|
@@ -421,81 +464,93 @@ const extractObject = (rows: [Array<string>, Array<any>]) => rows[1].map((row) =
|
|
|
421
464
|
const extractRows = (rows: [Array<string>, Array<any>]) => rows[1]
|
|
422
465
|
|
|
423
466
|
/**
|
|
424
|
-
*
|
|
425
|
-
*
|
|
467
|
+
* Fiber reference that stores transferables to include with worker-backed SQLite WASM query messages.
|
|
468
|
+
*
|
|
469
|
+
* @category transferables
|
|
470
|
+
* @since 4.0.0
|
|
426
471
|
*/
|
|
427
|
-
export const Transferables =
|
|
472
|
+
export const Transferables = Context.Reference<ReadonlyArray<Transferable>>(
|
|
428
473
|
"@effect/sql-sqlite-wasm/currentTransferables",
|
|
429
474
|
{ defaultValue: () => [] }
|
|
430
475
|
)
|
|
431
476
|
|
|
432
477
|
/**
|
|
433
|
-
*
|
|
434
|
-
*
|
|
478
|
+
* Runs an effect with the supplied transferables attached to worker-backed SQLite WASM query messages.
|
|
479
|
+
*
|
|
480
|
+
* @category transferables
|
|
481
|
+
* @since 4.0.0
|
|
435
482
|
*/
|
|
436
483
|
export const withTransferables =
|
|
437
484
|
(transferables: ReadonlyArray<Transferable>) => <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
|
|
438
485
|
Effect.provideService(effect, Transferables, transferables)
|
|
439
486
|
|
|
440
487
|
/**
|
|
488
|
+
* Builds a layer from an Effect `Config` value, providing both the in-memory SQLite WASM `SqliteClient` service and the generic `SqlClient` service.
|
|
489
|
+
*
|
|
441
490
|
* @category layers
|
|
442
|
-
* @since
|
|
491
|
+
* @since 4.0.0
|
|
443
492
|
*/
|
|
444
493
|
export const layerMemoryConfig = (
|
|
445
494
|
config: Config.Wrap<SqliteClientMemoryConfig>
|
|
446
495
|
): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
447
|
-
Layer.
|
|
448
|
-
Config.unwrap(config).
|
|
496
|
+
Layer.effectContext(
|
|
497
|
+
Config.unwrap(config).pipe(
|
|
449
498
|
Effect.flatMap(makeMemory),
|
|
450
499
|
Effect.map((client) =>
|
|
451
|
-
|
|
452
|
-
|
|
500
|
+
Context.make(SqliteClient, client).pipe(
|
|
501
|
+
Context.add(Client.SqlClient, client)
|
|
453
502
|
)
|
|
454
503
|
)
|
|
455
504
|
)
|
|
456
505
|
).pipe(Layer.provide(Reactivity.layer))
|
|
457
506
|
|
|
458
507
|
/**
|
|
508
|
+
* Builds a layer from an in-memory SQLite WASM client configuration, providing both `SqliteClient` and the generic `SqlClient` service.
|
|
509
|
+
*
|
|
459
510
|
* @category layers
|
|
460
|
-
* @since
|
|
511
|
+
* @since 4.0.0
|
|
461
512
|
*/
|
|
462
513
|
export const layerMemory = (
|
|
463
514
|
config: SqliteClientMemoryConfig
|
|
464
515
|
): Layer.Layer<SqliteClient | Client.SqlClient, SqlError> =>
|
|
465
|
-
Layer.
|
|
516
|
+
Layer.effectContext(
|
|
466
517
|
Effect.map(makeMemory(config), (client) =>
|
|
467
|
-
|
|
468
|
-
|
|
518
|
+
Context.make(SqliteClient, client).pipe(
|
|
519
|
+
Context.add(Client.SqlClient, client)
|
|
469
520
|
))
|
|
470
521
|
).pipe(Layer.provide(Reactivity.layer))
|
|
471
522
|
|
|
472
523
|
/**
|
|
524
|
+
* Builds a layer from a worker-backed SQLite WASM client configuration, providing both `SqliteClient` and the generic `SqlClient` service.
|
|
525
|
+
*
|
|
473
526
|
* @category layers
|
|
474
|
-
* @since
|
|
527
|
+
* @since 4.0.0
|
|
475
528
|
*/
|
|
476
529
|
export const layer = (
|
|
477
530
|
config: SqliteClientConfig
|
|
478
531
|
): Layer.Layer<SqliteClient | Client.SqlClient, SqlError> =>
|
|
479
|
-
Layer.
|
|
532
|
+
Layer.effectContext(
|
|
480
533
|
Effect.map(make(config), (client) =>
|
|
481
|
-
|
|
482
|
-
|
|
534
|
+
Context.make(SqliteClient, client).pipe(
|
|
535
|
+
Context.add(Client.SqlClient, client)
|
|
483
536
|
))
|
|
484
537
|
).pipe(Layer.provide(Reactivity.layer))
|
|
485
538
|
|
|
486
539
|
/**
|
|
540
|
+
* Builds a layer from an Effect `Config` value, providing both the worker-backed SQLite WASM `SqliteClient` service and the generic `SqlClient` service.
|
|
541
|
+
*
|
|
487
542
|
* @category layers
|
|
488
|
-
* @since
|
|
543
|
+
* @since 4.0.0
|
|
489
544
|
*/
|
|
490
545
|
export const layerConfig = (
|
|
491
546
|
config: Config.Wrap<SqliteClientConfig>
|
|
492
547
|
): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError | SqlError> =>
|
|
493
|
-
Layer.
|
|
494
|
-
Config.unwrap(config).
|
|
548
|
+
Layer.effectContext(
|
|
549
|
+
Config.unwrap(config).pipe(
|
|
495
550
|
Effect.flatMap(make),
|
|
496
551
|
Effect.map((client) =>
|
|
497
|
-
|
|
498
|
-
|
|
552
|
+
Context.make(SqliteClient, client).pipe(
|
|
553
|
+
Context.add(Client.SqlClient, client)
|
|
499
554
|
)
|
|
500
555
|
)
|
|
501
556
|
)
|
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"
|