@effect/sql-sqlite-do 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/SqliteClient.d.ts +62 -14
- package/dist/SqliteClient.d.ts.map +1 -1
- package/dist/SqliteClient.js +29 -17
- package/dist/SqliteClient.js.map +1 -1
- package/dist/SqliteMigrator.d.ts +31 -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 +4 -4
- package/src/SqliteClient.ts +76 -25
- package/src/SqliteMigrator.ts +31 -6
- package/src/index.ts +3 -3
package/dist/SqliteClient.d.ts
CHANGED
|
@@ -1,27 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Effect SQL client for Cloudflare Durable Object SQLite storage.
|
|
3
|
+
*
|
|
4
|
+
* This module adapts a Durable Object `SqlStorage` handle into both the
|
|
5
|
+
* Durable Object-specific {@link SqliteClient} service and the generic Effect
|
|
6
|
+
* SQL `SqlClient` service. Use it inside a Durable Object for per-object
|
|
7
|
+
* queries, repositories, migrations, transactional read/write workflows, and
|
|
8
|
+
* tests that exercise Cloudflare's SQLite-backed storage API.
|
|
9
|
+
*
|
|
10
|
+
* **Mental model**
|
|
11
|
+
*
|
|
12
|
+
* Durable Object storage is scoped to one object id. Each object instance has
|
|
13
|
+
* its own database, and {@link make} wraps the same `SqlStorage` handle that
|
|
14
|
+
* the object uses for normal reads and writes. Effect SQL access is serialized
|
|
15
|
+
* through one connection; a transaction keeps that permit for the lifetime of
|
|
16
|
+
* its scope.
|
|
17
|
+
*
|
|
18
|
+
* **Common tasks**
|
|
19
|
+
*
|
|
20
|
+
* Use {@link layer} when the Durable Object constructor already has a concrete
|
|
21
|
+
* `SqlStorage` handle. Use {@link layerConfig} when the handle and transform
|
|
22
|
+
* options should be supplied through Effect `Config`. Keep using generic
|
|
23
|
+
* `SqlClient` APIs for ordinary SQL, and depend on {@link SqliteClient} when
|
|
24
|
+
* code needs the Durable Object-specific service identity.
|
|
25
|
+
*
|
|
26
|
+
* **Gotchas**
|
|
27
|
+
*
|
|
28
|
+
* Keep transactions short and avoid suspending them across unrelated work,
|
|
29
|
+
* because all access through one client waits on the same serialized
|
|
30
|
+
* connection. `SqlStorage.exec` returns SQLite blob values as `ArrayBuffer`,
|
|
31
|
+
* which this client normalizes to `Uint8Array`. SQLite does not support
|
|
32
|
+
* `updateValues`.
|
|
33
|
+
*
|
|
34
|
+
* @since 4.0.0
|
|
3
35
|
*/
|
|
4
36
|
import type { SqlStorage } from "@cloudflare/workers-types";
|
|
5
37
|
import * as Config from "effect/Config";
|
|
38
|
+
import * as Context from "effect/Context";
|
|
6
39
|
import * as Effect from "effect/Effect";
|
|
7
40
|
import * as Layer from "effect/Layer";
|
|
8
41
|
import * as Scope from "effect/Scope";
|
|
9
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
10
42
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
11
43
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
12
44
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
45
|
+
* Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
46
|
+
*
|
|
47
|
+
* @category type IDs
|
|
48
|
+
* @since 4.0.0
|
|
15
49
|
*/
|
|
16
50
|
export declare const TypeId: TypeId;
|
|
17
51
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
52
|
+
* Type-level identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
53
|
+
*
|
|
54
|
+
* @category type IDs
|
|
55
|
+
* @since 4.0.0
|
|
20
56
|
*/
|
|
21
57
|
export type TypeId = "~@effect/sql-sqlite-do/SqliteClient";
|
|
22
58
|
/**
|
|
59
|
+
* Cloudflare Durable Object SQLite client service, extending `SqlClient` with its configuration. `updateValues` is not supported.
|
|
60
|
+
*
|
|
23
61
|
* @category models
|
|
24
|
-
* @since
|
|
62
|
+
* @since 4.0.0
|
|
25
63
|
*/
|
|
26
64
|
export interface SqliteClient extends Client.SqlClient {
|
|
27
65
|
readonly [TypeId]: TypeId;
|
|
@@ -30,13 +68,17 @@ export interface SqliteClient extends Client.SqlClient {
|
|
|
30
68
|
readonly updateValues: never;
|
|
31
69
|
}
|
|
32
70
|
/**
|
|
71
|
+
* Context tag used to access the Cloudflare Durable Object `SqliteClient` service.
|
|
72
|
+
*
|
|
33
73
|
* @category tags
|
|
34
|
-
* @since
|
|
74
|
+
* @since 4.0.0
|
|
35
75
|
*/
|
|
36
|
-
export declare const SqliteClient:
|
|
76
|
+
export declare const SqliteClient: Context.Service<SqliteClient, SqliteClient>;
|
|
37
77
|
/**
|
|
78
|
+
* Configuration for a Cloudflare Durable Object SQLite client, including the `SqlStorage` handle, span attributes, and query/result name transforms.
|
|
79
|
+
*
|
|
38
80
|
* @category models
|
|
39
|
-
* @since
|
|
81
|
+
* @since 4.0.0
|
|
40
82
|
*/
|
|
41
83
|
export interface SqliteClientConfig {
|
|
42
84
|
readonly db: SqlStorage;
|
|
@@ -45,18 +87,24 @@ export interface SqliteClientConfig {
|
|
|
45
87
|
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
46
88
|
}
|
|
47
89
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
90
|
+
* Creates a scoped Cloudflare Durable Object SQLite client around `SqlStorage`, serializing access and converting returned `ArrayBuffer` values to `Uint8Array`.
|
|
91
|
+
*
|
|
92
|
+
* @category constructors
|
|
93
|
+
* @since 4.0.0
|
|
50
94
|
*/
|
|
51
95
|
export declare const make: (options: SqliteClientConfig) => Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity>;
|
|
52
96
|
/**
|
|
97
|
+
* Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
98
|
+
*
|
|
53
99
|
* @category layers
|
|
54
|
-
* @since
|
|
100
|
+
* @since 4.0.0
|
|
55
101
|
*/
|
|
56
102
|
export declare const layerConfig: (config: Config.Wrap<SqliteClientConfig>) => Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError>;
|
|
57
103
|
/**
|
|
104
|
+
* Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
105
|
+
*
|
|
58
106
|
* @category layers
|
|
59
|
-
* @since
|
|
107
|
+
* @since 4.0.0
|
|
60
108
|
*/
|
|
61
109
|
export declare const layer: (config: SqliteClientConfig) => Layer.Layer<SqliteClient | Client.SqlClient>;
|
|
62
110
|
//# sourceMappingURL=SqliteClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SqliteClient.d.ts","sourceRoot":"","sources":["../src/SqliteClient.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"SqliteClient.d.ts","sourceRoot":"","sources":["../src/SqliteClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AAC3D,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAGvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAGrC,OAAO,KAAK,UAAU,MAAM,uCAAuC,CAAA;AACnE,OAAO,KAAK,MAAM,MAAM,+BAA+B,CAAA;AAUvD;;;;;GAKG;AACH,eAAO,MAAM,MAAM,EAAE,MAA8C,CAAA;AAEnE;;;;;GAKG;AACH,MAAM,MAAM,MAAM,GAAG,qCAAqC,CAAA;AAE1D;;;;;GAKG;AACH,MAAM,WAAW,YAA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
import * as Context from "effect/Context";
|
|
2
3
|
import * as Effect from "effect/Effect";
|
|
3
4
|
import * as Fiber from "effect/Fiber";
|
|
4
5
|
import { identity } from "effect/Function";
|
|
5
6
|
import * as Layer from "effect/Layer";
|
|
6
7
|
import * as Scope from "effect/Scope";
|
|
7
8
|
import * as Semaphore from "effect/Semaphore";
|
|
8
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
9
9
|
import * as Stream from "effect/Stream";
|
|
10
10
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
11
11
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
12
|
-
import { SqlError } from "effect/unstable/sql/SqlError";
|
|
12
|
+
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError";
|
|
13
13
|
import * as Statement from "effect/unstable/sql/Statement";
|
|
14
14
|
const ATTR_DB_SYSTEM_NAME = "db.system.name";
|
|
15
|
+
const classifyError = (cause, message, operation) => classifySqliteError(cause, {
|
|
16
|
+
message,
|
|
17
|
+
operation
|
|
18
|
+
});
|
|
15
19
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
20
|
+
* Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
21
|
+
*
|
|
22
|
+
* @category type IDs
|
|
23
|
+
* @since 4.0.0
|
|
18
24
|
*/
|
|
19
25
|
export const TypeId = "~@effect/sql-sqlite-do/SqliteClient";
|
|
20
26
|
/**
|
|
27
|
+
* Context tag used to access the Cloudflare Durable Object `SqliteClient` service.
|
|
28
|
+
*
|
|
21
29
|
* @category tags
|
|
22
|
-
* @since
|
|
30
|
+
* @since 4.0.0
|
|
23
31
|
*/
|
|
24
|
-
export const SqliteClient = /*#__PURE__*/
|
|
32
|
+
export const SqliteClient = /*#__PURE__*/Context.Service("@effect/sql-sqlite-do/SqliteClient");
|
|
25
33
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
34
|
+
* Creates a scoped Cloudflare Durable Object SQLite client around `SqlStorage`, serializing access and converting returned `ArrayBuffer` values to `Uint8Array`.
|
|
35
|
+
*
|
|
36
|
+
* @category constructors
|
|
37
|
+
* @since 4.0.0
|
|
28
38
|
*/
|
|
29
39
|
export const make = options => Effect.gen(function* () {
|
|
30
40
|
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames);
|
|
@@ -46,8 +56,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
46
56
|
const runStatement = (sql, params = []) => Effect.try({
|
|
47
57
|
try: () => Array.from(runIterator(sql, params)),
|
|
48
58
|
catch: cause => new SqlError({
|
|
49
|
-
cause,
|
|
50
|
-
message: `Failed to execute statement`
|
|
59
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
51
60
|
})
|
|
52
61
|
});
|
|
53
62
|
const runValues = (sql, params = []) => Effect.try({
|
|
@@ -61,8 +70,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
61
70
|
return row;
|
|
62
71
|
}),
|
|
63
72
|
catch: cause => new SqlError({
|
|
64
|
-
cause,
|
|
65
|
-
message: `Failed to execute statement`
|
|
73
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
66
74
|
})
|
|
67
75
|
});
|
|
68
76
|
return identity({
|
|
@@ -91,7 +99,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
91
99
|
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
|
|
92
100
|
const transactionAcquirer = Effect.uninterruptibleMask(restore => {
|
|
93
101
|
const fiber = Fiber.getCurrent();
|
|
94
|
-
const scope =
|
|
102
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope);
|
|
95
103
|
return Effect.as(Effect.tap(restore(semaphore.take(1)), () => Scope.addFinalizer(scope, semaphore.release(1))), connection);
|
|
96
104
|
});
|
|
97
105
|
return Object.assign(yield* Client.make({
|
|
@@ -106,13 +114,17 @@ export const make = options => Effect.gen(function* () {
|
|
|
106
114
|
});
|
|
107
115
|
});
|
|
108
116
|
/**
|
|
117
|
+
* Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
118
|
+
*
|
|
109
119
|
* @category layers
|
|
110
|
-
* @since
|
|
120
|
+
* @since 4.0.0
|
|
111
121
|
*/
|
|
112
|
-
export const layerConfig = config => Layer.
|
|
122
|
+
export const layerConfig = config => Layer.effectContext(Config.unwrap(config).pipe(Effect.flatMap(make), Effect.map(client => Context.make(SqliteClient, client).pipe(Context.add(Client.SqlClient, client))))).pipe(Layer.provide(Reactivity.layer));
|
|
113
123
|
/**
|
|
124
|
+
* Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
125
|
+
*
|
|
114
126
|
* @category layers
|
|
115
|
-
* @since
|
|
127
|
+
* @since 4.0.0
|
|
116
128
|
*/
|
|
117
|
-
export const layer = config => Layer.
|
|
129
|
+
export const layer = config => Layer.effectContext(Effect.map(make(config), client => Context.make(SqliteClient, client).pipe(Context.add(Client.SqlClient, client)))).pipe(Layer.provide(Reactivity.layer));
|
|
118
130
|
//# sourceMappingURL=SqliteClient.js.map
|
package/dist/SqliteClient.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SqliteClient.js","names":["Config","Effect","Fiber","identity","Layer","Scope","Semaphore","
|
|
1
|
+
{"version":3,"file":"SqliteClient.js","names":["Config","Context","Effect","Fiber","identity","Layer","Scope","Semaphore","Stream","Reactivity","Client","classifySqliteError","SqlError","Statement","ATTR_DB_SYSTEM_NAME","classifyError","cause","message","operation","TypeId","SqliteClient","Service","make","options","gen","compiler","makeCompilerSqlite","transformQueryNames","transformRows","transformResultNames","defaultTransforms","array","undefined","makeConnection","db","runIterator","sql","params","cursor","exec","columns","columnNames","result","raw","obj","i","length","value","ArrayBuffer","Uint8Array","runStatement","try","Array","from","catch","reason","runValues","row","execute","map","executeRaw","executeValues","executeUnprepared","executeStream","suspend","iterator","fromIteratorSucceed","pipe","mapArray","chunk","semaphore","connection","acquirer","withPermits","succeed","transactionAcquirer","uninterruptibleMask","restore","fiber","getCurrent","scope","getUnsafe","context","as","tap","take","addFinalizer","release","Object","assign","spanAttributes","entries","config","layerConfig","effectContext","unwrap","flatMap","client","add","SqlClient","provide","layer"],"sources":["../src/SqliteClient.ts"],"sourcesContent":[null],"mappings":"AAoCA,OAAO,KAAKA,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,SAASC,QAAQ,QAAQ,iBAAiB;AAC1C,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,SAAS,MAAM,kBAAkB;AAC7C,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,UAAU,MAAM,uCAAuC;AACnE,OAAO,KAAKC,MAAM,MAAM,+BAA+B;AAEvD,SAASC,mBAAmB,EAAEC,QAAQ,QAAQ,8BAA8B;AAC5E,OAAO,KAAKC,SAAS,MAAM,+BAA+B;AAE1D,MAAMC,mBAAmB,GAAG,gBAAgB;AAE5C,MAAMC,aAAa,GAAGA,CAACC,KAAc,EAAEC,OAAe,EAAEC,SAAiB,KACvEP,mBAAmB,CAACK,KAAK,EAAE;EAAEC,OAAO;EAAEC;AAAS,CAAE,CAAC;AAEpD;;;;;;AAMA,OAAO,MAAMC,MAAM,GAAW,qCAAqC;AAwBnE;;;;;;AAMA,OAAO,MAAMC,YAAY,gBAAGnB,OAAO,CAACoB,OAAO,CAAe,oCAAoC,CAAC;AAgB/F;;;;;;AAMA,OAAO,MAAMC,IAAI,GACfC,OAA2B,IAE3BrB,MAAM,CAACsB,GAAG,CAAC,aAAS;EAClB,MAAMC,QAAQ,GAAGZ,SAAS,CAACa,kBAAkB,CAACH,OAAO,CAACI,mBAAmB,CAAC;EAC1E,MAAMC,aAAa,GAAGL,OAAO,CAACM,oBAAoB,GAC9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,CAAC,CAAC","ignoreList":[]}
|
package/dist/SqliteMigrator.d.ts
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Utilities for applying Effect SQL migrations to Cloudflare Durable Object SQLite storage.
|
|
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 Durable Object `SqlStorage`-backed `SqlClient`. Use it when a Durable
|
|
7
|
+
* Object needs to create or upgrade its local schema during construction, before
|
|
8
|
+
* repositories or request handlers use the object storage, or in tests that
|
|
9
|
+
* exercise Durable Object persistence.
|
|
10
|
+
*
|
|
11
|
+
* Migrations are recorded in `effect_sql_migrations` by default and are loaded
|
|
12
|
+
* using the shared `<id>_<name>` file or record-key convention. The underlying
|
|
13
|
+
* storage is scoped to a Durable Object id, so running migrations for one object
|
|
14
|
+
* does not update any other object instance; run the migrator against the same
|
|
15
|
+
* `SqlStorage` handle that the object uses for normal queries. These SQL
|
|
16
|
+
* migrations are separate from Cloudflare's Durable Object class migrations, and
|
|
17
|
+
* the Durable Object must already be configured with SQLite storage before this
|
|
18
|
+
* module can apply schema changes. Repeated startup runs are expected and are
|
|
19
|
+
* guarded by the migrations table, but request handling should wait until the
|
|
20
|
+
* migration layer has finished. This adapter does not currently write SQLite
|
|
21
|
+
* schema dumps for `schemaDirectory`.
|
|
22
|
+
*
|
|
23
|
+
* @since 4.0.0
|
|
3
24
|
*/
|
|
4
25
|
import type * as Effect from "effect/Effect";
|
|
5
26
|
import * as Layer from "effect/Layer";
|
|
@@ -7,17 +28,21 @@ import * as Migrator from "effect/unstable/sql/Migrator";
|
|
|
7
28
|
import type * as Client from "effect/unstable/sql/SqlClient";
|
|
8
29
|
import type { SqlError } from "effect/unstable/sql/SqlError";
|
|
9
30
|
/**
|
|
10
|
-
* @since
|
|
31
|
+
* @since 4.0.0
|
|
11
32
|
*/
|
|
12
33
|
export * from "effect/unstable/sql/Migrator";
|
|
13
34
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
35
|
+
* Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
|
|
36
|
+
*
|
|
37
|
+
* @category constructors
|
|
38
|
+
* @since 4.0.0
|
|
16
39
|
*/
|
|
17
40
|
export declare const run: <R2 = never>({ loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>) => Effect.Effect<ReadonlyArray<readonly [id: number, name: string]>, Migrator.MigrationError | SqlError, Client.SqlClient | R2>;
|
|
18
41
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
42
|
+
* Creates a layer that runs the configured SQL migrations during layer construction.
|
|
43
|
+
*
|
|
44
|
+
* @category constructors
|
|
45
|
+
* @since 4.0.0
|
|
21
46
|
*/
|
|
22
47
|
export declare const layer: <R>(options: Migrator.MigratorOptions<R>) => Layer.Layer<never, Migrator.MigrationError | SqlError, Client.SqlClient | R>;
|
|
23
48
|
//# 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;;;;;;;;;;;;;;;;;;;;;;;GAuBG;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,EAAE,GAAG,KAAK,EAC3B,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,EAAE,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,KAC7D,MAAM,CAAC,MAAM,CAChB,aAAa,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,EAClD,QAAQ,CAAC,cAAc,GAAG,QAAQ,EAClC,MAAM,CAAC,SAAS,GAAG,EAAE,CACF,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,CAAC,cAAc,GAAG,QAAQ,EAAE,MAAM,CAAC,SAAS,GAAG,CAAC,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 using the configured `SqlClient`, returning the migrations that were applied.
|
|
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 SQL migrations during layer construction.
|
|
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":"AAyBA,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,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
* @since
|
|
5
|
+
* @since 4.0.0
|
|
6
6
|
*/
|
|
7
7
|
export * as SqliteClient from "./SqliteClient.ts";
|
|
8
8
|
/**
|
|
9
|
-
* @since
|
|
9
|
+
* @since 4.0.0
|
|
10
10
|
*/
|
|
11
11
|
export * as SqliteMigrator from "./SqliteMigrator.ts";
|
|
12
12
|
//# 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
|
// @barrel: Auto-generated exports. Do not edit manually.
|
|
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-do",
|
|
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",
|
|
@@ -43,11 +43,11 @@
|
|
|
43
43
|
"provenance": true
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@cloudflare/workers-types": "^4.
|
|
47
|
-
"effect": "^4.0.0-beta.
|
|
46
|
+
"@cloudflare/workers-types": "^4.20260511.1",
|
|
47
|
+
"effect": "^4.0.0-beta.71"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"effect": "^4.0.0-beta.
|
|
50
|
+
"effect": "^4.0.0-beta.71"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"codegen": "effect-utils codegen",
|
package/src/SqliteClient.ts
CHANGED
|
@@ -1,39 +1,80 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Effect SQL client for Cloudflare Durable Object SQLite storage.
|
|
3
|
+
*
|
|
4
|
+
* This module adapts a Durable Object `SqlStorage` handle into both the
|
|
5
|
+
* Durable Object-specific {@link SqliteClient} service and the generic Effect
|
|
6
|
+
* SQL `SqlClient` service. Use it inside a Durable Object for per-object
|
|
7
|
+
* queries, repositories, migrations, transactional read/write workflows, and
|
|
8
|
+
* tests that exercise Cloudflare's SQLite-backed storage API.
|
|
9
|
+
*
|
|
10
|
+
* **Mental model**
|
|
11
|
+
*
|
|
12
|
+
* Durable Object storage is scoped to one object id. Each object instance has
|
|
13
|
+
* its own database, and {@link make} wraps the same `SqlStorage` handle that
|
|
14
|
+
* the object uses for normal reads and writes. Effect SQL access is serialized
|
|
15
|
+
* through one connection; a transaction keeps that permit for the lifetime of
|
|
16
|
+
* its scope.
|
|
17
|
+
*
|
|
18
|
+
* **Common tasks**
|
|
19
|
+
*
|
|
20
|
+
* Use {@link layer} when the Durable Object constructor already has a concrete
|
|
21
|
+
* `SqlStorage` handle. Use {@link layerConfig} when the handle and transform
|
|
22
|
+
* options should be supplied through Effect `Config`. Keep using generic
|
|
23
|
+
* `SqlClient` APIs for ordinary SQL, and depend on {@link SqliteClient} when
|
|
24
|
+
* code needs the Durable Object-specific service identity.
|
|
25
|
+
*
|
|
26
|
+
* **Gotchas**
|
|
27
|
+
*
|
|
28
|
+
* Keep transactions short and avoid suspending them across unrelated work,
|
|
29
|
+
* because all access through one client waits on the same serialized
|
|
30
|
+
* connection. `SqlStorage.exec` returns SQLite blob values as `ArrayBuffer`,
|
|
31
|
+
* which this client normalizes to `Uint8Array`. SQLite does not support
|
|
32
|
+
* `updateValues`.
|
|
33
|
+
*
|
|
34
|
+
* @since 4.0.0
|
|
3
35
|
*/
|
|
4
36
|
import type { SqlStorage } from "@cloudflare/workers-types"
|
|
5
37
|
import * as Config from "effect/Config"
|
|
38
|
+
import * as Context from "effect/Context"
|
|
6
39
|
import * as Effect from "effect/Effect"
|
|
7
40
|
import * as Fiber from "effect/Fiber"
|
|
8
41
|
import { identity } from "effect/Function"
|
|
9
42
|
import * as Layer from "effect/Layer"
|
|
10
43
|
import * as Scope from "effect/Scope"
|
|
11
44
|
import * as Semaphore from "effect/Semaphore"
|
|
12
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
13
45
|
import * as Stream from "effect/Stream"
|
|
14
46
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
15
47
|
import * as Client from "effect/unstable/sql/SqlClient"
|
|
16
48
|
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
17
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
49
|
+
import { classifySqliteError, SqlError } from "effect/unstable/sql/SqlError"
|
|
18
50
|
import * as Statement from "effect/unstable/sql/Statement"
|
|
19
51
|
|
|
20
52
|
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
21
53
|
|
|
54
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
55
|
+
classifySqliteError(cause, { message, operation })
|
|
56
|
+
|
|
22
57
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
58
|
+
* Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
59
|
+
*
|
|
60
|
+
* @category type IDs
|
|
61
|
+
* @since 4.0.0
|
|
25
62
|
*/
|
|
26
63
|
export const TypeId: TypeId = "~@effect/sql-sqlite-do/SqliteClient"
|
|
27
64
|
|
|
28
65
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
66
|
+
* Type-level identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
67
|
+
*
|
|
68
|
+
* @category type IDs
|
|
69
|
+
* @since 4.0.0
|
|
31
70
|
*/
|
|
32
71
|
export type TypeId = "~@effect/sql-sqlite-do/SqliteClient"
|
|
33
72
|
|
|
34
73
|
/**
|
|
74
|
+
* Cloudflare Durable Object SQLite client service, extending `SqlClient` with its configuration. `updateValues` is not supported.
|
|
75
|
+
*
|
|
35
76
|
* @category models
|
|
36
|
-
* @since
|
|
77
|
+
* @since 4.0.0
|
|
37
78
|
*/
|
|
38
79
|
export interface SqliteClient extends Client.SqlClient {
|
|
39
80
|
readonly [TypeId]: TypeId
|
|
@@ -44,14 +85,18 @@ export interface SqliteClient extends Client.SqlClient {
|
|
|
44
85
|
}
|
|
45
86
|
|
|
46
87
|
/**
|
|
88
|
+
* Context tag used to access the Cloudflare Durable Object `SqliteClient` service.
|
|
89
|
+
*
|
|
47
90
|
* @category tags
|
|
48
|
-
* @since
|
|
91
|
+
* @since 4.0.0
|
|
49
92
|
*/
|
|
50
|
-
export const SqliteClient =
|
|
93
|
+
export const SqliteClient = Context.Service<SqliteClient>("@effect/sql-sqlite-do/SqliteClient")
|
|
51
94
|
|
|
52
95
|
/**
|
|
96
|
+
* Configuration for a Cloudflare Durable Object SQLite client, including the `SqlStorage` handle, span attributes, and query/result name transforms.
|
|
97
|
+
*
|
|
53
98
|
* @category models
|
|
54
|
-
* @since
|
|
99
|
+
* @since 4.0.0
|
|
55
100
|
*/
|
|
56
101
|
export interface SqliteClientConfig {
|
|
57
102
|
readonly db: SqlStorage
|
|
@@ -62,8 +107,10 @@ export interface SqliteClientConfig {
|
|
|
62
107
|
}
|
|
63
108
|
|
|
64
109
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
110
|
+
* Creates a scoped Cloudflare Durable Object SQLite client around `SqlStorage`, serializing access and converting returned `ArrayBuffer` values to `Uint8Array`.
|
|
111
|
+
*
|
|
112
|
+
* @category constructors
|
|
113
|
+
* @since 4.0.0
|
|
67
114
|
*/
|
|
68
115
|
export const make = (
|
|
69
116
|
options: SqliteClientConfig
|
|
@@ -99,7 +146,7 @@ export const make = (
|
|
|
99
146
|
): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
|
|
100
147
|
Effect.try({
|
|
101
148
|
try: () => Array.from(runIterator(sql, params)),
|
|
102
|
-
catch: (cause) => new SqlError({ cause,
|
|
149
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
103
150
|
})
|
|
104
151
|
|
|
105
152
|
const runValues = (
|
|
@@ -117,7 +164,7 @@ export const make = (
|
|
|
117
164
|
}
|
|
118
165
|
return row
|
|
119
166
|
}),
|
|
120
|
-
catch: (cause) => new SqlError({ cause,
|
|
167
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
121
168
|
})
|
|
122
169
|
|
|
123
170
|
return identity<Connection>({
|
|
@@ -156,7 +203,7 @@ export const make = (
|
|
|
156
203
|
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
|
157
204
|
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
|
158
205
|
const fiber = Fiber.getCurrent()!
|
|
159
|
-
const scope =
|
|
206
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
|
160
207
|
return Effect.as(
|
|
161
208
|
Effect.tap(
|
|
162
209
|
restore(semaphore.take(1)),
|
|
@@ -185,33 +232,37 @@ export const make = (
|
|
|
185
232
|
})
|
|
186
233
|
|
|
187
234
|
/**
|
|
235
|
+
* Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
236
|
+
*
|
|
188
237
|
* @category layers
|
|
189
|
-
* @since
|
|
238
|
+
* @since 4.0.0
|
|
190
239
|
*/
|
|
191
240
|
export const layerConfig = (
|
|
192
241
|
config: Config.Wrap<SqliteClientConfig>
|
|
193
242
|
): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError> =>
|
|
194
|
-
Layer.
|
|
195
|
-
Config.unwrap(config).
|
|
243
|
+
Layer.effectContext(
|
|
244
|
+
Config.unwrap(config).pipe(
|
|
196
245
|
Effect.flatMap(make),
|
|
197
246
|
Effect.map((client) =>
|
|
198
|
-
|
|
199
|
-
|
|
247
|
+
Context.make(SqliteClient, client).pipe(
|
|
248
|
+
Context.add(Client.SqlClient, client)
|
|
200
249
|
)
|
|
201
250
|
)
|
|
202
251
|
)
|
|
203
252
|
).pipe(Layer.provide(Reactivity.layer))
|
|
204
253
|
|
|
205
254
|
/**
|
|
255
|
+
* Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
256
|
+
*
|
|
206
257
|
* @category layers
|
|
207
|
-
* @since
|
|
258
|
+
* @since 4.0.0
|
|
208
259
|
*/
|
|
209
260
|
export const layer = (
|
|
210
261
|
config: SqliteClientConfig
|
|
211
262
|
): Layer.Layer<SqliteClient | Client.SqlClient> =>
|
|
212
|
-
Layer.
|
|
263
|
+
Layer.effectContext(
|
|
213
264
|
Effect.map(make(config), (client) =>
|
|
214
|
-
|
|
215
|
-
|
|
265
|
+
Context.make(SqliteClient, client).pipe(
|
|
266
|
+
Context.add(Client.SqlClient, client)
|
|
216
267
|
))
|
|
217
268
|
).pipe(Layer.provide(Reactivity.layer))
|
package/src/SqliteMigrator.ts
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Utilities for applying Effect SQL migrations to Cloudflare Durable Object SQLite storage.
|
|
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 Durable Object `SqlStorage`-backed `SqlClient`. Use it when a Durable
|
|
7
|
+
* Object needs to create or upgrade its local schema during construction, before
|
|
8
|
+
* repositories or request handlers use the object storage, or in tests that
|
|
9
|
+
* exercise Durable Object persistence.
|
|
10
|
+
*
|
|
11
|
+
* Migrations are recorded in `effect_sql_migrations` by default and are loaded
|
|
12
|
+
* using the shared `<id>_<name>` file or record-key convention. The underlying
|
|
13
|
+
* storage is scoped to a Durable Object id, so running migrations for one object
|
|
14
|
+
* does not update any other object instance; run the migrator against the same
|
|
15
|
+
* `SqlStorage` handle that the object uses for normal queries. These SQL
|
|
16
|
+
* migrations are separate from Cloudflare's Durable Object class migrations, and
|
|
17
|
+
* the Durable Object must already be configured with SQLite storage before this
|
|
18
|
+
* module can apply schema changes. Repeated startup runs are expected and are
|
|
19
|
+
* guarded by the migrations table, but request handling should wait until the
|
|
20
|
+
* migration layer has finished. This adapter does not currently write SQLite
|
|
21
|
+
* schema dumps for `schemaDirectory`.
|
|
22
|
+
*
|
|
23
|
+
* @since 4.0.0
|
|
3
24
|
*/
|
|
4
25
|
import type * as Effect from "effect/Effect"
|
|
5
26
|
import * as Layer from "effect/Layer"
|
|
@@ -8,13 +29,15 @@ import type * as Client from "effect/unstable/sql/SqlClient"
|
|
|
8
29
|
import type { SqlError } from "effect/unstable/sql/SqlError"
|
|
9
30
|
|
|
10
31
|
/**
|
|
11
|
-
* @since
|
|
32
|
+
* @since 4.0.0
|
|
12
33
|
*/
|
|
13
34
|
export * from "effect/unstable/sql/Migrator"
|
|
14
35
|
|
|
15
36
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
37
|
+
* Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
|
|
38
|
+
*
|
|
39
|
+
* @category constructors
|
|
40
|
+
* @since 4.0.0
|
|
18
41
|
*/
|
|
19
42
|
export const run: <R2 = never>(
|
|
20
43
|
{ loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>
|
|
@@ -25,8 +48,10 @@ export const run: <R2 = never>(
|
|
|
25
48
|
> = Migrator.make({})
|
|
26
49
|
|
|
27
50
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
51
|
+
* Creates a layer that runs the configured SQL migrations during layer construction.
|
|
52
|
+
*
|
|
53
|
+
* @category constructors
|
|
54
|
+
* @since 4.0.0
|
|
30
55
|
*/
|
|
31
56
|
export const layer = <R>(
|
|
32
57
|
options: Migrator.MigratorOptions<R>
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @since
|
|
2
|
+
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
// @barrel: Auto-generated exports. Do not edit manually.
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* @since
|
|
8
|
+
* @since 4.0.0
|
|
9
9
|
*/
|
|
10
10
|
export * as SqliteClient from "./SqliteClient.ts"
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @since
|
|
13
|
+
* @since 4.0.0
|
|
14
14
|
*/
|
|
15
15
|
export * as SqliteMigrator from "./SqliteMigrator.ts"
|