@effect/sql-sqlite-do 4.0.0-beta.1 → 4.0.0-beta.100

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # `@effect/sql-sqlite-do`
2
2
 
3
- An `@effect/sql` implementation for Cloudflare Durable Objects sqlite storage.
3
+ An Effect SQL implementation for Cloudflare Durable Objects SQLite storage.
4
4
 
5
5
  ## Documentation
6
6
 
@@ -1,27 +1,52 @@
1
1
  /**
2
- * @since 1.0.0
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
- * @category type ids
14
- * @since 1.0.0
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
- * @category type ids
19
- * @since 1.0.0
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 1.0.0
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
- * @category tags
34
- * @since 1.0.0
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: ServiceMap.Service<SqliteClient, 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 1.0.0
73
+ * @since 4.0.0
40
74
  */
41
75
  export interface SqliteClientConfig {
42
- readonly db: SqlStorage;
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
- * @category constructor
49
- * @since 1.0.0
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 1.0.0
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 1.0.0
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;;GAEG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AAC3D,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAGvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAE/C,OAAO,KAAK,UAAU,MAAM,uCAAuC,CAAA;AACnE,OAAO,KAAK,MAAM,MAAM,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D,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;AAiED;;;;;GAKG;AACH,eAAO,MAAM,IAAI,YACN,kBAAkB,KAC1B,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,UAAU,CA+HrE,CAAA;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,WAAW,WACd,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,KACtC,KAAK,CAAC,KAAK,CAAC,YAAY,GAAG,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,CAUzB,CAAA;AAEzC;;;;;GAKG;AACH,eAAO,MAAM,KAAK,WACR,kBAAkB,KACzB,KAAK,CAAC,KAAK,CAAC,YAAY,GAAG,MAAM,CAAC,SAAS,CAML,CAAA"}
@@ -1,37 +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
- import * as ServiceMap from "effect/ServiceMap";
9
+ import * as Semaphore from "effect/Semaphore";
8
10
  import * as Stream from "effect/Stream";
9
11
  import * as Reactivity from "effect/unstable/reactivity/Reactivity";
10
12
  import * as Client from "effect/unstable/sql/SqlClient";
11
- import { SqlError } from "effect/unstable/sql/SqlError";
13
+ import { classifySqliteError, SqlError, UnknownError } from "effect/unstable/sql/SqlError";
12
14
  import * as Statement from "effect/unstable/sql/Statement";
13
15
  const ATTR_DB_SYSTEM_NAME = "db.system.name";
16
+ const classifyError = (cause, message, operation) => classifySqliteError(cause, {
17
+ message,
18
+ operation
19
+ });
14
20
  /**
15
- * @category type ids
16
- * @since 1.0.0
21
+ * Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
22
+ *
23
+ * @category type IDs
24
+ * @since 4.0.0
17
25
  */
18
26
  export const TypeId = "~@effect/sql-sqlite-do/SqliteClient";
19
27
  /**
20
- * @category tags
21
- * @since 1.0.0
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
22
37
  */
23
- export const SqliteClient = /*#__PURE__*/ServiceMap.Service("@effect/sql-sqlite-do/SqliteClient");
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
+ });
24
76
  /**
25
- * @category constructor
26
- * @since 1.0.0
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
27
81
  */
28
82
  export const make = options => Effect.gen(function* () {
29
83
  const compiler = Statement.makeCompilerSqlite(options.transformQueryNames);
30
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;
31
90
  const makeConnection = Effect.gen(function* () {
32
- const db = options.db;
33
91
  function* runIterator(sql, params = []) {
34
- const cursor = db.exec(sql, ...params);
92
+ const cursor = sqlStorage.exec(sql, ...params);
35
93
  const columns = cursor.columnNames;
36
94
  for (const result of cursor.raw()) {
37
95
  const obj = {};
@@ -45,12 +103,11 @@ export const make = options => Effect.gen(function* () {
45
103
  const runStatement = (sql, params = []) => Effect.try({
46
104
  try: () => Array.from(runIterator(sql, params)),
47
105
  catch: cause => new SqlError({
48
- cause,
49
- message: `Failed to execute statement`
106
+ reason: classifyError(cause, "Failed to execute statement", "execute")
50
107
  })
51
108
  });
52
109
  const runValues = (sql, params = []) => Effect.try({
53
- try: () => Array.from(db.exec(sql, ...params).raw(), row => {
110
+ try: () => Array.from(sqlStorage.exec(sql, ...params).raw(), row => {
54
111
  for (let i = 0; i < row.length; i++) {
55
112
  const value = row[i];
56
113
  if (value instanceof ArrayBuffer) {
@@ -60,8 +117,7 @@ export const make = options => Effect.gen(function* () {
60
117
  return row;
61
118
  }),
62
119
  catch: cause => new SqlError({
63
- cause,
64
- message: `Failed to execute statement`
120
+ reason: classifyError(cause, "Failed to execute statement", "execute")
65
121
  })
66
122
  });
67
123
  return identity({
@@ -74,6 +130,9 @@ export const make = options => Effect.gen(function* () {
74
130
  executeValues(sql, params) {
75
131
  return runValues(sql, params);
76
132
  },
133
+ executeValuesUnprepared(sql, params) {
134
+ return runValues(sql, params);
135
+ },
77
136
  executeUnprepared(sql, params, transformRows) {
78
137
  return transformRows ? Effect.map(runStatement(sql, params), transformRows) : runStatement(sql, params);
79
138
  },
@@ -85,33 +144,40 @@ export const make = options => Effect.gen(function* () {
85
144
  }
86
145
  });
87
146
  });
88
- const semaphore = yield* Effect.makeSemaphore(1);
147
+ const semaphore = yield* Semaphore.make(1);
89
148
  const connection = yield* makeConnection;
90
149
  const acquirer = semaphore.withPermits(1)(Effect.succeed(connection));
91
150
  const transactionAcquirer = Effect.uninterruptibleMask(restore => {
92
151
  const fiber = Fiber.getCurrent();
93
- const scope = ServiceMap.getUnsafe(fiber.services, Scope.Scope);
152
+ const scope = Context.getUnsafe(fiber.context, Scope.Scope);
94
153
  return Effect.as(Effect.tap(restore(semaphore.take(1)), () => Scope.addFinalizer(scope, semaphore.release(1))), connection);
95
154
  });
96
- return Object.assign(yield* Client.make({
155
+ const client = yield* Client.make({
97
156
  acquirer,
98
157
  compiler,
99
158
  transactionAcquirer,
159
+ transactionService: SqliteTransaction,
100
160
  spanAttributes: [...(options.spanAttributes ? Object.entries(options.spanAttributes) : []), [ATTR_DB_SYSTEM_NAME, "sqlite"]],
101
161
  transformRows
102
- }), {
162
+ });
163
+ return Object.assign(client, {
103
164
  [TypeId]: TypeId,
104
- 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")
105
167
  });
106
168
  });
107
169
  /**
170
+ * Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
171
+ *
108
172
  * @category layers
109
- * @since 1.0.0
173
+ * @since 4.0.0
110
174
  */
111
- export const layerConfig = config => Layer.effectServices(Config.unwrap(config).asEffect().pipe(Effect.flatMap(make), Effect.map(client => ServiceMap.make(SqliteClient, client).pipe(ServiceMap.add(Client.SqlClient, client))))).pipe(Layer.provide(Reactivity.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));
112
176
  /**
177
+ * Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
178
+ *
113
179
  * @category layers
114
- * @since 1.0.0
180
+ * @since 4.0.0
115
181
  */
116
- export const layer = config => Layer.effectServices(Effect.map(make(config), client => ServiceMap.make(SqliteClient, client).pipe(ServiceMap.add(Client.SqlClient, client)))).pipe(Layer.provide(Reactivity.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));
117
183
  //# sourceMappingURL=SqliteClient.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"SqliteClient.js","names":["Config","Effect","Fiber","identity","Layer","Scope","ServiceMap","Stream","Reactivity","Client","SqlError","Statement","ATTR_DB_SYSTEM_NAME","TypeId","SqliteClient","Service","make","options","gen","compiler","makeCompilerSqlite","transformQueryNames","transformRows","transformResultNames","defaultTransforms","array","undefined","makeConnection","db","runIterator","sql","params","cursor","exec","columns","columnNames","result","raw","obj","i","length","value","ArrayBuffer","Uint8Array","runStatement","try","Array","from","catch","cause","message","runValues","row","execute","map","executeRaw","executeValues","executeUnprepared","executeStream","suspend","iterator","fromIteratorSucceed","pipe","mapArray","chunk","semaphore","makeSemaphore","connection","acquirer","withPermits","succeed","transactionAcquirer","uninterruptibleMask","restore","fiber","getCurrent","scope","getUnsafe","services","as","tap","take","addFinalizer","release","Object","assign","spanAttributes","entries","config","layerConfig","effectServices","unwrap","asEffect","flatMap","client","add","SqlClient","provide","layer"],"sources":["../src/SqliteClient.ts"],"sourcesContent":[null],"mappings":"AAIA,OAAO,KAAKA,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,SAASC,QAAQ,QAAQ,iBAAiB;AAC1C,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,UAAU,MAAM,mBAAmB;AAC/C,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,UAAU,MAAM,uCAAuC;AACnE,OAAO,KAAKC,MAAM,MAAM,+BAA+B;AAEvD,SAASC,QAAQ,QAAQ,8BAA8B;AACvD,OAAO,KAAKC,SAAS,MAAM,+BAA+B;AAE1D,MAAMC,mBAAmB,GAAG,gBAAgB;AAE5C;;;;AAIA,OAAO,MAAMC,MAAM,GAAW,qCAAqC;AAoBnE;;;;AAIA,OAAO,MAAMC,YAAY,gBAAGR,UAAU,CAACS,OAAO,CAAe,oCAAoC,CAAC;AAclG;;;;AAIA,OAAO,MAAMC,IAAI,GACfC,OAA2B,IAE3BhB,MAAM,CAACiB,GAAG,CAAC,aAAS;EAClB,MAAMC,QAAQ,GAAGR,SAAS,CAACS,kBAAkB,CAACH,OAAO,CAACI,mBAAmB,CAAC;EAC1E,MAAMC,aAAa,GAAGL,OAAO,CAACM,oBAAoB,GAC9CZ,SAAS,CAACa,iBAAiB,CAACP,OAAO,CAACM,oBAAoB,CAAC,CAACE,KAAK,GAC/DC,SAAS;EAEb,MAAMC,cAAc,GAAG1B,MAAM,CAACiB,GAAG,CAAC,aAAS;IACzC,MAAMU,EAAE,GAAGX,OAAO,CAAC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,CAChC;IACDU;GACD,CAAC,EACF;IACE,CAACT,MAAM,GAAGA,MAAgB;IAC1B2E,MAAM,EAAEvE;GACT,CACF;AACH,CAAC,CAAC;AAEJ;;;;AAIA,OAAO,MAAMwE,WAAW,GACtBD,MAAuC,IAEvCpF,KAAK,CAACsF,cAAc,CAClB1F,MAAM,CAAC2F,MAAM,CAACH,MAAM,CAAC,CAACI,QAAQ,EAAE,CAAC9B,IAAI,CACnC7D,MAAM,CAAC4F,OAAO,CAAC7E,IAAI,CAAC,EACpBf,MAAM,CAACqD,GAAG,CAAEwC,MAAM,IAChBxF,UAAU,CAACU,IAAI,CAACF,YAAY,EAAEgF,MAAM,CAAC,CAAChC,IAAI,CACxCxD,UAAU,CAACyF,GAAG,CAACtF,MAAM,CAACuF,SAAS,EAAEF,MAAM,CAAC,CACzC,CACF,CACF,CACF,CAAChC,IAAI,CAAC1D,KAAK,CAAC6F,OAAO,CAACzF,UAAU,CAAC0F,KAAK,CAAC,CAAC;AAEzC;;;;AAIA,OAAO,MAAMA,KAAK,GAChBV,MAA0B,IAE1BpF,KAAK,CAACsF,cAAc,CAClBzF,MAAM,CAACqD,GAAG,CAACtC,IAAI,CAACwE,MAAM,CAAC,EAAGM,MAAM,IAC9BxF,UAAU,CAACU,IAAI,CAACF,YAAY,EAAEgF,MAAM,CAAC,CAAChC,IAAI,CACxCxD,UAAU,CAACyF,GAAG,CAACtF,MAAM,CAACuF,SAAS,EAAEF,MAAM,CAAC,CACzC,CAAC,CACL,CAAChC,IAAI,CAAC1D,KAAK,CAAC6F,OAAO,CAACzF,UAAU,CAAC0F,KAAK,CAAC,CAAC","ignoreList":[]}
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,gBAAgB;AAE5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AAC4C,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":[]}
@@ -1,5 +1,28 @@
1
1
  /**
2
- * @since 1.0.0
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 1.0.0
33
+ * @since 4.0.0
11
34
  */
12
35
  export * from "effect/unstable/sql/Migrator";
13
36
  /**
14
- * @category constructor
15
- * @since 1.0.0
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
- * @category constructor
20
- * @since 1.0.0
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;;GAEG;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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AAE,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,WACZ,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"}
@@ -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 1.0.0
4
+ * @since 4.0.0
5
5
  */
6
6
  export * from "effect/unstable/sql/Migrator";
7
7
  /**
8
- * @category constructor
9
- * @since 1.0.0
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
- * @category constructor
14
- * @since 1.0.0
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":"AAIA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,QAAQ,MAAM,8BAA8B;AAIxD;;;AAGA,cAAc,8BAA8B;AAE5C;;;;AAIA,OAAO,MAAMC,GAAG,gBAMZD,QAAQ,CAACE,IAAI,CAAC,EAAE,CAAC;AAErB;;;;AAIA,OAAO,MAAMC,KAAK,GAChBC,OAAoC,IAC6CL,KAAK,CAACM,aAAa,CAACJ,GAAG,CAACG,OAAO,CAAC,CAAC","ignoreList":[]}
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 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
  /**
5
- * @since 1.0.0
5
+ * @since 4.0.0
6
6
  */
7
7
  export * as SqliteClient from "./SqliteClient.ts";
8
8
  /**
9
- * @since 1.0.0
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 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
  // @barrel: Auto-generated exports. Do not edit manually.
5
5
  /**
6
- * @since 1.0.0
6
+ * @since 4.0.0
7
7
  */
8
8
  export * as SqliteClient from "./SqliteClient.js";
9
9
  /**
10
- * @since 1.0.0
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,17 +1,17 @@
1
1
  {
2
2
  "name": "@effect/sql-sqlite-do",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.100",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "A SQLite toolkit for Effect",
7
7
  "homepage": "https://effect.website",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/Effect-TS/effect-smol.git",
10
+ "url": "https://github.com/Effect-TS/effect.git",
11
11
  "directory": "packages/sql/sqlite-do"
12
12
  },
13
13
  "bugs": {
14
- "url": "https://github.com/Effect-TS/effect-smol/issues"
14
+ "url": "https://github.com/Effect-TS/effect/issues"
15
15
  },
16
16
  "tags": [
17
17
  "typescript",
@@ -43,16 +43,15 @@
43
43
  "provenance": true
44
44
  },
45
45
  "devDependencies": {
46
- "@cloudflare/workers-types": "^4.20260131.0",
47
- "effect": "^4.0.0-beta.1"
46
+ "@cloudflare/workers-types": "^5.20260708.1",
47
+ "effect": "^4.0.0-beta.100"
48
48
  },
49
49
  "peerDependencies": {
50
- "effect": "^4.0.0-beta.1"
50
+ "effect": "^4.0.0-beta.100"
51
51
  },
52
52
  "scripts": {
53
53
  "codegen": "effect-utils codegen",
54
54
  "build": "tsc -b tsconfig.json && pnpm babel",
55
- "build:tsgo": "tsgo -b tsconfig.json && pnpm babel",
56
55
  "babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps",
57
56
  "check": "tsc -b tsconfig.json",
58
57
  "test": "vitest",
@@ -1,38 +1,68 @@
1
1
  /**
2
- * @since 1.0.0
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
- import * as ServiceMap from "effect/ServiceMap"
32
+ import * as Semaphore from "effect/Semaphore"
12
33
  import * as Stream from "effect/Stream"
13
34
  import * as Reactivity from "effect/unstable/reactivity/Reactivity"
14
35
  import * as Client from "effect/unstable/sql/SqlClient"
15
36
  import type { Connection } from "effect/unstable/sql/SqlConnection"
16
- import { SqlError } from "effect/unstable/sql/SqlError"
37
+ import { classifySqliteError, SqlError, UnknownError } from "effect/unstable/sql/SqlError"
17
38
  import * as Statement from "effect/unstable/sql/Statement"
18
39
 
19
40
  const ATTR_DB_SYSTEM_NAME = "db.system.name"
20
41
 
42
+ const classifyError = (cause: unknown, message: string, operation: string) =>
43
+ classifySqliteError(cause, { message, operation })
44
+
21
45
  /**
22
- * @category type ids
23
- * @since 1.0.0
46
+ * Runtime type identifier used to mark Cloudflare Durable Object `SqliteClient` values.
47
+ *
48
+ * @category type IDs
49
+ * @since 4.0.0
24
50
  */
25
51
  export const TypeId: TypeId = "~@effect/sql-sqlite-do/SqliteClient"
26
52
 
27
53
  /**
28
- * @category type ids
29
- * @since 1.0.0
54
+ * Type-level identifier used to mark Cloudflare Durable Object `SqliteClient` values.
55
+ *
56
+ * @category type IDs
57
+ * @since 4.0.0
30
58
  */
31
59
  export type TypeId = "~@effect/sql-sqlite-do/SqliteClient"
32
60
 
33
61
  /**
62
+ * Cloudflare Durable Object SQLite client service, extending `SqlClient` with its configuration. `updateValues` is not supported.
63
+ *
34
64
  * @category models
35
- * @since 1.0.0
65
+ * @since 4.0.0
36
66
  */
37
67
  export interface SqliteClient extends Client.SqlClient {
38
68
  readonly [TypeId]: TypeId
@@ -43,26 +73,105 @@ export interface SqliteClient extends Client.SqlClient {
43
73
  }
44
74
 
45
75
  /**
46
- * @category tags
47
- * @since 1.0.0
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
48
85
  */
49
- export const SqliteClient = ServiceMap.Service<SqliteClient>("@effect/sql-sqlite-do/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
+ )
50
91
 
51
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
+ *
52
95
  * @category models
53
- * @since 1.0.0
96
+ * @since 4.0.0
54
97
  */
55
98
  export interface SqliteClientConfig {
56
- readonly db: SqlStorage
99
+ readonly db?: SqlStorage | undefined
100
+ readonly storage?: DurableObjectStorage | undefined
57
101
  readonly spanAttributes?: Record<string, unknown> | undefined
58
102
 
59
103
  readonly transformResultNames?: ((str: string) => string) | undefined
60
104
  readonly transformQueryNames?: ((str: string) => string) | undefined
61
105
  }
62
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
+
63
170
  /**
64
- * @category constructor
65
- * @since 1.0.0
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
66
175
  */
67
176
  export const make = (
68
177
  options: SqliteClientConfig
@@ -72,15 +181,19 @@ export const make = (
72
181
  const transformRows = options.transformResultNames
73
182
  ? Statement.defaultTransforms(options.transformResultNames).array
74
183
  : undefined
184
+ const db = options.storage?.sql ?? options.db
75
185
 
76
- const makeConnection = Effect.gen(function*() {
77
- const db = options.db
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
78
190
 
191
+ const makeConnection = Effect.gen(function*() {
79
192
  function* runIterator(
80
193
  sql: string,
81
194
  params: ReadonlyArray<unknown> = []
82
195
  ) {
83
- const cursor = db.exec(sql, ...params)
196
+ const cursor = sqlStorage.exec(sql, ...params)
84
197
  const columns = cursor.columnNames
85
198
  for (const result of cursor.raw()) {
86
199
  const obj: any = {}
@@ -98,7 +211,7 @@ export const make = (
98
211
  ): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
99
212
  Effect.try({
100
213
  try: () => Array.from(runIterator(sql, params)),
101
- catch: (cause) => new SqlError({ cause, message: `Failed to execute statement` })
214
+ catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
102
215
  })
103
216
 
104
217
  const runValues = (
@@ -107,7 +220,7 @@ export const make = (
107
220
  ): Effect.Effect<ReadonlyArray<any>, SqlError, never> =>
108
221
  Effect.try({
109
222
  try: () =>
110
- Array.from(db.exec(sql, ...params).raw(), (row) => {
223
+ Array.from(sqlStorage.exec(sql, ...params).raw(), (row) => {
111
224
  for (let i = 0; i < row.length; i++) {
112
225
  const value = row[i]
113
226
  if (value instanceof ArrayBuffer) {
@@ -116,7 +229,7 @@ export const make = (
116
229
  }
117
230
  return row
118
231
  }),
119
- catch: (cause) => new SqlError({ cause, message: `Failed to execute statement` })
232
+ catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
120
233
  })
121
234
 
122
235
  return identity<Connection>({
@@ -131,6 +244,9 @@ export const make = (
131
244
  executeValues(sql, params) {
132
245
  return runValues(sql, params)
133
246
  },
247
+ executeValuesUnprepared(sql, params) {
248
+ return runValues(sql, params)
249
+ },
134
250
  executeUnprepared(sql, params, transformRows) {
135
251
  return transformRows
136
252
  ? Effect.map(runStatement(sql, params), transformRows)
@@ -149,13 +265,13 @@ export const make = (
149
265
  })
150
266
  })
151
267
 
152
- const semaphore = yield* Effect.makeSemaphore(1)
268
+ const semaphore = yield* Semaphore.make(1)
153
269
  const connection = yield* makeConnection
154
270
 
155
271
  const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
156
272
  const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
157
273
  const fiber = Fiber.getCurrent()!
158
- const scope = ServiceMap.getUnsafe(fiber.services, Scope.Scope)
274
+ const scope = Context.getUnsafe(fiber.context, Scope.Scope)
159
275
  return Effect.as(
160
276
  Effect.tap(
161
277
  restore(semaphore.take(1)),
@@ -165,52 +281,61 @@ export const make = (
165
281
  )
166
282
  })
167
283
 
168
- return Object.assign(
169
- (yield* Client.make({
170
- acquirer,
171
- compiler,
172
- transactionAcquirer,
173
- spanAttributes: [
174
- ...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
175
- [ATTR_DB_SYSTEM_NAME, "sqlite"]
176
- ],
177
- transformRows
178
- })) as SqliteClient,
179
- {
180
- [TypeId]: TypeId as TypeId,
181
- config: options
182
- }
183
- )
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
+ })
184
305
  })
185
306
 
186
307
  /**
308
+ * Creates a layer from a `Config`-wrapped Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
309
+ *
187
310
  * @category layers
188
- * @since 1.0.0
311
+ * @since 4.0.0
189
312
  */
190
313
  export const layerConfig = (
191
314
  config: Config.Wrap<SqliteClientConfig>
192
315
  ): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError> =>
193
- Layer.effectServices(
194
- Config.unwrap(config).asEffect().pipe(
316
+ Layer.effectContext(
317
+ Config.unwrap(config).pipe(
195
318
  Effect.flatMap(make),
196
319
  Effect.map((client) =>
197
- ServiceMap.make(SqliteClient, client).pipe(
198
- ServiceMap.add(Client.SqlClient, client)
320
+ Context.make(SqliteClient, client).pipe(
321
+ Context.add(Client.SqlClient, client)
199
322
  )
200
323
  )
201
324
  )
202
325
  ).pipe(Layer.provide(Reactivity.layer))
203
326
 
204
327
  /**
328
+ * Creates a layer from a concrete Durable Object SQLite client configuration, providing both `SqliteClient` and `SqlClient`.
329
+ *
205
330
  * @category layers
206
- * @since 1.0.0
331
+ * @since 4.0.0
207
332
  */
208
333
  export const layer = (
209
334
  config: SqliteClientConfig
210
335
  ): Layer.Layer<SqliteClient | Client.SqlClient> =>
211
- Layer.effectServices(
336
+ Layer.effectContext(
212
337
  Effect.map(make(config), (client) =>
213
- ServiceMap.make(SqliteClient, client).pipe(
214
- ServiceMap.add(Client.SqlClient, client)
338
+ Context.make(SqliteClient, client).pipe(
339
+ Context.add(Client.SqlClient, client)
215
340
  ))
216
341
  ).pipe(Layer.provide(Reactivity.layer))
@@ -1,5 +1,28 @@
1
1
  /**
2
- * @since 1.0.0
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 1.0.0
34
+ * @since 4.0.0
12
35
  */
13
36
  export * from "effect/unstable/sql/Migrator"
14
37
 
15
38
  /**
16
- * @category constructor
17
- * @since 1.0.0
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
- * @category constructor
29
- * @since 1.0.0
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 1.0.0
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 1.0.0
8
+ * @since 4.0.0
9
9
  */
10
10
  export * as SqliteClient from "./SqliteClient.ts"
11
11
 
12
12
  /**
13
- * @since 1.0.0
13
+ * @since 4.0.0
14
14
  */
15
15
  export * as SqliteMigrator from "./SqliteMigrator.ts"