@effect/sql-sqlite-do 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/SqliteClient.d.ts +58 -17
- package/dist/SqliteClient.d.ts.map +1 -1
- package/dist/SqliteClient.js +89 -24
- package/dist/SqliteClient.js.map +1 -1
- package/dist/SqliteMigrator.d.ts +33 -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 +172 -48
- package/src/SqliteMigrator.ts +33 -6
- package/src/index.ts +3 -3
package/dist/SqliteClient.d.ts
CHANGED
|
@@ -1,27 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Connects Effect SQL to SQLite storage inside Cloudflare Durable Objects.
|
|
3
|
+
*
|
|
4
|
+
* This module adapts a Durable Object `SqlStorage` handle into both the
|
|
5
|
+
* Durable Object-specific `SqliteClient` service and the generic Effect
|
|
6
|
+
* `SqlClient` service. Use it from inside a Durable Object to run local
|
|
7
|
+
* per-object queries, repositories, migrations, transactional read/write
|
|
8
|
+
* workflows, and tests that exercise Cloudflare's SQLite-backed storage API.
|
|
9
|
+
*
|
|
10
|
+
* Durable Object SQLite storage is scoped to one object id, so each object
|
|
11
|
+
* instance has its own database. Callers can pass the `SqlStorage` handle for
|
|
12
|
+
* normal queries, or the full `DurableObjectStorage` when `withTransaction` or
|
|
13
|
+
* migrations need Cloudflare-managed transactions. This adapter serializes
|
|
14
|
+
* Effect SQL access through one connection; a transaction holds that permit for
|
|
15
|
+
* the lifetime of its scope, so keep transactions short, avoid suspending them
|
|
16
|
+
* across unrelated work, and use them when multi-statement writes must commit
|
|
17
|
+
* atomically. `SqlStorage.exec` returns `ArrayBuffer` values
|
|
18
|
+
* for SQLite blobs, which this client normalizes to `Uint8Array`, and SQLite
|
|
19
|
+
* does not support `updateValues`.
|
|
20
|
+
*
|
|
21
|
+
* @since 4.0.0
|
|
3
22
|
*/
|
|
4
|
-
import type { SqlStorage } from "@cloudflare/workers-types";
|
|
23
|
+
import type { DurableObjectStorage, SqlStorage } from "@cloudflare/workers-types";
|
|
5
24
|
import * as Config from "effect/Config";
|
|
25
|
+
import * as Context from "effect/Context";
|
|
6
26
|
import * as Effect from "effect/Effect";
|
|
7
27
|
import * as Layer from "effect/Layer";
|
|
8
28
|
import * as Scope from "effect/Scope";
|
|
9
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
10
29
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
11
30
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
12
31
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
32
|
+
* Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
33
|
+
*
|
|
34
|
+
* @category type IDs
|
|
35
|
+
* @since 4.0.0
|
|
15
36
|
*/
|
|
16
37
|
export declare const TypeId: TypeId;
|
|
17
38
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
39
|
+
* Type-level identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
40
|
+
*
|
|
41
|
+
* @category type IDs
|
|
42
|
+
* @since 4.0.0
|
|
20
43
|
*/
|
|
21
44
|
export type TypeId = "~@effect/sql-sqlite-do/SqliteClient";
|
|
22
45
|
/**
|
|
46
|
+
* Cloudflare Durable Object SQLite client service, extending `SqlClient` with its configuration. `updateValues` is not supported.
|
|
47
|
+
*
|
|
23
48
|
* @category models
|
|
24
|
-
* @since
|
|
49
|
+
* @since 4.0.0
|
|
25
50
|
*/
|
|
26
51
|
export interface SqliteClient extends Client.SqlClient {
|
|
27
52
|
readonly [TypeId]: TypeId;
|
|
@@ -30,33 +55,49 @@ export interface SqliteClient extends Client.SqlClient {
|
|
|
30
55
|
readonly updateValues: never;
|
|
31
56
|
}
|
|
32
57
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
58
|
+
* Service tag for the Cloudflare Durable Object SQLite client service.
|
|
59
|
+
*
|
|
60
|
+
* **When to use**
|
|
61
|
+
*
|
|
62
|
+
* Use to access or provide a Durable Object SQLite client through the Effect
|
|
63
|
+
* context.
|
|
64
|
+
*
|
|
65
|
+
* @category services
|
|
66
|
+
* @since 4.0.0
|
|
35
67
|
*/
|
|
36
|
-
export declare const SqliteClient:
|
|
68
|
+
export declare const SqliteClient: Context.Service<SqliteClient, SqliteClient>;
|
|
37
69
|
/**
|
|
70
|
+
* Configuration for a Cloudflare Durable Object SQLite client, including either a `SqlStorage` handle or the full `DurableObjectStorage` for transaction support, span attributes, and query/result name transforms.
|
|
71
|
+
*
|
|
38
72
|
* @category models
|
|
39
|
-
* @since
|
|
73
|
+
* @since 4.0.0
|
|
40
74
|
*/
|
|
41
75
|
export interface SqliteClientConfig {
|
|
42
|
-
readonly db
|
|
76
|
+
readonly db?: SqlStorage | undefined;
|
|
77
|
+
readonly storage?: DurableObjectStorage | undefined;
|
|
43
78
|
readonly spanAttributes?: Record<string, unknown> | undefined;
|
|
44
79
|
readonly transformResultNames?: ((str: string) => string) | undefined;
|
|
45
80
|
readonly transformQueryNames?: ((str: string) => string) | undefined;
|
|
46
81
|
}
|
|
47
82
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
83
|
+
* Creates a scoped Cloudflare Durable Object SQLite client around Durable Object SQLite storage, serializing access and converting returned `ArrayBuffer` values to `Uint8Array`.
|
|
84
|
+
*
|
|
85
|
+
* @category constructors
|
|
86
|
+
* @since 4.0.0
|
|
50
87
|
*/
|
|
51
88
|
export declare const make: (options: SqliteClientConfig) => Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity>;
|
|
52
89
|
/**
|
|
90
|
+
* Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
91
|
+
*
|
|
53
92
|
* @category layers
|
|
54
|
-
* @since
|
|
93
|
+
* @since 4.0.0
|
|
55
94
|
*/
|
|
56
95
|
export declare const layerConfig: (config: Config.Wrap<SqliteClientConfig>) => Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError>;
|
|
57
96
|
/**
|
|
97
|
+
* Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
98
|
+
*
|
|
58
99
|
* @category layers
|
|
59
|
-
* @since
|
|
100
|
+
* @since 4.0.0
|
|
60
101
|
*/
|
|
61
102
|
export declare const layer: (config: SqliteClientConfig) => Layer.Layer<SqliteClient | Client.SqlClient>;
|
|
62
103
|
//# 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;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,EAAE,oBAAoB,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AACjF,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAIvC,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,YAAa,SAAQ,MAAM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.js
CHANGED
|
@@ -1,38 +1,95 @@
|
|
|
1
1
|
import * as Config from "effect/Config";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
2
3
|
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Exit from "effect/Exit";
|
|
3
5
|
import * as Fiber from "effect/Fiber";
|
|
4
6
|
import { identity } from "effect/Function";
|
|
5
7
|
import * as Layer from "effect/Layer";
|
|
6
8
|
import * as Scope from "effect/Scope";
|
|
7
9
|
import * as Semaphore from "effect/Semaphore";
|
|
8
|
-
import * as ServiceMap from "effect/ServiceMap";
|
|
9
10
|
import * as Stream from "effect/Stream";
|
|
10
11
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity";
|
|
11
12
|
import * as Client from "effect/unstable/sql/SqlClient";
|
|
12
|
-
import { SqlError } from "effect/unstable/sql/SqlError";
|
|
13
|
+
import { classifySqliteError, SqlError, UnknownError } from "effect/unstable/sql/SqlError";
|
|
13
14
|
import * as Statement from "effect/unstable/sql/Statement";
|
|
14
15
|
const ATTR_DB_SYSTEM_NAME = "db.system.name";
|
|
16
|
+
const classifyError = (cause, message, operation) => classifySqliteError(cause, {
|
|
17
|
+
message,
|
|
18
|
+
operation
|
|
19
|
+
});
|
|
15
20
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
21
|
+
* Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
22
|
+
*
|
|
23
|
+
* @category type IDs
|
|
24
|
+
* @since 4.0.0
|
|
18
25
|
*/
|
|
19
26
|
export const TypeId = "~@effect/sql-sqlite-do/SqliteClient";
|
|
20
27
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
28
|
+
* Service tag for the Cloudflare Durable Object SQLite client service.
|
|
29
|
+
*
|
|
30
|
+
* **When to use**
|
|
31
|
+
*
|
|
32
|
+
* Use to access or provide a Durable Object SQLite client through the Effect
|
|
33
|
+
* context.
|
|
34
|
+
*
|
|
35
|
+
* @category services
|
|
36
|
+
* @since 4.0.0
|
|
23
37
|
*/
|
|
24
|
-
export const SqliteClient = /*#__PURE__*/
|
|
38
|
+
export const SqliteClient = /*#__PURE__*/Context.Service("@effect/sql-sqlite-do/SqliteClient");
|
|
39
|
+
const SqliteTransaction = /*#__PURE__*/Context.Service("@effect/sql-sqlite-do/SqliteClient/SqliteTransaction");
|
|
40
|
+
const unsupportedTransaction = (message, operation) => new SqlError({
|
|
41
|
+
reason: new UnknownError({
|
|
42
|
+
cause: new Error(message),
|
|
43
|
+
message,
|
|
44
|
+
operation
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
const makeUnsupportedWithTransaction = message => _effect => Effect.fail(unsupportedTransaction(message, "transaction"));
|
|
48
|
+
const makeStorageBackedWithTransaction = (storage, connection, semaphore) => effect => Effect.withFiber(fiber => {
|
|
49
|
+
const services = fiber.context;
|
|
50
|
+
const connOption = Context.getOption(services, SqliteTransaction);
|
|
51
|
+
if (connOption._tag === "Some") {
|
|
52
|
+
return Effect.fail(unsupportedTransaction("Nested transactions are not supported by Cloudflare Durable Object SQLite storage", "transaction"));
|
|
53
|
+
}
|
|
54
|
+
const effectWithTxn = Effect.provideContext(effect, Context.add(services, SqliteTransaction, [connection, 0]));
|
|
55
|
+
return semaphore.withPermits(1)(Effect.callback(resume => {
|
|
56
|
+
let interrupted = false;
|
|
57
|
+
const promise = storage.transaction(txn => new Promise(resolve => {
|
|
58
|
+
if (interrupted) return resolve();
|
|
59
|
+
resume(Effect.onExit(effectWithTxn, exit => {
|
|
60
|
+
if (Exit.isFailure(exit)) {
|
|
61
|
+
txn.rollback();
|
|
62
|
+
}
|
|
63
|
+
resolve();
|
|
64
|
+
// wait for the transaction to complete
|
|
65
|
+
return Effect.promise(() => promise);
|
|
66
|
+
}));
|
|
67
|
+
})).catch(cause => resume(Effect.fail(new SqlError({
|
|
68
|
+
reason: classifyError(cause, "Failed transaction", "transaction")
|
|
69
|
+
}))));
|
|
70
|
+
return Effect.suspend(() => {
|
|
71
|
+
interrupted = true;
|
|
72
|
+
return Effect.promise(() => promise);
|
|
73
|
+
});
|
|
74
|
+
}));
|
|
75
|
+
});
|
|
25
76
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
77
|
+
* Creates a scoped Cloudflare Durable Object SQLite client around Durable Object SQLite storage, serializing access and converting returned `ArrayBuffer` values to `Uint8Array`.
|
|
78
|
+
*
|
|
79
|
+
* @category constructors
|
|
80
|
+
* @since 4.0.0
|
|
28
81
|
*/
|
|
29
82
|
export const make = options => Effect.gen(function* () {
|
|
30
83
|
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames);
|
|
31
84
|
const transformRows = options.transformResultNames ? Statement.defaultTransforms(options.transformResultNames).array : undefined;
|
|
85
|
+
const db = options.storage?.sql ?? options.db;
|
|
86
|
+
if (db === undefined) {
|
|
87
|
+
return yield* Effect.die("SqliteClient.make requires either a Durable Object storage or sql storage");
|
|
88
|
+
}
|
|
89
|
+
const sqlStorage = db;
|
|
32
90
|
const makeConnection = Effect.gen(function* () {
|
|
33
|
-
const db = options.db;
|
|
34
91
|
function* runIterator(sql, params = []) {
|
|
35
|
-
const cursor =
|
|
92
|
+
const cursor = sqlStorage.exec(sql, ...params);
|
|
36
93
|
const columns = cursor.columnNames;
|
|
37
94
|
for (const result of cursor.raw()) {
|
|
38
95
|
const obj = {};
|
|
@@ -46,12 +103,11 @@ export const make = options => Effect.gen(function* () {
|
|
|
46
103
|
const runStatement = (sql, params = []) => Effect.try({
|
|
47
104
|
try: () => Array.from(runIterator(sql, params)),
|
|
48
105
|
catch: cause => new SqlError({
|
|
49
|
-
cause,
|
|
50
|
-
message: `Failed to execute statement`
|
|
106
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
51
107
|
})
|
|
52
108
|
});
|
|
53
109
|
const runValues = (sql, params = []) => Effect.try({
|
|
54
|
-
try: () => Array.from(
|
|
110
|
+
try: () => Array.from(sqlStorage.exec(sql, ...params).raw(), row => {
|
|
55
111
|
for (let i = 0; i < row.length; i++) {
|
|
56
112
|
const value = row[i];
|
|
57
113
|
if (value instanceof ArrayBuffer) {
|
|
@@ -61,8 +117,7 @@ export const make = options => Effect.gen(function* () {
|
|
|
61
117
|
return row;
|
|
62
118
|
}),
|
|
63
119
|
catch: cause => new SqlError({
|
|
64
|
-
cause,
|
|
65
|
-
message: `Failed to execute statement`
|
|
120
|
+
reason: classifyError(cause, "Failed to execute statement", "execute")
|
|
66
121
|
})
|
|
67
122
|
});
|
|
68
123
|
return identity({
|
|
@@ -75,6 +130,9 @@ export const make = options => Effect.gen(function* () {
|
|
|
75
130
|
executeValues(sql, params) {
|
|
76
131
|
return runValues(sql, params);
|
|
77
132
|
},
|
|
133
|
+
executeValuesUnprepared(sql, params) {
|
|
134
|
+
return runValues(sql, params);
|
|
135
|
+
},
|
|
78
136
|
executeUnprepared(sql, params, transformRows) {
|
|
79
137
|
return transformRows ? Effect.map(runStatement(sql, params), transformRows) : runStatement(sql, params);
|
|
80
138
|
},
|
|
@@ -91,28 +149,35 @@ export const make = options => Effect.gen(function* () {
|
|
|
91
149
|
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
|
|
92
150
|
const transactionAcquirer = Effect.uninterruptibleMask(restore => {
|
|
93
151
|
const fiber = Fiber.getCurrent();
|
|
94
|
-
const scope =
|
|
152
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope);
|
|
95
153
|
return Effect.as(Effect.tap(restore(semaphore.take(1)), () => Scope.addFinalizer(scope, semaphore.release(1))), connection);
|
|
96
154
|
});
|
|
97
|
-
|
|
155
|
+
const client = yield* Client.make({
|
|
98
156
|
acquirer,
|
|
99
157
|
compiler,
|
|
100
158
|
transactionAcquirer,
|
|
159
|
+
transactionService: SqliteTransaction,
|
|
101
160
|
spanAttributes: [...(options.spanAttributes ? Object.entries(options.spanAttributes) : []), [ATTR_DB_SYSTEM_NAME, "sqlite"]],
|
|
102
161
|
transformRows
|
|
103
|
-
})
|
|
162
|
+
});
|
|
163
|
+
return Object.assign(client, {
|
|
104
164
|
[TypeId]: TypeId,
|
|
105
|
-
config: options
|
|
165
|
+
config: options,
|
|
166
|
+
withTransaction: options.storage ? makeStorageBackedWithTransaction(options.storage, connection, semaphore) : makeUnsupportedWithTransaction("Transactions require Durable Object storage; pass ctx.storage as the storage option")
|
|
106
167
|
});
|
|
107
168
|
});
|
|
108
169
|
/**
|
|
170
|
+
* Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
171
|
+
*
|
|
109
172
|
* @category layers
|
|
110
|
-
* @since
|
|
173
|
+
* @since 4.0.0
|
|
111
174
|
*/
|
|
112
|
-
export const layerConfig = config => Layer.
|
|
175
|
+
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
176
|
/**
|
|
177
|
+
* Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
178
|
+
*
|
|
114
179
|
* @category layers
|
|
115
|
-
* @since
|
|
180
|
+
* @since 4.0.0
|
|
116
181
|
*/
|
|
117
|
-
export const layer = config => Layer.
|
|
182
|
+
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
183
|
//# 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","Exit","Fiber","identity","Layer","Scope","Semaphore","Stream","Reactivity","Client","classifySqliteError","SqlError","UnknownError","Statement","ATTR_DB_SYSTEM_NAME","classifyError","cause","message","operation","TypeId","SqliteClient","Service","SqliteTransaction","unsupportedTransaction","reason","Error","makeUnsupportedWithTransaction","_effect","fail","makeStorageBackedWithTransaction","storage","connection","semaphore","effect","withFiber","fiber","services","context","connOption","getOption","_tag","effectWithTxn","provideContext","add","withPermits","callback","resume","interrupted","promise","transaction","txn","Promise","resolve","onExit","exit","isFailure","rollback","catch","suspend","make","options","gen","compiler","makeCompilerSqlite","transformQueryNames","transformRows","transformResultNames","defaultTransforms","array","undefined","db","sql","die","sqlStorage","makeConnection","runIterator","params","cursor","exec","columns","columnNames","result","raw","obj","i","length","value","ArrayBuffer","Uint8Array","runStatement","try","Array","from","runValues","row","execute","map","executeRaw","executeValues","executeValuesUnprepared","executeUnprepared","executeStream","iterator","fromIteratorSucceed","pipe","mapArray","chunk","acquirer","succeed","transactionAcquirer","uninterruptibleMask","restore","getCurrent","scope","getUnsafe","as","tap","take","addFinalizer","release","client","transactionService","spanAttributes","Object","entries","assign","config","withTransaction","layerConfig","effectContext","unwrap","flatMap","SqlClient","provide","layer"],"sources":["../src/SqliteClient.ts"],"sourcesContent":[null],"mappings":"AAuBA,OAAO,KAAKA,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,IAAI,MAAM,aAAa;AACnC,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,EAAEC,YAAY,QAAQ,8BAA8B;AAC1F,OAAO,KAAKC,SAAS,MAAM,+BAA+B;AAE1D,MAAMC,mBAAmB,GAAG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ACrCzG,OAAO,CAAC4C,GAAG,CAAClC,MAAM,CAAC2H,SAAS,EAAEZ,MAAM,CAAC,CACtC,CACF,CACF,CACF,CAAChB,IAAI,CAACpG,KAAK,CAACiI,OAAO,CAAC7H,UAAU,CAAC8H,KAAK,CAAC,CAAC;AAEzC;;;;;;AAMA,OAAO,MAAMA,KAAK,GAChBR,MAA0B,IAE1B1H,KAAK,CAAC6H,aAAa,CACjBjI,MAAM,CAACgG,GAAG,CAACrC,IAAI,CAACmE,MAAM,CAAC,EAAGN,MAAM,IAC9BzH,OAAO,CAAC4D,IAAI,CAACvC,YAAY,EAAEoG,MAAM,CAAC,CAAChB,IAAI,CACrCzG,OAAO,CAAC4C,GAAG,CAAClC,MAAM,CAAC2H,SAAS,EAAEZ,MAAM,CAAC,CACtC,CAAC,CACL,CAAChB,IAAI,CAACpG,KAAK,CAACiI,OAAO,CAAC7H,UAAU,CAAC8H,KAAK,CAAC,CAAC","ignoreList":[]}
|
package/dist/SqliteMigrator.d.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Runs database migrations for Durable Object SQLite storage that uses Effect
|
|
3
|
+
* SQL.
|
|
4
|
+
*
|
|
5
|
+
* This module re-exports the shared `Migrator` loaders and error types, then
|
|
6
|
+
* provides `run` and `layer` helpers that execute ordered migrations through the
|
|
7
|
+
* current Durable Object SQLite `SqlClient`. Use it when a Durable
|
|
8
|
+
* Object needs to create or upgrade its local schema during construction, before
|
|
9
|
+
* repositories or request handlers use the object storage, or in tests that
|
|
10
|
+
* exercise Durable Object persistence.
|
|
11
|
+
*
|
|
12
|
+
* Migrations are recorded in `effect_sql_migrations` by default and are loaded
|
|
13
|
+
* using the shared `<id>_<name>` file or record-key convention. The underlying
|
|
14
|
+
* storage is scoped to a Durable Object id, so running migrations for one object
|
|
15
|
+
* does not update any other object instance; run the migrator against the same
|
|
16
|
+
* `DurableObjectStorage`-backed client that the object uses for normal queries
|
|
17
|
+
* so migrations can run in Cloudflare-managed transactions. These SQL
|
|
18
|
+
* migrations are separate from Cloudflare's Durable Object class migrations, and
|
|
19
|
+
* the Durable Object must already be configured with SQLite storage before this
|
|
20
|
+
* module can apply schema changes. Repeated startup runs are expected and are
|
|
21
|
+
* guarded by the migrations table, but request handling should wait until the
|
|
22
|
+
* migration layer has finished. This adapter does not currently write SQLite
|
|
23
|
+
* schema dumps for `schemaDirectory`.
|
|
24
|
+
*
|
|
25
|
+
* @since 4.0.0
|
|
3
26
|
*/
|
|
4
27
|
import type * as Effect from "effect/Effect";
|
|
5
28
|
import * as Layer from "effect/Layer";
|
|
@@ -7,17 +30,21 @@ import * as Migrator from "effect/unstable/sql/Migrator";
|
|
|
7
30
|
import type * as Client from "effect/unstable/sql/SqlClient";
|
|
8
31
|
import type { SqlError } from "effect/unstable/sql/SqlError";
|
|
9
32
|
/**
|
|
10
|
-
* @since
|
|
33
|
+
* @since 4.0.0
|
|
11
34
|
*/
|
|
12
35
|
export * from "effect/unstable/sql/Migrator";
|
|
13
36
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
37
|
+
* Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
|
|
38
|
+
*
|
|
39
|
+
* @category constructors
|
|
40
|
+
* @since 4.0.0
|
|
16
41
|
*/
|
|
17
42
|
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
43
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
44
|
+
* Creates a layer that runs the configured SQL migrations during layer construction.
|
|
45
|
+
*
|
|
46
|
+
* @category constructors
|
|
47
|
+
* @since 4.0.0
|
|
21
48
|
*/
|
|
22
49
|
export declare const layer: <R>(options: Migrator.MigratorOptions<R>) => Layer.Layer<never, Migrator.MigrationError | SqlError, Client.SqlClient | R>;
|
|
23
50
|
//# 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;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;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":"AA2BA,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.91",
|
|
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.91"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"effect": "^4.0.0-beta.
|
|
50
|
+
"effect": "^4.0.0-beta.91"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"codegen": "effect-utils codegen",
|
package/src/SqliteClient.ts
CHANGED
|
@@ -1,39 +1,68 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Connects Effect SQL to SQLite storage inside Cloudflare Durable Objects.
|
|
3
|
+
*
|
|
4
|
+
* This module adapts a Durable Object `SqlStorage` handle into both the
|
|
5
|
+
* Durable Object-specific `SqliteClient` service and the generic Effect
|
|
6
|
+
* `SqlClient` service. Use it from inside a Durable Object to run local
|
|
7
|
+
* per-object queries, repositories, migrations, transactional read/write
|
|
8
|
+
* workflows, and tests that exercise Cloudflare's SQLite-backed storage API.
|
|
9
|
+
*
|
|
10
|
+
* Durable Object SQLite storage is scoped to one object id, so each object
|
|
11
|
+
* instance has its own database. Callers can pass the `SqlStorage` handle for
|
|
12
|
+
* normal queries, or the full `DurableObjectStorage` when `withTransaction` or
|
|
13
|
+
* migrations need Cloudflare-managed transactions. This adapter serializes
|
|
14
|
+
* Effect SQL access through one connection; a transaction holds that permit for
|
|
15
|
+
* the lifetime of its scope, so keep transactions short, avoid suspending them
|
|
16
|
+
* across unrelated work, and use them when multi-statement writes must commit
|
|
17
|
+
* atomically. `SqlStorage.exec` returns `ArrayBuffer` values
|
|
18
|
+
* for SQLite blobs, which this client normalizes to `Uint8Array`, and SQLite
|
|
19
|
+
* does not support `updateValues`.
|
|
20
|
+
*
|
|
21
|
+
* @since 4.0.0
|
|
3
22
|
*/
|
|
4
|
-
import type { SqlStorage } from "@cloudflare/workers-types"
|
|
23
|
+
import type { DurableObjectStorage, SqlStorage } from "@cloudflare/workers-types"
|
|
5
24
|
import * as Config from "effect/Config"
|
|
25
|
+
import * as Context from "effect/Context"
|
|
6
26
|
import * as Effect from "effect/Effect"
|
|
27
|
+
import * as Exit from "effect/Exit"
|
|
7
28
|
import * as Fiber from "effect/Fiber"
|
|
8
29
|
import { identity } from "effect/Function"
|
|
9
30
|
import * as Layer from "effect/Layer"
|
|
10
31
|
import * as Scope from "effect/Scope"
|
|
11
32
|
import * as Semaphore from "effect/Semaphore"
|
|
12
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
13
33
|
import * as Stream from "effect/Stream"
|
|
14
34
|
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
|
15
35
|
import * as Client from "effect/unstable/sql/SqlClient"
|
|
16
36
|
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
|
17
|
-
import { SqlError } from "effect/unstable/sql/SqlError"
|
|
37
|
+
import { classifySqliteError, SqlError, UnknownError } from "effect/unstable/sql/SqlError"
|
|
18
38
|
import * as Statement from "effect/unstable/sql/Statement"
|
|
19
39
|
|
|
20
40
|
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
|
21
41
|
|
|
42
|
+
const classifyError = (cause: unknown, message: string, operation: string) =>
|
|
43
|
+
classifySqliteError(cause, { message, operation })
|
|
44
|
+
|
|
22
45
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
46
|
+
* Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
47
|
+
*
|
|
48
|
+
* @category type IDs
|
|
49
|
+
* @since 4.0.0
|
|
25
50
|
*/
|
|
26
51
|
export const TypeId: TypeId = "~@effect/sql-sqlite-do/SqliteClient"
|
|
27
52
|
|
|
28
53
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
54
|
+
* Type-level identifier used to mark Cloudflare Durable Object `SqliteClient` values.
|
|
55
|
+
*
|
|
56
|
+
* @category type IDs
|
|
57
|
+
* @since 4.0.0
|
|
31
58
|
*/
|
|
32
59
|
export type TypeId = "~@effect/sql-sqlite-do/SqliteClient"
|
|
33
60
|
|
|
34
61
|
/**
|
|
62
|
+
* Cloudflare Durable Object SQLite client service, extending `SqlClient` with its configuration. `updateValues` is not supported.
|
|
63
|
+
*
|
|
35
64
|
* @category models
|
|
36
|
-
* @since
|
|
65
|
+
* @since 4.0.0
|
|
37
66
|
*/
|
|
38
67
|
export interface SqliteClient extends Client.SqlClient {
|
|
39
68
|
readonly [TypeId]: TypeId
|
|
@@ -44,26 +73,105 @@ export interface SqliteClient extends Client.SqlClient {
|
|
|
44
73
|
}
|
|
45
74
|
|
|
46
75
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
76
|
+
* Service tag for the Cloudflare Durable Object SQLite client service.
|
|
77
|
+
*
|
|
78
|
+
* **When to use**
|
|
79
|
+
*
|
|
80
|
+
* Use to access or provide a Durable Object SQLite client through the Effect
|
|
81
|
+
* context.
|
|
82
|
+
*
|
|
83
|
+
* @category services
|
|
84
|
+
* @since 4.0.0
|
|
49
85
|
*/
|
|
50
|
-
export const SqliteClient =
|
|
86
|
+
export const SqliteClient = Context.Service<SqliteClient>("@effect/sql-sqlite-do/SqliteClient")
|
|
87
|
+
|
|
88
|
+
const SqliteTransaction = Context.Service<Client.TransactionConnection, Client.TransactionConnection.Service>(
|
|
89
|
+
"@effect/sql-sqlite-do/SqliteClient/SqliteTransaction"
|
|
90
|
+
)
|
|
51
91
|
|
|
52
92
|
/**
|
|
93
|
+
* Configuration for a Cloudflare Durable Object SQLite client, including either a `SqlStorage` handle or the full `DurableObjectStorage` for transaction support, span attributes, and query/result name transforms.
|
|
94
|
+
*
|
|
53
95
|
* @category models
|
|
54
|
-
* @since
|
|
96
|
+
* @since 4.0.0
|
|
55
97
|
*/
|
|
56
98
|
export interface SqliteClientConfig {
|
|
57
|
-
readonly db
|
|
99
|
+
readonly db?: SqlStorage | undefined
|
|
100
|
+
readonly storage?: DurableObjectStorage | undefined
|
|
58
101
|
readonly spanAttributes?: Record<string, unknown> | undefined
|
|
59
102
|
|
|
60
103
|
readonly transformResultNames?: ((str: string) => string) | undefined
|
|
61
104
|
readonly transformQueryNames?: ((str: string) => string) | undefined
|
|
62
105
|
}
|
|
63
106
|
|
|
107
|
+
const unsupportedTransaction = (message: string, operation: string) =>
|
|
108
|
+
new SqlError({
|
|
109
|
+
reason: new UnknownError({
|
|
110
|
+
cause: new Error(message),
|
|
111
|
+
message,
|
|
112
|
+
operation
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
const makeUnsupportedWithTransaction =
|
|
117
|
+
(message: string): Client.SqlClient["withTransaction"] =>
|
|
118
|
+
<R, E, A>(_effect: Effect.Effect<A, E, R>): Effect.Effect<A, E | SqlError, R> =>
|
|
119
|
+
Effect.fail(unsupportedTransaction(message, "transaction"))
|
|
120
|
+
|
|
121
|
+
const makeStorageBackedWithTransaction = (
|
|
122
|
+
storage: DurableObjectStorage,
|
|
123
|
+
connection: Connection,
|
|
124
|
+
semaphore: Semaphore.Semaphore
|
|
125
|
+
): Client.SqlClient["withTransaction"] =>
|
|
126
|
+
<R, E, A>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E | SqlError, R> =>
|
|
127
|
+
Effect.withFiber((fiber) => {
|
|
128
|
+
const services = fiber.context
|
|
129
|
+
const connOption = Context.getOption(services, SqliteTransaction)
|
|
130
|
+
if (connOption._tag === "Some") {
|
|
131
|
+
return Effect.fail(
|
|
132
|
+
unsupportedTransaction(
|
|
133
|
+
"Nested transactions are not supported by Cloudflare Durable Object SQLite storage",
|
|
134
|
+
"transaction"
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const effectWithTxn = Effect.provideContext(
|
|
140
|
+
effect,
|
|
141
|
+
Context.add(services, SqliteTransaction, [connection, 0] as const)
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
return semaphore.withPermits(1)(
|
|
145
|
+
Effect.callback((resume) => {
|
|
146
|
+
let interrupted = false
|
|
147
|
+
const promise = storage.transaction((txn) =>
|
|
148
|
+
new Promise<void>((resolve) => {
|
|
149
|
+
if (interrupted) return resolve()
|
|
150
|
+
resume(Effect.onExit(effectWithTxn, (exit) => {
|
|
151
|
+
if (Exit.isFailure(exit)) {
|
|
152
|
+
txn.rollback()
|
|
153
|
+
}
|
|
154
|
+
resolve()
|
|
155
|
+
// wait for the transaction to complete
|
|
156
|
+
return Effect.promise(() => promise)
|
|
157
|
+
}))
|
|
158
|
+
})
|
|
159
|
+
).catch((cause) =>
|
|
160
|
+
resume(Effect.fail(new SqlError({ reason: classifyError(cause, "Failed transaction", "transaction") })))
|
|
161
|
+
)
|
|
162
|
+
return Effect.suspend(() => {
|
|
163
|
+
interrupted = true
|
|
164
|
+
return Effect.promise(() => promise)
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
)
|
|
168
|
+
})
|
|
169
|
+
|
|
64
170
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
171
|
+
* Creates a scoped Cloudflare Durable Object SQLite client around Durable Object SQLite storage, serializing access and converting returned `ArrayBuffer` values to `Uint8Array`.
|
|
172
|
+
*
|
|
173
|
+
* @category constructors
|
|
174
|
+
* @since 4.0.0
|
|
67
175
|
*/
|
|
68
176
|
export const make = (
|
|
69
177
|
options: SqliteClientConfig
|
|
@@ -73,15 +181,19 @@ export const make = (
|
|
|
73
181
|
const transformRows = options.transformResultNames
|
|
74
182
|
? Statement.defaultTransforms(options.transformResultNames).array
|
|
75
183
|
: undefined
|
|
184
|
+
const db = options.storage?.sql ?? options.db
|
|
76
185
|
|
|
77
|
-
|
|
78
|
-
|
|
186
|
+
if (db === undefined) {
|
|
187
|
+
return yield* Effect.die("SqliteClient.make requires either a Durable Object storage or sql storage")
|
|
188
|
+
}
|
|
189
|
+
const sqlStorage = db
|
|
79
190
|
|
|
191
|
+
const makeConnection = Effect.gen(function*() {
|
|
80
192
|
function* runIterator(
|
|
81
193
|
sql: string,
|
|
82
194
|
params: ReadonlyArray<unknown> = []
|
|
83
195
|
) {
|
|
84
|
-
const cursor =
|
|
196
|
+
const cursor = sqlStorage.exec(sql, ...params)
|
|
85
197
|
const columns = cursor.columnNames
|
|
86
198
|
for (const result of cursor.raw()) {
|
|
87
199
|
const obj: any = {}
|
|
@@ -99,7 +211,7 @@ export const make = (
|
|
|
99
211
|
): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
|
|
100
212
|
Effect.try({
|
|
101
213
|
try: () => Array.from(runIterator(sql, params)),
|
|
102
|
-
catch: (cause) => new SqlError({ cause,
|
|
214
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
103
215
|
})
|
|
104
216
|
|
|
105
217
|
const runValues = (
|
|
@@ -108,7 +220,7 @@ export const make = (
|
|
|
108
220
|
): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
|
|
109
221
|
Effect.try({
|
|
110
222
|
try: () =>
|
|
111
|
-
Array.from(
|
|
223
|
+
Array.from(sqlStorage.exec(sql, ...params).raw(), (row) => {
|
|
112
224
|
for (let i = 0; i < row.length; i++) {
|
|
113
225
|
const value = row[i]
|
|
114
226
|
if (value instanceof ArrayBuffer) {
|
|
@@ -117,7 +229,7 @@ export const make = (
|
|
|
117
229
|
}
|
|
118
230
|
return row
|
|
119
231
|
}),
|
|
120
|
-
catch: (cause) => new SqlError({ cause,
|
|
232
|
+
catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
|
|
121
233
|
})
|
|
122
234
|
|
|
123
235
|
return identity<Connection>({
|
|
@@ -132,6 +244,9 @@ export const make = (
|
|
|
132
244
|
executeValues(sql, params) {
|
|
133
245
|
return runValues(sql, params)
|
|
134
246
|
},
|
|
247
|
+
executeValuesUnprepared(sql, params) {
|
|
248
|
+
return runValues(sql, params)
|
|
249
|
+
},
|
|
135
250
|
executeUnprepared(sql, params, transformRows) {
|
|
136
251
|
return transformRows
|
|
137
252
|
? Effect.map(runStatement(sql, params), transformRows)
|
|
@@ -156,7 +271,7 @@ export const make = (
|
|
|
156
271
|
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
|
157
272
|
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
|
158
273
|
const fiber = Fiber.getCurrent()!
|
|
159
|
-
const scope =
|
|
274
|
+
const scope = Context.getUnsafe(fiber.context, Scope.Scope)
|
|
160
275
|
return Effect.as(
|
|
161
276
|
Effect.tap(
|
|
162
277
|
restore(semaphore.take(1)),
|
|
@@ -166,52 +281,61 @@ export const make = (
|
|
|
166
281
|
)
|
|
167
282
|
})
|
|
168
283
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
284
|
+
const client = (yield* Client.make({
|
|
285
|
+
acquirer,
|
|
286
|
+
compiler,
|
|
287
|
+
transactionAcquirer,
|
|
288
|
+
transactionService: SqliteTransaction,
|
|
289
|
+
spanAttributes: [
|
|
290
|
+
...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
|
|
291
|
+
[ATTR_DB_SYSTEM_NAME, "sqlite"]
|
|
292
|
+
],
|
|
293
|
+
transformRows
|
|
294
|
+
})) as SqliteClient
|
|
295
|
+
|
|
296
|
+
return Object.assign(client, {
|
|
297
|
+
[TypeId]: TypeId as TypeId,
|
|
298
|
+
config: options,
|
|
299
|
+
withTransaction: options.storage
|
|
300
|
+
? makeStorageBackedWithTransaction(options.storage, connection, semaphore)
|
|
301
|
+
: makeUnsupportedWithTransaction(
|
|
302
|
+
"Transactions require Durable Object storage; pass ctx.storage as the storage option"
|
|
303
|
+
)
|
|
304
|
+
})
|
|
185
305
|
})
|
|
186
306
|
|
|
187
307
|
/**
|
|
308
|
+
* Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
309
|
+
*
|
|
188
310
|
* @category layers
|
|
189
|
-
* @since
|
|
311
|
+
* @since 4.0.0
|
|
190
312
|
*/
|
|
191
313
|
export const layerConfig = (
|
|
192
314
|
config: Config.Wrap<SqliteClientConfig>
|
|
193
315
|
): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError> =>
|
|
194
|
-
Layer.
|
|
195
|
-
Config.unwrap(config).
|
|
316
|
+
Layer.effectContext(
|
|
317
|
+
Config.unwrap(config).pipe(
|
|
196
318
|
Effect.flatMap(make),
|
|
197
319
|
Effect.map((client) =>
|
|
198
|
-
|
|
199
|
-
|
|
320
|
+
Context.make(SqliteClient, client).pipe(
|
|
321
|
+
Context.add(Client.SqlClient, client)
|
|
200
322
|
)
|
|
201
323
|
)
|
|
202
324
|
)
|
|
203
325
|
).pipe(Layer.provide(Reactivity.layer))
|
|
204
326
|
|
|
205
327
|
/**
|
|
328
|
+
* Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
|
|
329
|
+
*
|
|
206
330
|
* @category layers
|
|
207
|
-
* @since
|
|
331
|
+
* @since 4.0.0
|
|
208
332
|
*/
|
|
209
333
|
export const layer = (
|
|
210
334
|
config: SqliteClientConfig
|
|
211
335
|
): Layer.Layer<SqliteClient | Client.SqlClient> =>
|
|
212
|
-
Layer.
|
|
336
|
+
Layer.effectContext(
|
|
213
337
|
Effect.map(make(config), (client) =>
|
|
214
|
-
|
|
215
|
-
|
|
338
|
+
Context.make(SqliteClient, client).pipe(
|
|
339
|
+
Context.add(Client.SqlClient, client)
|
|
216
340
|
))
|
|
217
341
|
).pipe(Layer.provide(Reactivity.layer))
|
package/src/SqliteMigrator.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Runs database migrations for Durable Object SQLite storage that uses Effect
|
|
3
|
+
* SQL.
|
|
4
|
+
*
|
|
5
|
+
* This module re-exports the shared `Migrator` loaders and error types, then
|
|
6
|
+
* provides `run` and `layer` helpers that execute ordered migrations through the
|
|
7
|
+
* current Durable Object SQLite `SqlClient`. Use it when a Durable
|
|
8
|
+
* Object needs to create or upgrade its local schema during construction, before
|
|
9
|
+
* repositories or request handlers use the object storage, or in tests that
|
|
10
|
+
* exercise Durable Object persistence.
|
|
11
|
+
*
|
|
12
|
+
* Migrations are recorded in `effect_sql_migrations` by default and are loaded
|
|
13
|
+
* using the shared `<id>_<name>` file or record-key convention. The underlying
|
|
14
|
+
* storage is scoped to a Durable Object id, so running migrations for one object
|
|
15
|
+
* does not update any other object instance; run the migrator against the same
|
|
16
|
+
* `DurableObjectStorage`-backed client that the object uses for normal queries
|
|
17
|
+
* so migrations can run in Cloudflare-managed transactions. These SQL
|
|
18
|
+
* migrations are separate from Cloudflare's Durable Object class migrations, and
|
|
19
|
+
* the Durable Object must already be configured with SQLite storage before this
|
|
20
|
+
* module can apply schema changes. Repeated startup runs are expected and are
|
|
21
|
+
* guarded by the migrations table, but request handling should wait until the
|
|
22
|
+
* migration layer has finished. This adapter does not currently write SQLite
|
|
23
|
+
* schema dumps for `schemaDirectory`.
|
|
24
|
+
*
|
|
25
|
+
* @since 4.0.0
|
|
3
26
|
*/
|
|
4
27
|
import type * as Effect from "effect/Effect"
|
|
5
28
|
import * as Layer from "effect/Layer"
|
|
@@ -8,13 +31,15 @@ import type * as Client from "effect/unstable/sql/SqlClient"
|
|
|
8
31
|
import type { SqlError } from "effect/unstable/sql/SqlError"
|
|
9
32
|
|
|
10
33
|
/**
|
|
11
|
-
* @since
|
|
34
|
+
* @since 4.0.0
|
|
12
35
|
*/
|
|
13
36
|
export * from "effect/unstable/sql/Migrator"
|
|
14
37
|
|
|
15
38
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
39
|
+
* Runs SQL migrations using the configured `SqlClient`, returning the migrations that were applied.
|
|
40
|
+
*
|
|
41
|
+
* @category constructors
|
|
42
|
+
* @since 4.0.0
|
|
18
43
|
*/
|
|
19
44
|
export const run: <R2 = never>(
|
|
20
45
|
{ loader, schemaDirectory, table }: Migrator.MigratorOptions<R2>
|
|
@@ -25,8 +50,10 @@ export const run: <R2 = never>(
|
|
|
25
50
|
> = Migrator.make({})
|
|
26
51
|
|
|
27
52
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
53
|
+
* Creates a layer that runs the configured SQL migrations during layer construction.
|
|
54
|
+
*
|
|
55
|
+
* @category constructors
|
|
56
|
+
* @since 4.0.0
|
|
30
57
|
*/
|
|
31
58
|
export const layer = <R>(
|
|
32
59
|
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"
|