@effect/sql-d1 0.0.0-snapshot-5fe45984d8022d8f8001e1057a5bacdd3e510676
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/D1Client/package.json +6 -0
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/cjs/D1Client.js +133 -0
- package/dist/cjs/D1Client.js.map +1 -0
- package/dist/cjs/index.js +34 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/dts/D1Client.d.ts +60 -0
- package/dist/dts/D1Client.d.ts.map +1 -0
- package/dist/dts/index.d.ts +5 -0
- package/dist/dts/index.d.ts.map +1 -0
- package/dist/esm/D1Client.js +100 -0
- package/dist/esm/D1Client.js.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +4 -0
- package/package.json +47 -0
- package/src/D1Client.ts +194 -0
- package/src/index.ts +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present The Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.make = exports.layer = exports.TypeId = exports.D1Client = void 0;
|
|
7
|
+
var Client = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/SqlClient"));
|
|
8
|
+
var _SqlError = /*#__PURE__*/require("@effect/sql/SqlError");
|
|
9
|
+
var Statement = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/sql/Statement"));
|
|
10
|
+
var Otel = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@opentelemetry/semantic-conventions"));
|
|
11
|
+
var Cache = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Cache"));
|
|
12
|
+
var Config = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Config"));
|
|
13
|
+
var Context = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Context"));
|
|
14
|
+
var Duration = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Duration"));
|
|
15
|
+
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Effect"));
|
|
16
|
+
var _Function = /*#__PURE__*/require("effect/Function");
|
|
17
|
+
var Layer = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Layer"));
|
|
18
|
+
function _getRequireWildcardCache(e) {
|
|
19
|
+
if ("function" != typeof WeakMap) return null;
|
|
20
|
+
var r = new WeakMap(),
|
|
21
|
+
t = new WeakMap();
|
|
22
|
+
return (_getRequireWildcardCache = function (e) {
|
|
23
|
+
return e ? t : r;
|
|
24
|
+
})(e);
|
|
25
|
+
}
|
|
26
|
+
function _interopRequireWildcard(e, r) {
|
|
27
|
+
if (!r && e && e.__esModule) return e;
|
|
28
|
+
if (null === e || "object" != typeof e && "function" != typeof e) return {
|
|
29
|
+
default: e
|
|
30
|
+
};
|
|
31
|
+
var t = _getRequireWildcardCache(r);
|
|
32
|
+
if (t && t.has(e)) return t.get(e);
|
|
33
|
+
var n = {
|
|
34
|
+
__proto__: null
|
|
35
|
+
},
|
|
36
|
+
a = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
37
|
+
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
|
|
38
|
+
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
|
|
39
|
+
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
|
|
40
|
+
}
|
|
41
|
+
return n.default = e, t && t.set(e, n), n;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @category type ids
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
*/
|
|
47
|
+
const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("@effect/sql-d1/D1Client");
|
|
48
|
+
/**
|
|
49
|
+
* @category tags
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
*/
|
|
52
|
+
const D1Client = exports.D1Client = /*#__PURE__*/Context.GenericTag("@effect/sql-d1/D1Client");
|
|
53
|
+
/**
|
|
54
|
+
* @category constructor
|
|
55
|
+
* @since 1.0.0
|
|
56
|
+
*/
|
|
57
|
+
const make = options => Effect.gen(function* () {
|
|
58
|
+
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames);
|
|
59
|
+
const transformRows = Statement.defaultTransforms(options.transformResultNames).array;
|
|
60
|
+
const makeConnection = Effect.gen(function* () {
|
|
61
|
+
const db = options.db;
|
|
62
|
+
const prepareCache = yield* Cache.make({
|
|
63
|
+
capacity: options.prepareCacheSize ?? 200,
|
|
64
|
+
timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
|
|
65
|
+
lookup: sql => Effect.try({
|
|
66
|
+
try: () => db.prepare(sql),
|
|
67
|
+
catch: error => new _SqlError.SqlError({
|
|
68
|
+
error
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
});
|
|
72
|
+
const runStatement = (statement, params = []) => Effect.tryPromise({
|
|
73
|
+
try: async () => {
|
|
74
|
+
const response = await statement.bind(...params).all();
|
|
75
|
+
if (response.error) {
|
|
76
|
+
throw response.error;
|
|
77
|
+
}
|
|
78
|
+
return response.results || [];
|
|
79
|
+
},
|
|
80
|
+
catch: error => new _SqlError.SqlError({
|
|
81
|
+
error
|
|
82
|
+
})
|
|
83
|
+
});
|
|
84
|
+
const run = (sql, params = []) => Effect.flatMap(prepareCache.get(sql), s => runStatement(s, params));
|
|
85
|
+
const runRaw = (sql, params = []) => Effect.map(runStatement(db.prepare(sql), params), transformRows);
|
|
86
|
+
const runTransform = options.transformResultNames ? (sql, params) => Effect.map(run(sql, params), transformRows) : run;
|
|
87
|
+
const runValues = (sql, params) => Effect.flatMap(prepareCache.get(sql), statement => Effect.tryPromise({
|
|
88
|
+
try: () => {
|
|
89
|
+
return statement.bind(...params).raw();
|
|
90
|
+
},
|
|
91
|
+
catch: error => new _SqlError.SqlError({
|
|
92
|
+
error
|
|
93
|
+
})
|
|
94
|
+
}));
|
|
95
|
+
return (0, _Function.identity)({
|
|
96
|
+
execute(sql, params) {
|
|
97
|
+
return runTransform(sql, params);
|
|
98
|
+
},
|
|
99
|
+
executeValues(sql, params) {
|
|
100
|
+
return runValues(sql, params);
|
|
101
|
+
},
|
|
102
|
+
executeWithoutTransform(sql, params) {
|
|
103
|
+
return run(sql, params);
|
|
104
|
+
},
|
|
105
|
+
executeRaw(sql, params) {
|
|
106
|
+
return runRaw(sql, params);
|
|
107
|
+
},
|
|
108
|
+
executeStream(_sql, _params) {
|
|
109
|
+
return Effect.dieMessage("executeStream not implemented");
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
const connection = yield* makeConnection;
|
|
114
|
+
const acquirer = Effect.succeed(connection);
|
|
115
|
+
const transactionAcquirer = Effect.dieMessage("transactions are not supported in D1");
|
|
116
|
+
return Object.assign(Client.make({
|
|
117
|
+
acquirer,
|
|
118
|
+
compiler,
|
|
119
|
+
transactionAcquirer,
|
|
120
|
+
spanAttributes: [...(options.spanAttributes ? Object.entries(options.spanAttributes) : []), [Otel.SEMATTRS_DB_SYSTEM, Otel.DBSYSTEMVALUES_SQLITE]]
|
|
121
|
+
}), {
|
|
122
|
+
[TypeId]: TypeId,
|
|
123
|
+
config: options
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
/**
|
|
127
|
+
* @category layers
|
|
128
|
+
* @since 1.0.0
|
|
129
|
+
*/
|
|
130
|
+
exports.make = make;
|
|
131
|
+
const layer = config => Layer.scopedContext(Config.unwrap(config).pipe(Effect.flatMap(make), Effect.map(client => Context.make(D1Client, client).pipe(Context.add(Client.SqlClient, client)))));
|
|
132
|
+
exports.layer = layer;
|
|
133
|
+
//# sourceMappingURL=D1Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"D1Client.js","names":["Client","_interopRequireWildcard","require","_SqlError","Statement","Otel","Cache","Config","Context","Duration","Effect","_Function","Layer","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","TypeId","exports","Symbol","for","D1Client","GenericTag","make","options","gen","compiler","makeCompilerSqlite","transformQueryNames","transformRows","defaultTransforms","transformResultNames","array","makeConnection","db","prepareCache","capacity","prepareCacheSize","timeToLive","prepareCacheTTL","minutes","lookup","sql","try","prepare","catch","error","SqlError","runStatement","statement","params","tryPromise","response","bind","all","results","run","flatMap","s","runRaw","map","runTransform","runValues","raw","identity","execute","executeValues","executeWithoutTransform","executeRaw","executeStream","_sql","_params","dieMessage","connection","acquirer","succeed","transactionAcquirer","assign","spanAttributes","entries","SEMATTRS_DB_SYSTEM","DBSYSTEMVALUES_SQLITE","config","layer","scopedContext","unwrap","pipe","client","add","SqlClient"],"sources":["../../src/D1Client.ts"],"sourcesContent":[null],"mappings":";;;;;;AAIA,IAAAA,MAAA,gBAAAC,uBAAA,eAAAC,OAAA;AAEA,IAAAC,SAAA,gBAAAD,OAAA;AACA,IAAAE,SAAA,gBAAAH,uBAAA,eAAAC,OAAA;AACA,IAAAG,IAAA,gBAAAJ,uBAAA,eAAAC,OAAA;AACA,IAAAI,KAAA,gBAAAL,uBAAA,eAAAC,OAAA;AACA,IAAAK,MAAA,gBAAAN,uBAAA,eAAAC,OAAA;AAEA,IAAAM,OAAA,gBAAAP,uBAAA,eAAAC,OAAA;AACA,IAAAO,QAAA,gBAAAR,uBAAA,eAAAC,OAAA;AACA,IAAAQ,MAAA,gBAAAT,uBAAA,eAAAC,OAAA;AACA,IAAAS,SAAA,gBAAAT,OAAA;AACA,IAAAU,KAAA,gBAAAX,uBAAA,eAAAC,OAAA;AAAqC,SAAAW,yBAAAC,CAAA;EAAA,yBAAAC,OAAA;EAAA,IAAAC,CAAA,OAAAD,OAAA;IAAAE,CAAA,OAAAF,OAAA;EAAA,QAAAF,wBAAA,YAAAA,CAAAC,CAAA;IAAA,OAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA;EAAA,GAAAF,CAAA;AAAA;AAAA,SAAAb,wBAAAa,CAAA,EAAAE,CAAA;EAAA,KAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA;EAAA,aAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA;IAAAK,OAAA,EAAAL;EAAA;EAAA,IAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA;EAAA,IAAAC,CAAA,IAA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
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.D1Client = void 0;
|
|
7
|
+
var _D1Client = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./D1Client.js"));
|
|
8
|
+
exports.D1Client = _D1Client;
|
|
9
|
+
function _getRequireWildcardCache(e) {
|
|
10
|
+
if ("function" != typeof WeakMap) return null;
|
|
11
|
+
var r = new WeakMap(),
|
|
12
|
+
t = new WeakMap();
|
|
13
|
+
return (_getRequireWildcardCache = function (e) {
|
|
14
|
+
return e ? t : r;
|
|
15
|
+
})(e);
|
|
16
|
+
}
|
|
17
|
+
function _interopRequireWildcard(e, r) {
|
|
18
|
+
if (!r && e && e.__esModule) return e;
|
|
19
|
+
if (null === e || "object" != typeof e && "function" != typeof e) return {
|
|
20
|
+
default: e
|
|
21
|
+
};
|
|
22
|
+
var t = _getRequireWildcardCache(r);
|
|
23
|
+
if (t && t.has(e)) return t.get(e);
|
|
24
|
+
var n = {
|
|
25
|
+
__proto__: null
|
|
26
|
+
},
|
|
27
|
+
a = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
28
|
+
for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
|
|
29
|
+
var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
|
|
30
|
+
i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
|
|
31
|
+
}
|
|
32
|
+
return n.default = e, t && t.set(e, n), n;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
5
|
+
import * as Client from "@effect/sql/SqlClient";
|
|
6
|
+
import * as Config from "effect/Config";
|
|
7
|
+
import type { ConfigError } from "effect/ConfigError";
|
|
8
|
+
import * as Context from "effect/Context";
|
|
9
|
+
import * as Duration from "effect/Duration";
|
|
10
|
+
import * as Effect from "effect/Effect";
|
|
11
|
+
import * as Layer from "effect/Layer";
|
|
12
|
+
import type * as Scope from "effect/Scope";
|
|
13
|
+
/**
|
|
14
|
+
* @category type ids
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
export declare const TypeId: unique symbol;
|
|
18
|
+
/**
|
|
19
|
+
* @category type ids
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
export type TypeId = typeof TypeId;
|
|
23
|
+
/**
|
|
24
|
+
* @category models
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
export interface D1Client extends Client.SqlClient {
|
|
28
|
+
readonly [TypeId]: TypeId;
|
|
29
|
+
readonly config: D1ClientConfig;
|
|
30
|
+
/** Not supported in d1 */
|
|
31
|
+
readonly updateValues: never;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @category tags
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
export declare const D1Client: Context.Tag<D1Client, D1Client>;
|
|
38
|
+
/**
|
|
39
|
+
* @category models
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
*/
|
|
42
|
+
export interface D1ClientConfig {
|
|
43
|
+
readonly db: D1Database;
|
|
44
|
+
readonly prepareCacheSize?: number | undefined;
|
|
45
|
+
readonly prepareCacheTTL?: Duration.DurationInput | undefined;
|
|
46
|
+
readonly spanAttributes?: Record<string, unknown> | undefined;
|
|
47
|
+
readonly transformResultNames?: ((str: string) => string) | undefined;
|
|
48
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @category constructor
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
*/
|
|
54
|
+
export declare const make: (options: D1ClientConfig) => Effect.Effect<D1Client, never, Scope.Scope>;
|
|
55
|
+
/**
|
|
56
|
+
* @category layers
|
|
57
|
+
* @since 1.0.0
|
|
58
|
+
*/
|
|
59
|
+
export declare const layer: (config: Config.Config.Wrap<D1ClientConfig>) => Layer.Layer<D1Client | Client.SqlClient, ConfigError>;
|
|
60
|
+
//# sourceMappingURL=D1Client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"D1Client.d.ts","sourceRoot":"","sources":["../../src/D1Client.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,UAAU,EAAuB,MAAM,2BAA2B,CAAA;AAChF,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAA;AAM/C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AACrD,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;;;GAGG;AACH,eAAO,MAAM,MAAM,EAAE,OAAO,MAA8C,CAAA;AAE1E;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,MAAM,CAAA;AAElC;;;GAGG;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;;;GAGG;AACH,eAAO,MAAM,QAAQ,iCAA0D,CAAA;AAE/E;;;GAGG;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,aAAa,GAAG,SAAS,CAAA;IAC7D,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;;;GAGG;AACH,eAAO,MAAM,IAAI,YACN,cAAc,KACtB,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CA0GzC,CAAA;AAEJ;;;GAGG;AACH,eAAO,MAAM,KAAK,WACR,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,KACzC,KAAK,CAAC,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,SAAS,EAAE,WAAW,CAUpD,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import * as Client from "@effect/sql/SqlClient";
|
|
2
|
+
import { SqlError } from "@effect/sql/SqlError";
|
|
3
|
+
import * as Statement from "@effect/sql/Statement";
|
|
4
|
+
import * as Otel from "@opentelemetry/semantic-conventions";
|
|
5
|
+
import * as Cache from "effect/Cache";
|
|
6
|
+
import * as Config from "effect/Config";
|
|
7
|
+
import * as Context from "effect/Context";
|
|
8
|
+
import * as Duration from "effect/Duration";
|
|
9
|
+
import * as Effect from "effect/Effect";
|
|
10
|
+
import { identity } from "effect/Function";
|
|
11
|
+
import * as Layer from "effect/Layer";
|
|
12
|
+
/**
|
|
13
|
+
* @category type ids
|
|
14
|
+
* @since 1.0.0
|
|
15
|
+
*/
|
|
16
|
+
export const TypeId = /*#__PURE__*/Symbol.for("@effect/sql-d1/D1Client");
|
|
17
|
+
/**
|
|
18
|
+
* @category tags
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
*/
|
|
21
|
+
export const D1Client = /*#__PURE__*/Context.GenericTag("@effect/sql-d1/D1Client");
|
|
22
|
+
/**
|
|
23
|
+
* @category constructor
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
*/
|
|
26
|
+
export const make = options => Effect.gen(function* () {
|
|
27
|
+
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames);
|
|
28
|
+
const transformRows = Statement.defaultTransforms(options.transformResultNames).array;
|
|
29
|
+
const makeConnection = Effect.gen(function* () {
|
|
30
|
+
const db = options.db;
|
|
31
|
+
const prepareCache = yield* Cache.make({
|
|
32
|
+
capacity: options.prepareCacheSize ?? 200,
|
|
33
|
+
timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
|
|
34
|
+
lookup: sql => Effect.try({
|
|
35
|
+
try: () => db.prepare(sql),
|
|
36
|
+
catch: error => new SqlError({
|
|
37
|
+
error
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
});
|
|
41
|
+
const runStatement = (statement, params = []) => Effect.tryPromise({
|
|
42
|
+
try: async () => {
|
|
43
|
+
const response = await statement.bind(...params).all();
|
|
44
|
+
if (response.error) {
|
|
45
|
+
throw response.error;
|
|
46
|
+
}
|
|
47
|
+
return response.results || [];
|
|
48
|
+
},
|
|
49
|
+
catch: error => new SqlError({
|
|
50
|
+
error
|
|
51
|
+
})
|
|
52
|
+
});
|
|
53
|
+
const run = (sql, params = []) => Effect.flatMap(prepareCache.get(sql), s => runStatement(s, params));
|
|
54
|
+
const runRaw = (sql, params = []) => Effect.map(runStatement(db.prepare(sql), params), transformRows);
|
|
55
|
+
const runTransform = options.transformResultNames ? (sql, params) => Effect.map(run(sql, params), transformRows) : run;
|
|
56
|
+
const runValues = (sql, params) => Effect.flatMap(prepareCache.get(sql), statement => Effect.tryPromise({
|
|
57
|
+
try: () => {
|
|
58
|
+
return statement.bind(...params).raw();
|
|
59
|
+
},
|
|
60
|
+
catch: error => new SqlError({
|
|
61
|
+
error
|
|
62
|
+
})
|
|
63
|
+
}));
|
|
64
|
+
return identity({
|
|
65
|
+
execute(sql, params) {
|
|
66
|
+
return runTransform(sql, params);
|
|
67
|
+
},
|
|
68
|
+
executeValues(sql, params) {
|
|
69
|
+
return runValues(sql, params);
|
|
70
|
+
},
|
|
71
|
+
executeWithoutTransform(sql, params) {
|
|
72
|
+
return run(sql, params);
|
|
73
|
+
},
|
|
74
|
+
executeRaw(sql, params) {
|
|
75
|
+
return runRaw(sql, params);
|
|
76
|
+
},
|
|
77
|
+
executeStream(_sql, _params) {
|
|
78
|
+
return Effect.dieMessage("executeStream not implemented");
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
const connection = yield* makeConnection;
|
|
83
|
+
const acquirer = Effect.succeed(connection);
|
|
84
|
+
const transactionAcquirer = Effect.dieMessage("transactions are not supported in D1");
|
|
85
|
+
return Object.assign(Client.make({
|
|
86
|
+
acquirer,
|
|
87
|
+
compiler,
|
|
88
|
+
transactionAcquirer,
|
|
89
|
+
spanAttributes: [...(options.spanAttributes ? Object.entries(options.spanAttributes) : []), [Otel.SEMATTRS_DB_SYSTEM, Otel.DBSYSTEMVALUES_SQLITE]]
|
|
90
|
+
}), {
|
|
91
|
+
[TypeId]: TypeId,
|
|
92
|
+
config: options
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
/**
|
|
96
|
+
* @category layers
|
|
97
|
+
* @since 1.0.0
|
|
98
|
+
*/
|
|
99
|
+
export const layer = config => Layer.scopedContext(Config.unwrap(config).pipe(Effect.flatMap(make), Effect.map(client => Context.make(D1Client, client).pipe(Context.add(Client.SqlClient, client)))));
|
|
100
|
+
//# sourceMappingURL=D1Client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"D1Client.js","names":["Client","SqlError","Statement","Otel","Cache","Config","Context","Duration","Effect","identity","Layer","TypeId","Symbol","for","D1Client","GenericTag","make","options","gen","compiler","makeCompilerSqlite","transformQueryNames","transformRows","defaultTransforms","transformResultNames","array","makeConnection","db","prepareCache","capacity","prepareCacheSize","timeToLive","prepareCacheTTL","minutes","lookup","sql","try","prepare","catch","error","runStatement","statement","params","tryPromise","response","bind","all","results","run","flatMap","get","s","runRaw","map","runTransform","runValues","raw","execute","executeValues","executeWithoutTransform","executeRaw","executeStream","_sql","_params","dieMessage","connection","acquirer","succeed","transactionAcquirer","Object","assign","spanAttributes","entries","SEMATTRS_DB_SYSTEM","DBSYSTEMVALUES_SQLITE","config","layer","scopedContext","unwrap","pipe","client","add","SqlClient"],"sources":["../../src/D1Client.ts"],"sourcesContent":[null],"mappings":"AAIA,OAAO,KAAKA,MAAM,MAAM,uBAAuB;AAE/C,SAASC,QAAQ,QAAQ,sBAAsB;AAC/C,OAAO,KAAKC,SAAS,MAAM,uBAAuB;AAClD,OAAO,KAAKC,IAAI,MAAM,qCAAqC;AAC3D,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AAEvC,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;AAGrC;;;;AAIA,OAAO,MAAMC,MAAM,gBAAkBC,MAAM,CAACC,GAAG,CAAC,yBA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E2B,MAAM,IAChB1E,OAAO,CAACU,IAAI,CAACF,QAAQ,EAAEkE,MAAM,CAAC,CAACD,IAAI,CACjCzE,OAAO,CAAC2E,GAAG,CAACjF,MAAM,CAACkF,SAAS,EAAEF,MAAM,CAAC,CACtC,CACF,CACF,CACF","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["D1Client"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,QAAQ,MAAM,eAAe","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect/sql-d1",
|
|
3
|
+
"version": "0.0.0-snapshot-5fe45984d8022d8f8001e1057a5bacdd3e510676",
|
|
4
|
+
"description": "A Cloudflare D1 integration for Effect",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/Effect-TS/effect.git",
|
|
9
|
+
"directory": "packages/sql-d1"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": [],
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@cloudflare/workers-types": "^4.20240620.0",
|
|
14
|
+
"@opentelemetry/semantic-conventions": "^1.24.1"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@effect/platform": "^0.0.0-snapshot-5fe45984d8022d8f8001e1057a5bacdd3e510676",
|
|
18
|
+
"effect": "^0.0.0-snapshot-5fe45984d8022d8f8001e1057a5bacdd3e510676",
|
|
19
|
+
"@effect/sql": "^0.0.0-snapshot-5fe45984d8022d8f8001e1057a5bacdd3e510676"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"provenance": true
|
|
23
|
+
},
|
|
24
|
+
"main": "./dist/cjs/index.js",
|
|
25
|
+
"module": "./dist/esm/index.js",
|
|
26
|
+
"types": "./dist/dts/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
"./package.json": "./package.json",
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/dts/index.d.ts",
|
|
31
|
+
"import": "./dist/esm/index.js",
|
|
32
|
+
"default": "./dist/cjs/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./D1Client": {
|
|
35
|
+
"types": "./dist/dts/D1Client.d.ts",
|
|
36
|
+
"import": "./dist/esm/D1Client.js",
|
|
37
|
+
"default": "./dist/cjs/D1Client.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"typesVersions": {
|
|
41
|
+
"*": {
|
|
42
|
+
"D1Client": [
|
|
43
|
+
"./dist/dts/D1Client.d.ts"
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/D1Client.ts
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type { D1Database, D1PreparedStatement } from "@cloudflare/workers-types"
|
|
5
|
+
import * as Client from "@effect/sql/SqlClient"
|
|
6
|
+
import type { Connection } from "@effect/sql/SqlConnection"
|
|
7
|
+
import { SqlError } from "@effect/sql/SqlError"
|
|
8
|
+
import * as Statement from "@effect/sql/Statement"
|
|
9
|
+
import * as Otel from "@opentelemetry/semantic-conventions"
|
|
10
|
+
import * as Cache from "effect/Cache"
|
|
11
|
+
import * as Config from "effect/Config"
|
|
12
|
+
import type { ConfigError } from "effect/ConfigError"
|
|
13
|
+
import * as Context from "effect/Context"
|
|
14
|
+
import * as Duration from "effect/Duration"
|
|
15
|
+
import * as Effect from "effect/Effect"
|
|
16
|
+
import { identity } from "effect/Function"
|
|
17
|
+
import * as Layer from "effect/Layer"
|
|
18
|
+
import type * as Scope from "effect/Scope"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @category type ids
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export const TypeId: unique symbol = Symbol.for("@effect/sql-d1/D1Client")
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @category type ids
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
*/
|
|
30
|
+
export type TypeId = typeof TypeId
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @category models
|
|
34
|
+
* @since 1.0.0
|
|
35
|
+
*/
|
|
36
|
+
export interface D1Client extends Client.SqlClient {
|
|
37
|
+
readonly [TypeId]: TypeId
|
|
38
|
+
readonly config: D1ClientConfig
|
|
39
|
+
|
|
40
|
+
/** Not supported in d1 */
|
|
41
|
+
readonly updateValues: never
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @category tags
|
|
46
|
+
* @since 1.0.0
|
|
47
|
+
*/
|
|
48
|
+
export const D1Client = Context.GenericTag<D1Client>("@effect/sql-d1/D1Client")
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @category models
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
*/
|
|
54
|
+
export interface D1ClientConfig {
|
|
55
|
+
readonly db: D1Database
|
|
56
|
+
readonly prepareCacheSize?: number | undefined
|
|
57
|
+
readonly prepareCacheTTL?: Duration.DurationInput | undefined
|
|
58
|
+
readonly spanAttributes?: Record<string, unknown> | undefined
|
|
59
|
+
|
|
60
|
+
readonly transformResultNames?: ((str: string) => string) | undefined
|
|
61
|
+
readonly transformQueryNames?: ((str: string) => string) | undefined
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @category constructor
|
|
66
|
+
* @since 1.0.0
|
|
67
|
+
*/
|
|
68
|
+
export const make = (
|
|
69
|
+
options: D1ClientConfig
|
|
70
|
+
): Effect.Effect<D1Client, never, Scope.Scope> =>
|
|
71
|
+
Effect.gen(function*() {
|
|
72
|
+
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
|
|
73
|
+
const transformRows = Statement.defaultTransforms(
|
|
74
|
+
options.transformResultNames!
|
|
75
|
+
).array
|
|
76
|
+
|
|
77
|
+
const makeConnection = Effect.gen(function*() {
|
|
78
|
+
const db = options.db
|
|
79
|
+
|
|
80
|
+
const prepareCache = yield* Cache.make({
|
|
81
|
+
capacity: options.prepareCacheSize ?? 200,
|
|
82
|
+
timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
|
|
83
|
+
lookup: (sql: string) =>
|
|
84
|
+
Effect.try({
|
|
85
|
+
try: () => db.prepare(sql),
|
|
86
|
+
catch: (error) => new SqlError({ error })
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
const runStatement = (
|
|
91
|
+
statement: D1PreparedStatement,
|
|
92
|
+
params: ReadonlyArray<Statement.Primitive> = []
|
|
93
|
+
) =>
|
|
94
|
+
Effect.tryPromise({
|
|
95
|
+
try: async () => {
|
|
96
|
+
const response = await statement.bind(...params).all()
|
|
97
|
+
if (response.error) {
|
|
98
|
+
throw response.error
|
|
99
|
+
}
|
|
100
|
+
return response.results || []
|
|
101
|
+
},
|
|
102
|
+
catch: (error) => new SqlError({ error })
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const run = (
|
|
106
|
+
sql: string,
|
|
107
|
+
params: ReadonlyArray<Statement.Primitive> = []
|
|
108
|
+
) => Effect.flatMap(prepareCache.get(sql), (s) => runStatement(s, params))
|
|
109
|
+
|
|
110
|
+
const runRaw = (
|
|
111
|
+
sql: string,
|
|
112
|
+
params: ReadonlyArray<Statement.Primitive> = []
|
|
113
|
+
) => Effect.map(runStatement(db.prepare(sql), params), transformRows)
|
|
114
|
+
|
|
115
|
+
const runTransform = options.transformResultNames
|
|
116
|
+
? (sql: string, params?: ReadonlyArray<Statement.Primitive>) => Effect.map(run(sql, params), transformRows)
|
|
117
|
+
: run
|
|
118
|
+
|
|
119
|
+
const runValues = (
|
|
120
|
+
sql: string,
|
|
121
|
+
params: ReadonlyArray<Statement.Primitive>
|
|
122
|
+
) =>
|
|
123
|
+
Effect.flatMap(
|
|
124
|
+
prepareCache.get(sql),
|
|
125
|
+
(statement) =>
|
|
126
|
+
Effect.tryPromise({
|
|
127
|
+
try: () => {
|
|
128
|
+
return statement.bind(...params).raw() as Promise<
|
|
129
|
+
ReadonlyArray<
|
|
130
|
+
ReadonlyArray<Statement.Primitive>
|
|
131
|
+
>
|
|
132
|
+
>
|
|
133
|
+
},
|
|
134
|
+
catch: (error) => new SqlError({ error })
|
|
135
|
+
})
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
return identity<Connection>({
|
|
139
|
+
execute(sql, params) {
|
|
140
|
+
return runTransform(sql, params)
|
|
141
|
+
},
|
|
142
|
+
executeValues(sql, params) {
|
|
143
|
+
return runValues(sql, params)
|
|
144
|
+
},
|
|
145
|
+
executeWithoutTransform(sql, params) {
|
|
146
|
+
return run(sql, params)
|
|
147
|
+
},
|
|
148
|
+
executeRaw(sql, params) {
|
|
149
|
+
return runRaw(sql, params)
|
|
150
|
+
},
|
|
151
|
+
executeStream(_sql, _params) {
|
|
152
|
+
return Effect.dieMessage("executeStream not implemented")
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
const connection = yield* makeConnection
|
|
158
|
+
const acquirer = Effect.succeed(connection)
|
|
159
|
+
const transactionAcquirer = Effect.dieMessage("transactions are not supported in D1")
|
|
160
|
+
|
|
161
|
+
return Object.assign(
|
|
162
|
+
Client.make({
|
|
163
|
+
acquirer,
|
|
164
|
+
compiler,
|
|
165
|
+
transactionAcquirer,
|
|
166
|
+
spanAttributes: [
|
|
167
|
+
...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
|
|
168
|
+
[Otel.SEMATTRS_DB_SYSTEM, Otel.DBSYSTEMVALUES_SQLITE]
|
|
169
|
+
]
|
|
170
|
+
}) as D1Client,
|
|
171
|
+
{
|
|
172
|
+
[TypeId]: TypeId as TypeId,
|
|
173
|
+
config: options
|
|
174
|
+
}
|
|
175
|
+
)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @category layers
|
|
180
|
+
* @since 1.0.0
|
|
181
|
+
*/
|
|
182
|
+
export const layer = (
|
|
183
|
+
config: Config.Config.Wrap<D1ClientConfig>
|
|
184
|
+
): Layer.Layer<D1Client | Client.SqlClient, ConfigError> =>
|
|
185
|
+
Layer.scopedContext(
|
|
186
|
+
Config.unwrap(config).pipe(
|
|
187
|
+
Effect.flatMap(make),
|
|
188
|
+
Effect.map((client) =>
|
|
189
|
+
Context.make(D1Client, client).pipe(
|
|
190
|
+
Context.add(Client.SqlClient, client)
|
|
191
|
+
)
|
|
192
|
+
)
|
|
193
|
+
)
|
|
194
|
+
)
|
package/src/index.ts
ADDED