@effect/sql-d1 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/D1Client.d.ts +51 -14
- package/dist/D1Client.d.ts.map +1 -1
- package/dist/D1Client.js +30 -18
- package/dist/D1Client.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/package.json +5 -5
- package/src/D1Client.ts +65 -25
- package/src/index.ts +2 -2
package/dist/D1Client.d.ts
CHANGED
|
@@ -1,28 +1,55 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Cloudflare D1 client implementation for Effect SQL, backed by a Workers `D1Database` binding.
|
|
3
|
+
*
|
|
4
|
+
* This module adapts a Cloudflare D1 database binding into both the
|
|
5
|
+
* D1-specific `D1Client` service and the generic Effect `SqlClient` service.
|
|
6
|
+
* Use it in Workers, Pages Functions, and tests that provide a D1 binding to
|
|
7
|
+
* run SQLite-compatible queries through Effect services and layers, including
|
|
8
|
+
* repositories, migrations, request handlers, and local development against
|
|
9
|
+
* Wrangler or Miniflare-backed D1 databases.
|
|
10
|
+
*
|
|
11
|
+
* The client prepares statements with D1, caches them by SQL string, and uses
|
|
12
|
+
* the SQLite statement compiler for query and result name transforms. D1
|
|
13
|
+
* commits individual statements automatically, and native `D1Database.batch`
|
|
14
|
+
* is the D1 API for sequential, transactional multi-statement work; this
|
|
15
|
+
* adapter does not expose Effect SQL transactions because it cannot map
|
|
16
|
+
* `withTransaction` onto a connection-scoped D1 transaction. D1 databases are
|
|
17
|
+
* serverless SQLite storage with platform limits and single-database serialized
|
|
18
|
+
* execution, so keep queries small and indexed, batch large maintenance work
|
|
19
|
+
* at the D1 API level, and use D1 sessions outside this client when an
|
|
20
|
+
* application needs bookmark-based sequential consistency or read replicas.
|
|
21
|
+
* Streaming queries and `updateValues` are not supported.
|
|
22
|
+
*
|
|
23
|
+
* @since 4.0.0
|
|
3
24
|
*/
|
|
4
25
|
import type { D1Database } from "@cloudflare/workers-types";
|
|
5
26
|
import * as Config from "effect/Config";
|
|
27
|
+
import * as Context from "effect/Context";
|
|
6
28
|
import * as Duration from "effect/Duration";
|
|
7
29
|
import * as Effect from "effect/Effect";
|
|
8
30
|
import * as Layer from "effect/Layer";
|
|
9
31
|
import type * as Scope from "effect/Scope";
|
|
10
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
11
32
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
12
33
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
13
34
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
35
|
+
* Unique runtime identifier used to tag `D1Client` values.
|
|
36
|
+
*
|
|
37
|
+
* @category type IDs
|
|
38
|
+
* @since 4.0.0
|
|
16
39
|
*/
|
|
17
40
|
export declare const TypeId: TypeId;
|
|
18
41
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
42
|
+
* Type-level literal for the `D1Client` runtime identifier.
|
|
43
|
+
*
|
|
44
|
+
* @category type IDs
|
|
45
|
+
* @since 4.0.0
|
|
21
46
|
*/
|
|
22
47
|
export type TypeId = "~@effect/sql-d1/D1Client";
|
|
23
48
|
/**
|
|
49
|
+
* Cloudflare D1 SQL client service, extending `SqlClient` with its D1 configuration and no `updateValues` support.
|
|
50
|
+
*
|
|
24
51
|
* @category models
|
|
25
|
-
* @since
|
|
52
|
+
* @since 4.0.0
|
|
26
53
|
*/
|
|
27
54
|
export interface D1Client extends Client.SqlClient {
|
|
28
55
|
readonly [TypeId]: TypeId;
|
|
@@ -31,13 +58,17 @@ export interface D1Client extends Client.SqlClient {
|
|
|
31
58
|
readonly updateValues: never;
|
|
32
59
|
}
|
|
33
60
|
/**
|
|
61
|
+
* Context tag used to access the `D1Client` service.
|
|
62
|
+
*
|
|
34
63
|
* @category tags
|
|
35
|
-
* @since
|
|
64
|
+
* @since 4.0.0
|
|
36
65
|
*/
|
|
37
|
-
export declare const D1Client:
|
|
66
|
+
export declare const D1Client: Context.Service<D1Client, D1Client>;
|
|
38
67
|
/**
|
|
68
|
+
* Configuration for a Cloudflare D1 client, including the `D1Database`, prepared statement cache settings, span attributes, and query/result name transforms.
|
|
69
|
+
*
|
|
39
70
|
* @category models
|
|
40
|
-
* @since
|
|
71
|
+
* @since 4.0.0
|
|
41
72
|
*/
|
|
42
73
|
export interface D1ClientConfig {
|
|
43
74
|
readonly db: D1Database;
|
|
@@ -48,18 +79,24 @@ export interface D1ClientConfig {
|
|
|
48
79
|
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
49
80
|
}
|
|
50
81
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
82
|
+
* Creates a scoped Cloudflare D1 SQL client. Prepared statements are cached, while transactions and streaming queries are not supported by this driver.
|
|
83
|
+
*
|
|
84
|
+
* @category constructors
|
|
85
|
+
* @since 4.0.0
|
|
53
86
|
*/
|
|
54
87
|
export declare const make: (options: D1ClientConfig) => Effect.Effect<D1Client, never, Scope.Scope | Reactivity.Reactivity>;
|
|
55
88
|
/**
|
|
89
|
+
* Creates a layer from a `Config`-wrapped D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
90
|
+
*
|
|
56
91
|
* @category layers
|
|
57
|
-
* @since
|
|
92
|
+
* @since 4.0.0
|
|
58
93
|
*/
|
|
59
94
|
export declare const layerConfig: (config: Config.Wrap<D1ClientConfig>) => Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError>;
|
|
60
95
|
/**
|
|
96
|
+
* Creates a layer from a concrete D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
97
|
+
*
|
|
61
98
|
* @category layers
|
|
62
|
-
* @since
|
|
99
|
+
* @since 4.0.0
|
|
63
100
|
*/
|
|
64
101
|
export declare const layer: (config: D1ClientConfig) => Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError>;
|
|
65
102
|
//# sourceMappingURL=D1Client.d.ts.map
|
package/dist/D1Client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"D1Client.d.ts","sourceRoot":"","sources":["../src/D1Client.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"D1Client.d.ts","sourceRoot":"","sources":["../src/D1Client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,KAAK,EAAE,UAAU,EAAuB,MAAM,2BAA2B,CAAA;AAEhF,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,KAAK,KAAK,MAAM,cAAc,CAAA;AAE1C,OAAO,KAAK,UAAU,MAAM,uCAAuC,CAAA;AACnE,OAAO,KAAK,MAAM,MAAM,+BAA+B,CAAA;AAUvD;;;;;GAKG;AACH,eAAO,MAAM,MAAM,EAAE,MAAmC,CAAA;AAExD;;;;;GAKG;AACH,MAAM,MAAM,MAAM,GAAG,0BAA0B,CAAA;AAE/C;;;;;GAKG;AACH,MAAM,WAAW,QAAS,SAAQ,MAAM,CAAC,SAAS;IAChD,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;IAE/B,0BAA0B;IAC1B,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAA;CAC7B;AAED;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,qCAAuD,CAAA;AAE5E;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,EAAE,EAAE,UAAU,CAAA;IACvB,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC9C,QAAQ,CAAC,eAAe,CAAC,EAAE,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAA;IACrD,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;IAE7D,QAAQ,CAAC,oBAAoB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,SAAS,CAAA;IACrE,QAAQ,CAAC,mBAAmB,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,SAAS,CAAA;CACrE;AAED;;;;;GAKG;AACH,eAAO,MAAM,IAAI,GACf,SAAS,cAAc,KACtB,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,UAAU,CAgHjE,CAAA;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GACtB,QAAQ,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,KAClC,KAAK,CAAC,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,CAUrB,CAAA;AAEzC;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAChB,QAAQ,cAAc,KACrB,KAAK,CAAC,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,CAMrB,CAAA"}
|
package/dist/D1Client.js
CHANGED
|
@@ -1,29 +1,40 @@
|
|
|
1
1
|
import * as Cache from "effect/Cache";
|
|
2
2
|
import * as Config from "effect/Config";
|
|
3
|
+
import * as Context from "effect/Context";
|
|
3
4
|
import * as Duration from "effect/Duration";
|
|
4
5
|
import * as Effect from "effect/Effect";
|
|
5
6
|
import { identity } from "effect/Function";
|
|
6
7
|
import * as Layer from "effect/Layer";
|
|
7
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
8
8
|
import * as Stream from "effect/Stream";
|
|
9
9
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
10
10
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
11
|
-
import { SqlError } from "effect/unstable/sql/SqlError";
|
|
11
|
+
import { SqlError, UnknownError } from "effect/unstable/sql/SqlError";
|
|
12
12
|
import * as Statement from "effect/unstable/sql/Statement";
|
|
13
13
|
const ATTR_DB_SYSTEM_NAME = "db.system.name";
|
|
14
|
+
const classifyError = (cause, message, operation) => new UnknownError({
|
|
15
|
+
cause,
|
|
16
|
+
message,
|
|
17
|
+
operation
|
|
18
|
+
});
|
|
14
19
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
20
|
+
* Unique runtime identifier used to tag `D1Client` values.
|
|
21
|
+
*
|
|
22
|
+
* @category type IDs
|
|
23
|
+
* @since 4.0.0
|
|
17
24
|
*/
|
|
18
25
|
export const TypeId = "~@effect/sql-d1/D1Client";
|
|
19
26
|
/**
|
|
27
|
+
* Context tag used to access the `D1Client` service.
|
|
28
|
+
*
|
|
20
29
|
* @category tags
|
|
21
|
-
* @since
|
|
30
|
+
* @since 4.0.0
|
|
22
31
|
*/
|
|
23
|
-
export const D1Client = /*#__PURE__*/
|
|
32
|
+
export const D1Client = /*#__PURE__*/Context.Service("@effect/sql-d1/D1Client");
|
|
24
33
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
34
|
+
* Creates a scoped Cloudflare D1 SQL client. Prepared statements are cached, while transactions and streaming queries are not supported by this driver.
|
|
35
|
+
*
|
|
36
|
+
* @category constructors
|
|
37
|
+
* @since 4.0.0
|
|
27
38
|
*/
|
|
28
39
|
export const make = options => Effect.gen(function* () {
|
|
29
40
|
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames);
|
|
@@ -36,8 +47,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
36
47
|
lookup: sql => Effect.try({
|
|
37
48
|
try: () => db.prepare(sql),
|
|
38
49
|
catch: cause => new SqlError({
|
|
39
|
-
cause,
|
|
40
|
-
message: `Failed to prepare statement`
|
|
50
|
+
reason: classifyError(cause, "Failed to prepare statement", "prepare")
|
|
41
51
|
})
|
|
42
52
|
})
|
|
43
53
|
});
|
|
@@ -50,8 +60,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
50
60
|
return response.results || [];
|
|
51
61
|
},
|
|
52
62
|
catch: cause => new SqlError({
|
|
53
|
-
cause,
|
|
54
|
-
message: `Failed to execute statement`
|
|
63
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
55
64
|
})
|
|
56
65
|
});
|
|
57
66
|
const runRaw = (sql, params = []) => runStatement(db.prepare(sql), params);
|
|
@@ -62,8 +71,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
62
71
|
return statement.bind(...params).raw();
|
|
63
72
|
},
|
|
64
73
|
catch: cause => new SqlError({
|
|
65
|
-
cause,
|
|
66
|
-
message: `Failed to execute statement`
|
|
74
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
67
75
|
})
|
|
68
76
|
}));
|
|
69
77
|
return identity({
|
|
@@ -99,13 +107,17 @@ export const make = options => Effect.gen(function* () {
|
|
|
99
107
|
});
|
|
100
108
|
});
|
|
101
109
|
/**
|
|
110
|
+
* Creates a layer from a `Config`-wrapped D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
111
|
+
*
|
|
102
112
|
* @category layers
|
|
103
|
-
* @since
|
|
113
|
+
* @since 4.0.0
|
|
104
114
|
*/
|
|
105
|
-
export const layerConfig = config => Layer.
|
|
115
|
+
export const layerConfig = config => Layer.effectContext(Config.unwrap(config).pipe(Effect.flatMap(make), Effect.map(client => Context.make(D1Client, client).pipe(Context.add(Client.SqlClient, client))))).pipe(Layer.provide(Reactivity.layer));
|
|
106
116
|
/**
|
|
117
|
+
* Creates a layer from a concrete D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
118
|
+
*
|
|
107
119
|
* @category layers
|
|
108
|
-
* @since
|
|
120
|
+
* @since 4.0.0
|
|
109
121
|
*/
|
|
110
|
-
export const layer = config => Layer.
|
|
122
|
+
export const layer = config => Layer.effectContext(Effect.map(make(config), client => Context.make(D1Client, client).pipe(Context.add(Client.SqlClient, client)))).pipe(Layer.provide(Reactivity.layer));
|
|
111
123
|
//# sourceMappingURL=D1Client.js.map
|
package/dist/D1Client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"D1Client.js","names":["Cache","Config","Duration","Effect","identity","Layer","
|
|
1
|
+
{"version":3,"file":"D1Client.js","names":["Cache","Config","Context","Duration","Effect","identity","Layer","Stream","Reactivity","Client","SqlError","UnknownError","Statement","ATTR_DB_SYSTEM_NAME","classifyError","cause","message","operation","TypeId","D1Client","Service","make","options","gen","compiler","makeCompilerSqlite","transformQueryNames","transformRows","transformResultNames","defaultTransforms","array","undefined","makeConnection","db","prepareCache","capacity","prepareCacheSize","timeToLive","prepareCacheTTL","minutes","lookup","sql","try","prepare","catch","reason","runStatement","statement","params","tryPromise","response","bind","all","error","results","runRaw","runCached","flatMap","get","s","runUncached","runValues","raw","execute","map","executeRaw","executeValues","executeUnprepared","executeStream","_sql","_params","die","connection","acquirer","succeed","transactionAcquirer","Object","assign","spanAttributes","entries","config","layerConfig","effectContext","unwrap","pipe","client","add","SqlClient","provide","layer"],"sources":["../src/D1Client.ts"],"sourcesContent":[null],"mappings":"AAyBA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,QAAQ,MAAM,iBAAiB;AAC3C,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,SAASC,QAAQ,QAAQ,iBAAiB;AAC1C,OAAO,KAAKC,KAAK,MAAM,cAAc;AAErC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,UAAU,MAAM,uCAAuC;AACnE,OAAO,KAAKC,MAAM,MAAM,+BAA+B;AAEvD,SAASC,QAAQ,EAAEC,YAAY,QAAQ,8BAA8B;AACrE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9E,KAAK,CAACkF,OAAO,CAAChF,UAAU,CAACiF,KAAK,CAAC,CAAC","ignoreList":[]}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/sql-d1",
|
|
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 Cloudflare D1 integration for Effect",
|
|
@@ -47,14 +47,14 @@
|
|
|
47
47
|
"provenance": true
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"miniflare": "^4.
|
|
51
|
-
"effect": "^4.0.0-beta.
|
|
50
|
+
"miniflare": "^4.20260507.1",
|
|
51
|
+
"effect": "^4.0.0-beta.71"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"effect": "^4.0.0-beta.
|
|
54
|
+
"effect": "^4.0.0-beta.71"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@cloudflare/workers-types": "^4.
|
|
57
|
+
"@cloudflare/workers-types": "^4.20260511.1"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"codegen": "effect-utils codegen",
|
package/src/D1Client.ts
CHANGED
|
@@ -1,39 +1,69 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Cloudflare D1 client implementation for Effect SQL, backed by a Workers `D1Database` binding.
|
|
3
|
+
*
|
|
4
|
+
* This module adapts a Cloudflare D1 database binding into both the
|
|
5
|
+
* D1-specific `D1Client` service and the generic Effect `SqlClient` service.
|
|
6
|
+
* Use it in Workers, Pages Functions, and tests that provide a D1 binding to
|
|
7
|
+
* run SQLite-compatible queries through Effect services and layers, including
|
|
8
|
+
* repositories, migrations, request handlers, and local development against
|
|
9
|
+
* Wrangler or Miniflare-backed D1 databases.
|
|
10
|
+
*
|
|
11
|
+
* The client prepares statements with D1, caches them by SQL string, and uses
|
|
12
|
+
* the SQLite statement compiler for query and result name transforms. D1
|
|
13
|
+
* commits individual statements automatically, and native `D1Database.batch`
|
|
14
|
+
* is the D1 API for sequential, transactional multi-statement work; this
|
|
15
|
+
* adapter does not expose Effect SQL transactions because it cannot map
|
|
16
|
+
* `withTransaction` onto a connection-scoped D1 transaction. D1 databases are
|
|
17
|
+
* serverless SQLite storage with platform limits and single-database serialized
|
|
18
|
+
* execution, so keep queries small and indexed, batch large maintenance work
|
|
19
|
+
* at the D1 API level, and use D1 sessions outside this client when an
|
|
20
|
+
* application needs bookmark-based sequential consistency or read replicas.
|
|
21
|
+
* Streaming queries and `updateValues` are not supported.
|
|
22
|
+
*
|
|
23
|
+
* @since 4.0.0
|
|
3
24
|
*/
|
|
4
25
|
import type { D1Database, D1PreparedStatement } from "@cloudflare/workers-types"
|
|
5
26
|
import * as Cache from "effect/Cache"
|
|
6
27
|
import * as Config from "effect/Config"
|
|
28
|
+
import * as Context from "effect/Context"
|
|
7
29
|
import * as Duration from "effect/Duration"
|
|
8
30
|
import * as Effect from "effect/Effect"
|
|
9
31
|
import { identity } from "effect/Function"
|
|
10
32
|
import * as Layer from "effect/Layer"
|
|
11
33
|
import type * as Scope from "effect/Scope"
|
|
12
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
13
34
|
import * as Stream from "effect/Stream"
|
|
14
35
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
15
36
|
import * as Client from "effect/unstable/sql/SqlClient"
|
|
16
37
|
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
17
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
38
|
+
import { SqlError, UnknownError } from "effect/unstable/sql/SqlError"
|
|
18
39
|
import * as Statement from "effect/unstable/sql/Statement"
|
|
19
40
|
|
|
20
41
|
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
21
42
|
|
|
43
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
44
|
+
new UnknownError({ cause, message, operation })
|
|
45
|
+
|
|
22
46
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
47
|
+
* Unique runtime identifier used to tag `D1Client` values.
|
|
48
|
+
*
|
|
49
|
+
* @category type IDs
|
|
50
|
+
* @since 4.0.0
|
|
25
51
|
*/
|
|
26
52
|
export const TypeId: TypeId = "~@effect/sql-d1/D1Client"
|
|
27
53
|
|
|
28
54
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
55
|
+
* Type-level literal for the `D1Client` runtime identifier.
|
|
56
|
+
*
|
|
57
|
+
* @category type IDs
|
|
58
|
+
* @since 4.0.0
|
|
31
59
|
*/
|
|
32
60
|
export type TypeId = "~@effect/sql-d1/D1Client"
|
|
33
61
|
|
|
34
62
|
/**
|
|
63
|
+
* Cloudflare D1 SQL client service, extending `SqlClient` with its D1 configuration and no `updateValues` support.
|
|
64
|
+
*
|
|
35
65
|
* @category models
|
|
36
|
-
* @since
|
|
66
|
+
* @since 4.0.0
|
|
37
67
|
*/
|
|
38
68
|
export interface D1Client extends Client.SqlClient {
|
|
39
69
|
readonly [TypeId]: TypeId
|
|
@@ -44,14 +74,18 @@ export interface D1Client extends Client.SqlClient {
|
|
|
44
74
|
}
|
|
45
75
|
|
|
46
76
|
/**
|
|
77
|
+
* Context tag used to access the `D1Client` service.
|
|
78
|
+
*
|
|
47
79
|
* @category tags
|
|
48
|
-
* @since
|
|
80
|
+
* @since 4.0.0
|
|
49
81
|
*/
|
|
50
|
-
export const D1Client =
|
|
82
|
+
export const D1Client = Context.Service<D1Client>("@effect/sql-d1/D1Client")
|
|
51
83
|
|
|
52
84
|
/**
|
|
85
|
+
* Configuration for a Cloudflare D1 client, including the `D1Database`, prepared statement cache settings, span attributes, and query/result name transforms.
|
|
86
|
+
*
|
|
53
87
|
* @category models
|
|
54
|
-
* @since
|
|
88
|
+
* @since 4.0.0
|
|
55
89
|
*/
|
|
56
90
|
export interface D1ClientConfig {
|
|
57
91
|
readonly db: D1Database
|
|
@@ -64,8 +98,10 @@ export interface D1ClientConfig {
|
|
|
64
98
|
}
|
|
65
99
|
|
|
66
100
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
101
|
+
* Creates a scoped Cloudflare D1 SQL client. Prepared statements are cached, while transactions and streaming queries are not supported by this driver.
|
|
102
|
+
*
|
|
103
|
+
* @category constructors
|
|
104
|
+
* @since 4.0.0
|
|
69
105
|
*/
|
|
70
106
|
export const make = (
|
|
71
107
|
options: D1ClientConfig
|
|
@@ -85,7 +121,7 @@ export const make = (
|
|
|
85
121
|
lookup: (sql: string) =>
|
|
86
122
|
Effect.try({
|
|
87
123
|
try: () => db.prepare(sql),
|
|
88
|
-
catch: (cause) => new SqlError({ cause,
|
|
124
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to prepare statement", "prepare") })
|
|
89
125
|
})
|
|
90
126
|
})
|
|
91
127
|
|
|
@@ -101,7 +137,7 @@ export const make = (
|
|
|
101
137
|
}
|
|
102
138
|
return response.results || []
|
|
103
139
|
},
|
|
104
|
-
catch: (cause) => new SqlError({ cause,
|
|
140
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
105
141
|
})
|
|
106
142
|
|
|
107
143
|
const runRaw = (
|
|
@@ -134,7 +170,7 @@ export const make = (
|
|
|
134
170
|
>
|
|
135
171
|
>
|
|
136
172
|
},
|
|
137
|
-
catch: (cause) => new SqlError({ cause,
|
|
173
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
138
174
|
})
|
|
139
175
|
)
|
|
140
176
|
|
|
@@ -184,33 +220,37 @@ export const make = (
|
|
|
184
220
|
})
|
|
185
221
|
|
|
186
222
|
/**
|
|
223
|
+
* Creates a layer from a `Config`-wrapped D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
224
|
+
*
|
|
187
225
|
* @category layers
|
|
188
|
-
* @since
|
|
226
|
+
* @since 4.0.0
|
|
189
227
|
*/
|
|
190
228
|
export const layerConfig = (
|
|
191
229
|
config: Config.Wrap<D1ClientConfig>
|
|
192
230
|
): Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError> =>
|
|
193
|
-
Layer.
|
|
194
|
-
Config.unwrap(config).
|
|
231
|
+
Layer.effectContext(
|
|
232
|
+
Config.unwrap(config).pipe(
|
|
195
233
|
Effect.flatMap(make),
|
|
196
234
|
Effect.map((client) =>
|
|
197
|
-
|
|
198
|
-
|
|
235
|
+
Context.make(D1Client, client).pipe(
|
|
236
|
+
Context.add(Client.SqlClient, client)
|
|
199
237
|
)
|
|
200
238
|
)
|
|
201
239
|
)
|
|
202
240
|
).pipe(Layer.provide(Reactivity.layer))
|
|
203
241
|
|
|
204
242
|
/**
|
|
243
|
+
* Creates a layer from a concrete D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
244
|
+
*
|
|
205
245
|
* @category layers
|
|
206
|
-
* @since
|
|
246
|
+
* @since 4.0.0
|
|
207
247
|
*/
|
|
208
248
|
export const layer = (
|
|
209
249
|
config: D1ClientConfig
|
|
210
250
|
): Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError> =>
|
|
211
|
-
Layer.
|
|
251
|
+
Layer.effectContext(
|
|
212
252
|
Effect.map(make(config), (client) =>
|
|
213
|
-
|
|
214
|
-
|
|
253
|
+
Context.make(D1Client, client).pipe(
|
|
254
|
+
Context.add(Client.SqlClient, client)
|
|
215
255
|
))
|
|
216
256
|
).pipe(Layer.provide(Reactivity.layer))
|