@effect/sql-d1 4.0.0-beta.9 → 4.0.0-beta.91
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 +45 -15
- package/dist/D1Client.d.ts.map +1 -1
- package/dist/D1Client.js +47 -19
- 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 +77 -26
- package/src/index.ts +2 -2
package/dist/D1Client.d.ts
CHANGED
|
@@ -1,28 +1,43 @@
|
|
|
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
|
+
* It uses the SQLite statement compiler, caches prepared statements, maps D1
|
|
7
|
+
* failures to `SqlError`, and provides direct or config-backed layers.
|
|
8
|
+
* Transactions, streaming queries, and `updateValues` are not supported by this
|
|
9
|
+
* driver.
|
|
10
|
+
*
|
|
11
|
+
* @since 4.0.0
|
|
3
12
|
*/
|
|
4
13
|
import type { D1Database } from "@cloudflare/workers-types";
|
|
5
14
|
import * as Config from "effect/Config";
|
|
15
|
+
import * as Context from "effect/Context";
|
|
6
16
|
import * as Duration from "effect/Duration";
|
|
7
17
|
import * as Effect from "effect/Effect";
|
|
8
18
|
import * as Layer from "effect/Layer";
|
|
9
19
|
import type * as Scope from "effect/Scope";
|
|
10
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
11
20
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
12
21
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
13
22
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
23
|
+
* Unique runtime identifier used to tag `D1Client` values.
|
|
24
|
+
*
|
|
25
|
+
* @category type IDs
|
|
26
|
+
* @since 4.0.0
|
|
16
27
|
*/
|
|
17
28
|
export declare const TypeId: TypeId;
|
|
18
29
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
30
|
+
* Type-level literal for the `D1Client` runtime identifier.
|
|
31
|
+
*
|
|
32
|
+
* @category type IDs
|
|
33
|
+
* @since 4.0.0
|
|
21
34
|
*/
|
|
22
35
|
export type TypeId = "~@effect/sql-d1/D1Client";
|
|
23
36
|
/**
|
|
37
|
+
* Cloudflare D1 SQL client service, extending `SqlClient` with its D1 configuration and no `updateValues` support.
|
|
38
|
+
*
|
|
24
39
|
* @category models
|
|
25
|
-
* @since
|
|
40
|
+
* @since 4.0.0
|
|
26
41
|
*/
|
|
27
42
|
export interface D1Client extends Client.SqlClient {
|
|
28
43
|
readonly [TypeId]: TypeId;
|
|
@@ -31,13 +46,22 @@ export interface D1Client extends Client.SqlClient {
|
|
|
31
46
|
readonly updateValues: never;
|
|
32
47
|
}
|
|
33
48
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
49
|
+
* Service tag for the Cloudflare D1 SQL client.
|
|
50
|
+
*
|
|
51
|
+
* **When to use**
|
|
52
|
+
*
|
|
53
|
+
* Use to access or provide a Cloudflare D1 SQL client through the Effect
|
|
54
|
+
* context.
|
|
55
|
+
*
|
|
56
|
+
* @category services
|
|
57
|
+
* @since 4.0.0
|
|
36
58
|
*/
|
|
37
|
-
export declare const D1Client:
|
|
59
|
+
export declare const D1Client: Context.Service<D1Client, D1Client>;
|
|
38
60
|
/**
|
|
61
|
+
* Configuration for a Cloudflare D1 client, including the `D1Database`, prepared statement cache settings, span attributes, and query/result name transforms.
|
|
62
|
+
*
|
|
39
63
|
* @category models
|
|
40
|
-
* @since
|
|
64
|
+
* @since 4.0.0
|
|
41
65
|
*/
|
|
42
66
|
export interface D1ClientConfig {
|
|
43
67
|
readonly db: D1Database;
|
|
@@ -48,18 +72,24 @@ export interface D1ClientConfig {
|
|
|
48
72
|
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
49
73
|
}
|
|
50
74
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
75
|
+
* Creates a scoped Cloudflare D1 SQL client. Prepared statements are cached, while transactions and streaming queries are not supported by this driver.
|
|
76
|
+
*
|
|
77
|
+
* @category constructors
|
|
78
|
+
* @since 4.0.0
|
|
53
79
|
*/
|
|
54
80
|
export declare const make: (options: D1ClientConfig) => Effect.Effect<D1Client, never, Scope.Scope | Reactivity.Reactivity>;
|
|
55
81
|
/**
|
|
82
|
+
* Creates a layer from a `Config`-wrapped D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
83
|
+
*
|
|
56
84
|
* @category layers
|
|
57
|
-
* @since
|
|
85
|
+
* @since 4.0.0
|
|
58
86
|
*/
|
|
59
87
|
export declare const layerConfig: (config: Config.Wrap<D1ClientConfig>) => Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError>;
|
|
60
88
|
/**
|
|
89
|
+
* Creates a layer from a concrete D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
90
|
+
*
|
|
61
91
|
* @category layers
|
|
62
|
-
* @since
|
|
92
|
+
* @since 4.0.0
|
|
63
93
|
*/
|
|
64
94
|
export declare const layer: (config: D1ClientConfig) => Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError>;
|
|
65
95
|
//# 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;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,UAAU,EAAuB,MAAM,2BAA2B,CAAA;AAEhF,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;A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;AACH,eAAO,MAAM,IAAI,GACf,SAAS,cAAc,KACtB,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,UAAU,CAkIjE,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,45 @@
|
|
|
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
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
27
|
+
* Service tag for the Cloudflare D1 SQL client.
|
|
28
|
+
*
|
|
29
|
+
* **When to use**
|
|
30
|
+
*
|
|
31
|
+
* Use to access or provide a Cloudflare D1 SQL client through the Effect
|
|
32
|
+
* context.
|
|
33
|
+
*
|
|
34
|
+
* @category services
|
|
35
|
+
* @since 4.0.0
|
|
22
36
|
*/
|
|
23
|
-
export const D1Client = /*#__PURE__*/
|
|
37
|
+
export const D1Client = /*#__PURE__*/Context.Service("@effect/sql-d1/D1Client");
|
|
24
38
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
39
|
+
* Creates a scoped Cloudflare D1 SQL client. Prepared statements are cached, while transactions and streaming queries are not supported by this driver.
|
|
40
|
+
*
|
|
41
|
+
* @category constructors
|
|
42
|
+
* @since 4.0.0
|
|
27
43
|
*/
|
|
28
44
|
export const make = options => Effect.gen(function* () {
|
|
29
45
|
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames);
|
|
@@ -36,8 +52,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
36
52
|
lookup: sql => Effect.try({
|
|
37
53
|
try: () => db.prepare(sql),
|
|
38
54
|
catch: cause => new SqlError({
|
|
39
|
-
cause,
|
|
40
|
-
message: `Failed to prepare statement`
|
|
55
|
+
reason: classifyError(cause, "Failed to prepare statement", "prepare")
|
|
41
56
|
})
|
|
42
57
|
})
|
|
43
58
|
});
|
|
@@ -50,8 +65,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
50
65
|
return response.results || [];
|
|
51
66
|
},
|
|
52
67
|
catch: cause => new SqlError({
|
|
53
|
-
cause,
|
|
54
|
-
message: `Failed to execute statement`
|
|
68
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
55
69
|
})
|
|
56
70
|
});
|
|
57
71
|
const runRaw = (sql, params = []) => runStatement(db.prepare(sql), params);
|
|
@@ -62,10 +76,17 @@ export const make = options => Effect.gen(function* () {
|
|
|
62
76
|
return statement.bind(...params).raw();
|
|
63
77
|
},
|
|
64
78
|
catch: cause => new SqlError({
|
|
65
|
-
cause,
|
|
66
|
-
message: `Failed to execute statement`
|
|
79
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
67
80
|
})
|
|
68
81
|
}));
|
|
82
|
+
const runValuesUncached = (sql, params) => Effect.tryPromise({
|
|
83
|
+
try: () => {
|
|
84
|
+
return db.prepare(sql).bind(...params).raw();
|
|
85
|
+
},
|
|
86
|
+
catch: cause => new SqlError({
|
|
87
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
88
|
+
})
|
|
89
|
+
});
|
|
69
90
|
return identity({
|
|
70
91
|
execute(sql, params, transformRows) {
|
|
71
92
|
return transformRows ? Effect.map(runCached(sql, params), transformRows) : runCached(sql, params);
|
|
@@ -76,6 +97,9 @@ export const make = options => Effect.gen(function* () {
|
|
|
76
97
|
executeValues(sql, params) {
|
|
77
98
|
return runValues(sql, params);
|
|
78
99
|
},
|
|
100
|
+
executeValuesUnprepared(sql, params) {
|
|
101
|
+
return runValuesUncached(sql, params);
|
|
102
|
+
},
|
|
79
103
|
executeUnprepared(sql, params, transformRows) {
|
|
80
104
|
return transformRows ? Effect.map(runUncached(sql, params), transformRows) : runUncached(sql, params);
|
|
81
105
|
},
|
|
@@ -99,13 +123,17 @@ export const make = options => Effect.gen(function* () {
|
|
|
99
123
|
});
|
|
100
124
|
});
|
|
101
125
|
/**
|
|
126
|
+
* Creates a layer from a `Config`-wrapped D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
127
|
+
*
|
|
102
128
|
* @category layers
|
|
103
|
-
* @since
|
|
129
|
+
* @since 4.0.0
|
|
104
130
|
*/
|
|
105
|
-
export const layerConfig = config => Layer.
|
|
131
|
+
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
132
|
/**
|
|
133
|
+
* Creates a layer from a concrete D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
134
|
+
*
|
|
107
135
|
* @category layers
|
|
108
|
-
* @since
|
|
136
|
+
* @since 4.0.0
|
|
109
137
|
*/
|
|
110
|
-
export const layer = config => Layer.
|
|
138
|
+
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
139
|
//# 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","runValuesUncached","execute","map","executeRaw","executeValues","executeValuesUnprepared","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":"AAaA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC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F;IACE,CAACT,MAAM,GAAGA,MAAgB;IAC1BgE,MAAM,EAAE5D;GACT,CACF;AACH,CAAC,CAAC;AAEJ;;;;;;AAMA,OAAO,MAAM6D,WAAW,GACtBD,MAAmC,IAEnC5E,KAAK,CAAC8E,aAAa,CACjBnF,MAAM,CAACoF,MAAM,CAACH,MAAM,CAAC,CAACI,IAAI,CACxBlF,MAAM,CAACqD,OAAO,CAACpC,IAAI,CAAC,EACpBjB,MAAM,CAAC6D,GAAG,CAAEsB,MAAM,IAChBrF,OAAO,CAACmB,IAAI,CAACF,QAAQ,EAAEoE,MAAM,CAAC,CAACD,IAAI,CACjCpF,OAAO,CAACsF,GAAG,CAAC/E,MAAM,CAACgF,SAAS,EAAEF,MAAM,CAAC,CACtC,CACF,CACF,CACF,CAACD,IAAI,CAAChF,KAAK,CAACoF,OAAO,CAAClF,UAAU,CAACmF,KAAK,CAAC,CAAC;AAEzC;;;;;;AAMA,OAAO,MAAMA,KAAK,GAChBT,MAAsB,IAEtB5E,KAAK,CAAC8E,aAAa,CACjBhF,MAAM,CAAC6D,GAAG,CAAC5C,IAAI,CAAC6D,MAAM,CAAC,EAAGK,MAAM,IAC9BrF,OAAO,CAACmB,IAAI,CAACF,QAAQ,EAAEoE,MAAM,CAAC,CAACD,IAAI,CACjCpF,OAAO,CAACsF,GAAG,CAAC/E,MAAM,CAACgF,SAAS,EAAEF,MAAM,CAAC,CACtC,CAAC,CACL,CAACD,IAAI,CAAChF,KAAK,CAACoF,OAAO,CAAClF,UAAU,CAACmF,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.91",
|
|
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.91"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"effect": "^4.0.0-beta.
|
|
54
|
+
"effect": "^4.0.0-beta.91"
|
|
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,57 @@
|
|
|
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
|
+
* It uses the SQLite statement compiler, caches prepared statements, maps D1
|
|
7
|
+
* failures to `SqlError`, and provides direct or config-backed layers.
|
|
8
|
+
* Transactions, streaming queries, and `updateValues` are not supported by this
|
|
9
|
+
* driver.
|
|
10
|
+
*
|
|
11
|
+
* @since 4.0.0
|
|
3
12
|
*/
|
|
4
13
|
import type { D1Database, D1PreparedStatement } from "@cloudflare/workers-types"
|
|
5
14
|
import * as Cache from "effect/Cache"
|
|
6
15
|
import * as Config from "effect/Config"
|
|
16
|
+
import * as Context from "effect/Context"
|
|
7
17
|
import * as Duration from "effect/Duration"
|
|
8
18
|
import * as Effect from "effect/Effect"
|
|
9
19
|
import { identity } from "effect/Function"
|
|
10
20
|
import * as Layer from "effect/Layer"
|
|
11
21
|
import type * as Scope from "effect/Scope"
|
|
12
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
13
22
|
import * as Stream from "effect/Stream"
|
|
14
23
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
15
24
|
import * as Client from "effect/unstable/sql/SqlClient"
|
|
16
25
|
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
17
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
26
|
+
import { SqlError, UnknownError } from "effect/unstable/sql/SqlError"
|
|
18
27
|
import * as Statement from "effect/unstable/sql/Statement"
|
|
19
28
|
|
|
20
29
|
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
21
30
|
|
|
31
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
32
|
+
new UnknownError({ cause, message, operation })
|
|
33
|
+
|
|
22
34
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
35
|
+
* Unique runtime identifier used to tag `D1Client` values.
|
|
36
|
+
*
|
|
37
|
+
* @category type IDs
|
|
38
|
+
* @since 4.0.0
|
|
25
39
|
*/
|
|
26
40
|
export const TypeId: TypeId = "~@effect/sql-d1/D1Client"
|
|
27
41
|
|
|
28
42
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
43
|
+
* Type-level literal for the `D1Client` runtime identifier.
|
|
44
|
+
*
|
|
45
|
+
* @category type IDs
|
|
46
|
+
* @since 4.0.0
|
|
31
47
|
*/
|
|
32
48
|
export type TypeId = "~@effect/sql-d1/D1Client"
|
|
33
49
|
|
|
34
50
|
/**
|
|
51
|
+
* Cloudflare D1 SQL client service, extending `SqlClient` with its D1 configuration and no `updateValues` support.
|
|
52
|
+
*
|
|
35
53
|
* @category models
|
|
36
|
-
* @since
|
|
54
|
+
* @since 4.0.0
|
|
37
55
|
*/
|
|
38
56
|
export interface D1Client extends Client.SqlClient {
|
|
39
57
|
readonly [TypeId]: TypeId
|
|
@@ -44,14 +62,23 @@ export interface D1Client extends Client.SqlClient {
|
|
|
44
62
|
}
|
|
45
63
|
|
|
46
64
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
65
|
+
* Service tag for the Cloudflare D1 SQL client.
|
|
66
|
+
*
|
|
67
|
+
* **When to use**
|
|
68
|
+
*
|
|
69
|
+
* Use to access or provide a Cloudflare D1 SQL client through the Effect
|
|
70
|
+
* context.
|
|
71
|
+
*
|
|
72
|
+
* @category services
|
|
73
|
+
* @since 4.0.0
|
|
49
74
|
*/
|
|
50
|
-
export const D1Client =
|
|
75
|
+
export const D1Client = Context.Service<D1Client>("@effect/sql-d1/D1Client")
|
|
51
76
|
|
|
52
77
|
/**
|
|
78
|
+
* Configuration for a Cloudflare D1 client, including the `D1Database`, prepared statement cache settings, span attributes, and query/result name transforms.
|
|
79
|
+
*
|
|
53
80
|
* @category models
|
|
54
|
-
* @since
|
|
81
|
+
* @since 4.0.0
|
|
55
82
|
*/
|
|
56
83
|
export interface D1ClientConfig {
|
|
57
84
|
readonly db: D1Database
|
|
@@ -64,8 +91,10 @@ export interface D1ClientConfig {
|
|
|
64
91
|
}
|
|
65
92
|
|
|
66
93
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
94
|
+
* Creates a scoped Cloudflare D1 SQL client. Prepared statements are cached, while transactions and streaming queries are not supported by this driver.
|
|
95
|
+
*
|
|
96
|
+
* @category constructors
|
|
97
|
+
* @since 4.0.0
|
|
69
98
|
*/
|
|
70
99
|
export const make = (
|
|
71
100
|
options: D1ClientConfig
|
|
@@ -85,7 +114,7 @@ export const make = (
|
|
|
85
114
|
lookup: (sql: string) =>
|
|
86
115
|
Effect.try({
|
|
87
116
|
try: () => db.prepare(sql),
|
|
88
|
-
catch: (cause) => new SqlError({ cause,
|
|
117
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to prepare statement", "prepare") })
|
|
89
118
|
})
|
|
90
119
|
})
|
|
91
120
|
|
|
@@ -101,7 +130,7 @@ export const make = (
|
|
|
101
130
|
}
|
|
102
131
|
return response.results || []
|
|
103
132
|
},
|
|
104
|
-
catch: (cause) => new SqlError({ cause,
|
|
133
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
105
134
|
})
|
|
106
135
|
|
|
107
136
|
const runRaw = (
|
|
@@ -134,10 +163,25 @@ export const make = (
|
|
|
134
163
|
>
|
|
135
164
|
>
|
|
136
165
|
},
|
|
137
|
-
catch: (cause) => new SqlError({ cause,
|
|
166
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
138
167
|
})
|
|
139
168
|
)
|
|
140
169
|
|
|
170
|
+
const runValuesUncached = (
|
|
171
|
+
sql: string,
|
|
172
|
+
params: ReadonlyArray<unknown>
|
|
173
|
+
) =>
|
|
174
|
+
Effect.tryPromise({
|
|
175
|
+
try: () => {
|
|
176
|
+
return db.prepare(sql).bind(...params).raw() as Promise<
|
|
177
|
+
ReadonlyArray<
|
|
178
|
+
ReadonlyArray<unknown>
|
|
179
|
+
>
|
|
180
|
+
>
|
|
181
|
+
},
|
|
182
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
183
|
+
})
|
|
184
|
+
|
|
141
185
|
return identity<Connection>({
|
|
142
186
|
execute(sql, params, transformRows) {
|
|
143
187
|
return transformRows
|
|
@@ -150,6 +194,9 @@ export const make = (
|
|
|
150
194
|
executeValues(sql, params) {
|
|
151
195
|
return runValues(sql, params)
|
|
152
196
|
},
|
|
197
|
+
executeValuesUnprepared(sql, params) {
|
|
198
|
+
return runValuesUncached(sql, params)
|
|
199
|
+
},
|
|
153
200
|
executeUnprepared(sql, params, transformRows) {
|
|
154
201
|
return transformRows
|
|
155
202
|
? Effect.map(runUncached(sql, params), transformRows)
|
|
@@ -184,33 +231,37 @@ export const make = (
|
|
|
184
231
|
})
|
|
185
232
|
|
|
186
233
|
/**
|
|
234
|
+
* Creates a layer from a `Config`-wrapped D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
235
|
+
*
|
|
187
236
|
* @category layers
|
|
188
|
-
* @since
|
|
237
|
+
* @since 4.0.0
|
|
189
238
|
*/
|
|
190
239
|
export const layerConfig = (
|
|
191
240
|
config: Config.Wrap<D1ClientConfig>
|
|
192
241
|
): Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError> =>
|
|
193
|
-
Layer.
|
|
194
|
-
Config.unwrap(config).
|
|
242
|
+
Layer.effectContext(
|
|
243
|
+
Config.unwrap(config).pipe(
|
|
195
244
|
Effect.flatMap(make),
|
|
196
245
|
Effect.map((client) =>
|
|
197
|
-
|
|
198
|
-
|
|
246
|
+
Context.make(D1Client, client).pipe(
|
|
247
|
+
Context.add(Client.SqlClient, client)
|
|
199
248
|
)
|
|
200
249
|
)
|
|
201
250
|
)
|
|
202
251
|
).pipe(Layer.provide(Reactivity.layer))
|
|
203
252
|
|
|
204
253
|
/**
|
|
254
|
+
* Creates a layer from a concrete D1 client configuration, providing both `D1Client` and `SqlClient`.
|
|
255
|
+
*
|
|
205
256
|
* @category layers
|
|
206
|
-
* @since
|
|
257
|
+
* @since 4.0.0
|
|
207
258
|
*/
|
|
208
259
|
export const layer = (
|
|
209
260
|
config: D1ClientConfig
|
|
210
261
|
): Layer.Layer<D1Client | Client.SqlClient, Config.ConfigError> =>
|
|
211
|
-
Layer.
|
|
262
|
+
Layer.effectContext(
|
|
212
263
|
Effect.map(make(config), (client) =>
|
|
213
|
-
|
|
214
|
-
|
|
264
|
+
Context.make(D1Client, client).pipe(
|
|
265
|
+
Context.add(Client.SqlClient, client)
|
|
215
266
|
))
|
|
216
267
|
).pipe(Layer.provide(Reactivity.layer))
|