@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 583 lines 19.8 KB
Raw
1 /**
2 * Connects Effect SQL to SQLite compiled to WebAssembly with
3 * `@effect/wa-sqlite`.
4 *
5 * This module can create an in-memory SQLite database in the current runtime or
6 * connect to a worker-backed database, such as the OPFS worker from
7 * `OpfsWorker`. Both clients are exposed as `SqliteClient` and the generic
8 * Effect SQL client, serialize access, support database import and export, and
9 * can install reactivity hooks from SQLite update notifications. In-memory
10 * clients can stream query rows; worker-backed clients cannot. `updateValues`
11 * is not supported by this driver.
12 *
13 * @stability unstable
14 * @since 4.0.0
15 */
16 // oxlint-disable-next-line effect/no-import-from-barrel-package
17 import * as WaSqlite from "@effect/wa-sqlite"
18 import SQLiteESMFactory from "@effect/wa-sqlite/dist/wa-sqlite.mjs"
19 import { MemoryVFS } from "@effect/wa-sqlite/src/examples/MemoryVFS.js"
20 import * as Config from "effect/Config"
21 import * as Context from "effect/Context"
22 import * as Deferred from "effect/Deferred"
23 import * as Effect from "effect/Effect"
24 import * as Exit from "effect/Exit"
25 import { identity } from "effect/Function"
26 import * as Layer from "effect/Layer"
27 import * as Reactivity from "effect/reactivity/Reactivity"
28 import * as Rec from "effect/Record"
29 import * as Scope from "effect/Scope"
30 import * as ScopedRef from "effect/ScopedRef"
31 import * as Semaphore from "effect/Semaphore"
32 import * as Client from "effect/sql/SqlClient"
33 import type { Connection } from "effect/sql/SqlConnection"
34 import { classifySqliteError, SqlError } from "effect/sql/SqlError"
35 import * as Statement from "effect/sql/Statement"
36 import * as Stream from "effect/Stream"
37 import type { OpfsWorkerMessage } from "./internal/opfsWorker.ts"
38
39 const ATTR_DB_SYSTEM_NAME = "db.system.name"
40
41 const classifyError = (cause: unknown, message: string, operation: string) =>
42 classifySqliteError(cause, { message, operation })
43
44 /**
45 * Runtime identifier attached to SQLite WASM client values.
46 *
47 * @stability unstable
48 * @category type IDs
49 * @since 4.0.0
50 */
51 export const TypeId: TypeId = "~@effect/sql-sqlite-wasm/SqliteClient"
52
53 /**
54 * Type-level identifier for SQLite WASM client values.
55 *
56 * @stability unstable
57 * @category type IDs
58 * @since 4.0.0
59 */
60 export type TypeId = "~@effect/sql-sqlite-wasm/SqliteClient"
61
62 /**
63 * SQLite WASM client service interface, extending `SqlClient` with database `export` and `import` operations and marking `updateValues` as unsupported for SQLite.
64 *
65 * @stability unstable
66 * @category services
67 * @since 4.0.0
68 */
69 export interface SqliteClient extends Client.SqlClient {
70 readonly [TypeId]: TypeId
71 readonly config: SqliteClientMemoryConfig
72 readonly export: Effect.Effect<Uint8Array, SqlError>
73 readonly import: (data: Uint8Array) => Effect.Effect<void, SqlError>
74
75 /** Not supported in sqlite */
76 readonly updateValues: never
77 }
78
79 /**
80 * Service tag for the SQLite WASM client.
81 *
82 * @stability unstable
83 * @category services
84 * @since 4.0.0
85 */
86 export const SqliteClient = Context.Service<SqliteClient>("@effect/sql-sqlite-wasm/SqliteClient")
87
88 /**
89 * Configuration for an in-memory SQLite WASM client, including optional reactivity hooks, span attributes, and query/result name transforms.
90 *
91 * @stability unstable
92 * @category models
93 * @since 4.0.0
94 */
95 export interface SqliteClientMemoryConfig {
96 readonly installReactivityHooks?: boolean
97 readonly spanAttributes?: Record<string, unknown>
98 readonly transformResultNames?: (str: string) => string
99 readonly transformQueryNames?: (str: string) => string
100 }
101
102 /**
103 * Configuration for a worker-backed SQLite WASM client, including the scoped worker or message port, optional reactivity hooks, span attributes, and query/result name transforms.
104 *
105 * @stability unstable
106 * @category models
107 * @since 4.0.0
108 */
109 export interface SqliteClientConfig {
110 readonly worker: Effect.Effect<Worker | SharedWorker | MessagePort, never, Scope.Scope>
111 readonly installReactivityHooks?: boolean
112 readonly spanAttributes?: Record<string, unknown>
113 readonly transformResultNames?: (str: string) => string
114 readonly transformQueryNames?: (str: string) => string
115 }
116
117 interface SqliteConnection extends Connection {
118 readonly export: Effect.Effect<Uint8Array, SqlError>
119 readonly import: (data: Uint8Array) => Effect.Effect<void, SqlError>
120 }
121
122 interface SqliteMemoryConnection extends SqliteConnection {
123 readonly isTransaction: () => boolean
124 }
125
126 const initModule = Effect.runSync(
127 Effect.cached(Effect.promise(() => SQLiteESMFactory()))
128 )
129
130 const initEffect = Effect.runSync(
131 Effect.cached(initModule.pipe(Effect.map((module) => WaSqlite.Factory(module))))
132 )
133
134 const registered = new Set<string>()
135
136 /**
137 * Creates a scoped in-memory SQLite WASM client using the memory VFS, serializing access through a semaphore and exposing database `export` and `import` operations.
138 *
139 * @stability unstable
140 * @category constructors
141 * @since 4.0.0
142 */
143 export const makeMemory = (
144 options: SqliteClientMemoryConfig
145 ): Effect.Effect<SqliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
146 Effect.gen(function*() {
147 const reactivity = yield* Reactivity.Reactivity
148 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
149 const transformRows = options.transformResultNames ?
150 Statement.defaultTransforms(
151 options.transformResultNames
152 ).array :
153 undefined
154
155 const makeConnection = Effect.gen(function*() {
156 const sqlite3 = yield* initEffect
157
158 if (registered.has("memory-vfs") === false) {
159 registered.add("memory-vfs")
160 const module = yield* initModule
161 // @ts-expect-error
162 const vfs = new MemoryVFS("memory-vfs", module)
163 sqlite3.vfs_register(vfs as any, false)
164 }
165 const db = yield* Effect.acquireRelease(
166 Effect.try({
167 try: () => sqlite3.open_v2(":memory:", undefined, "memory-vfs"),
168 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to open database", "openDatabase") })
169 }),
170 (db) => Effect.sync(() => sqlite3.close(db))
171 )
172
173 if (options.installReactivityHooks) {
174 sqlite3.update_hook(db, (_op, _db, table, rowid) => {
175 if (!table) return
176 const id = String(Number(rowid))
177 reactivity.invalidateUnsafe({ [table]: [id] })
178 })
179 }
180
181 const run = (
182 sql: string,
183 params: ReadonlyArray<unknown> = [],
184 rowMode: "object" | "array" = "object"
185 ) =>
186 Effect.try({
187 try: () => {
188 const results: Array<any> = []
189 for (const stmt of sqlite3.statements(db, sql)) {
190 let columns: Array<string> | undefined
191 sqlite3.bind_collection(stmt, params as any)
192 while (sqlite3.step(stmt) === WaSqlite.SQLITE_ROW) {
193 columns = columns ?? sqlite3.column_names(stmt)
194 const row = sqlite3.row(stmt)
195 if (rowMode === "object") {
196 const obj: Record<string, any> = {}
197 for (let i = 0; i < columns.length; i++) {
198 Rec.assignProperty(obj, columns[i], row[i])
199 }
200 results.push(obj)
201 } else {
202 results.push(row)
203 }
204 }
205 }
206 return results
207 },
208 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to execute statement", "execute") })
209 })
210
211 return identity<SqliteMemoryConnection>({
212 isTransaction: () => sqlite3.get_autocommit(db) === 0,
213 execute(sql, params, transformRows) {
214 return transformRows
215 ? Effect.map(run(sql, params), transformRows)
216 : run(sql, params)
217 },
218 executeRaw(sql, params) {
219 return run(sql, params)
220 },
221 executeValues(sql, params) {
222 return run(sql, params, "array")
223 },
224 executeValuesUnprepared(sql, params) {
225 return run(sql, params, "array")
226 },
227 executeUnprepared(sql, params, transformRows) {
228 return this.execute(sql, params, transformRows)
229 },
230 executeStream(sql, params, transformRows) {
231 function* stream() {
232 for (const stmt of sqlite3.statements(db, sql)) {
233 let columns: Array<string> | undefined
234 sqlite3.bind_collection(stmt, params as any)
235 while (sqlite3.step(stmt) === WaSqlite.SQLITE_ROW) {
236 columns = columns ?? sqlite3.column_names(stmt)
237 const row = sqlite3.row(stmt)
238 const obj: Record<string, any> = {}
239 for (let i = 0; i < columns.length; i++) {
240 Rec.assignProperty(obj, columns[i], row[i])
241 }
242 yield obj
243 }
244 }
245 }
246 return Stream.suspend(() => Stream.fromIteratorSucceed(stream()[Symbol.iterator]())).pipe(
247 transformRows
248 ? Stream.mapArray((chunk) => transformRows(chunk) as any)
249 : identity,
250 Stream.mapError((cause) =>
251 new SqlError({ reason: classifyError(cause, "Failed to execute statement", "stream") })
252 )
253 )
254 },
255 export: Effect.try({
256 try: () => sqlite3.serialize(db, "main"),
257 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to export database", "export") })
258 }),
259 import(data) {
260 return Effect.try({
261 try: () => sqlite3.deserialize(db, "main", data, data.length, data.length, 1 | 2),
262 catch: (cause) => new SqlError({ reason: classifyError(cause, "Failed to import database", "import") })
263 })
264 }
265 })
266 })
267
268 const { acquirer, borrower, isTransaction, onCommitFailure, transactionAcquirer } = Client.makeSqliteAcquirers({
269 connection: Effect.succeed(yield* makeConnection),
270 semaphore: yield* Semaphore.make(1),
271 isTransaction: (conn) => conn.isTransaction()
272 })
273
274 return Object.assign(
275 (yield* Client.make({
276 driver: "sqlite-wasm",
277 acquirer,
278 borrower,
279 compiler,
280 transactionAcquirer,
281 onCommitFailure,
282 isTransaction,
283 releaseSavepoint: (name) => `RELEASE SAVEPOINT ${name}`,
284 spanAttributes: [
285 ...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
286 [ATTR_DB_SYSTEM_NAME, "sqlite"]
287 ],
288 transformRows
289 })) as SqliteClient,
290 {
291 [TypeId]: TypeId as TypeId,
292 config: options,
293 export: Effect.scoped(Effect.flatMap(transactionAcquirer, (conn) => conn.export)),
294 import(data: Uint8Array) {
295 return Effect.scoped(Effect.flatMap(transactionAcquirer, (conn) => conn.import(data)))
296 }
297 }
298 )
299 })
300
301 /**
302 * Creates a scoped worker-backed SQLite WASM client, communicating with the configured worker or message port, restarting the scoped connection on worker errors, and exposing database `export` and `import` operations.
303 *
304 * @stability unstable
305 * @category constructors
306 * @since 4.0.0
307 */
308 export const make = (
309 options: SqliteClientConfig
310 ): Effect.Effect<SqliteClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
311 Effect.gen(function*() {
312 const reactivity = yield* Reactivity.Reactivity
313 const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
314 const transformRows = options.transformResultNames ?
315 Statement.defaultTransforms(options.transformResultNames).array :
316 undefined
317 const makeConnection = Effect.gen(function*() {
318 let currentId = 0
319 const pending = new Map<number, (effect: Exit.Exit<any, SqlError>) => void>()
320 const scope = yield* Effect.scope
321 const readyDeferred = yield* Deferred.make<void>()
322
323 const worker = yield* options.worker
324 const port = "port" in worker ? worker.port : worker
325 const postMessage = (message: OpfsWorkerMessage, transferables?: ReadonlyArray<any>) =>
326 port.postMessage(message, transferables as any)
327
328 yield* Scope.addFinalizer(scope, Effect.sync(() => postMessage(["close"])))
329
330 const onMessage = (event: any) => {
331 const [id, error, results] = event.data
332 if (id === "ready") {
333 Deferred.doneUnsafe(readyDeferred, Exit.void)
334 return
335 } else if (id === "update_hook") {
336 reactivity.invalidateUnsafe({ [error]: [results] })
337 return
338 } else {
339 const resume = pending.get(id)
340 if (!resume) return
341 pending.delete(id)
342 if (error) {
343 resume(
344 Exit.fail(
345 new SqlError({ reason: classifyError(error, "Failed to execute statement", "execute") })
346 )
347 )
348 } else {
349 resume(Exit.succeed(results))
350 }
351 }
352 }
353 port.addEventListener("message", onMessage)
354 if ("start" in port) {
355 port.start()
356 }
357
358 function onError(cause: Event) {
359 const exit = Exit.fail(
360 new SqlError({ reason: classifyError(cause, "SQLite WASM worker failed", "worker") })
361 )
362 const requests = Array.from(pending.values())
363 pending.clear()
364 for (const resume of requests) {
365 resume(exit)
366 }
367 Effect.runFork(ScopedRef.set(connectionRef, makeConnection))
368 }
369 if ("onerror" in worker) {
370 worker.addEventListener("error", onError)
371 }
372
373 yield* Scope.addFinalizer(
374 scope,
375 Effect.sync(() => {
376 port.removeEventListener("message", onMessage)
377 worker.removeEventListener("error", onError)
378 })
379 )
380
381 yield* Deferred.await(readyDeferred)
382
383 if (options.installReactivityHooks) {
384 postMessage(["update_hook"])
385 }
386
387 const send = (id: number, message: OpfsWorkerMessage, transferables?: ReadonlyArray<any>) =>
388 Effect.callback<any, SqlError>((resume) => {
389 pending.set(id, resume)
390 postMessage(message, transferables)
391 })
392
393 const run = (
394 sql: string,
395 params: ReadonlyArray<unknown> = [],
396 rowMode: "object" | "array" = "object"
397 ): Effect.Effect<Array<any>, SqlError, never> => {
398 const rows = Effect.withFiber<WorkerResult, SqlError>((fiber) => {
399 const id = currentId++
400 return send(id, [id, sql, params], fiber.getRef(Transferables))
401 })
402 return rowMode === "object"
403 ? Effect.map(rows, extractObject)
404 : Effect.map(rows, extractRows)
405 }
406
407 return identity<SqliteConnection>({
408 execute(sql, params, transformRows) {
409 return transformRows
410 ? Effect.map(run(sql, params), transformRows)
411 : run(sql, params)
412 },
413 executeRaw(sql, params) {
414 return run(sql, params)
415 },
416 executeValues(sql, params) {
417 return run(sql, params, "array")
418 },
419 executeValuesUnprepared(sql, params) {
420 return run(sql, params, "array")
421 },
422 executeUnprepared(sql, params, transformRows) {
423 return this.execute(sql, params, transformRows)
424 },
425 executeStream() {
426 return Stream.die("executeStream not implemented")
427 },
428 export: Effect.suspend(() => {
429 const id = currentId++
430 return send(id, ["export", id])
431 }),
432 import(data) {
433 return Effect.suspend(() => {
434 const id = currentId++
435 return send(id, ["import", id, data], [data.buffer])
436 })
437 }
438 })
439 })
440
441 const connectionRef = yield* ScopedRef.fromAcquire(makeConnection)
442
443 const { acquirer, borrower, onCommitFailure, transactionAcquirer } = Client.makeSqliteAcquirers({
444 connection: ScopedRef.get(connectionRef),
445 semaphore: yield* Semaphore.make(1)
446 })
447
448 return Object.assign(
449 (yield* Client.make({
450 driver: "sqlite-wasm",
451 acquirer,
452 borrower,
453 compiler,
454 transactionAcquirer,
455 onCommitFailure,
456 releaseSavepoint: (name) => `RELEASE SAVEPOINT ${name}`,
457 spanAttributes: [
458 ...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
459 [ATTR_DB_SYSTEM_NAME, "sqlite"]
460 ],
461 transformRows
462 })) as SqliteClient,
463 {
464 [TypeId]: TypeId as TypeId,
465 config: options,
466 export: Effect.scoped(Effect.flatMap(acquirer, (connection) => connection.export)),
467 import(data: Uint8Array) {
468 return Effect.scoped(Effect.flatMap(acquirer, (connection) => connection.import(data)))
469 }
470 }
471 )
472 })
473
474 function rowToObject(columns: Array<string>, row: Array<any>) {
475 const obj: Record<string, any> = {}
476 for (let i = 0; i < columns.length; i++) {
477 Rec.assignProperty(obj, columns[i], row[i])
478 }
479 return obj
480 }
481 type WorkerResult = [columns: Array<Array<string>>, rows: Array<any>]
482
483 const extractObject = (rows: WorkerResult) => rows[1].map((row, index) => rowToObject(rows[0][index], row))
484 const extractRows = (rows: WorkerResult) => rows[1]
485
486 /**
487 * Fiber reference that stores transferables to include with worker-backed SQLite WASM query messages.
488 *
489 * @stability unstable
490 * @category services
491 * @since 4.0.0
492 */
493 export const Transferables = Context.Reference<ReadonlyArray<Transferable>>(
494 "@effect/sql-sqlite-wasm/currentTransferables",
495 { defaultValue: () => [] }
496 )
497
498 /**
499 * Runs an effect with the supplied transferables attached to worker-backed SQLite WASM query messages.
500 *
501 * @stability unstable
502 * @category transferables
503 * @since 4.0.0
504 */
505 export const withTransferables =
506 (transferables: ReadonlyArray<Transferable>) => <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
507 Effect.provideService(effect, Transferables, transferables)
508
509 /**
510 * Builds a layer from an Effect `Config` value, providing both the in-memory SQLite WASM `SqliteClient` service and the generic `SqlClient` service.
511 *
512 * @stability unstable
513 * @category layers
514 * @since 4.0.0
515 */
516 export const layerMemoryConfig = (
517 config: Config.Wrap<SqliteClientMemoryConfig>
518 ): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError | SqlError> =>
519 Layer.effectContext(
520 Config.unwrap(config).pipe(
521 Effect.flatMap(makeMemory),
522 Effect.map((client) =>
523 Context.make(SqliteClient, client).pipe(
524 Context.add(Client.SqlClient, client)
525 )
526 )
527 )
528 ).pipe(Layer.provide(Reactivity.layer))
529
530 /**
531 * Builds a layer from an in-memory SQLite WASM client configuration, providing both `SqliteClient` and the generic `SqlClient` service.
532 *
533 * @stability unstable
534 * @category layers
535 * @since 4.0.0
536 */
537 export const layerMemory = (
538 config: SqliteClientMemoryConfig
539 ): Layer.Layer<SqliteClient | Client.SqlClient, SqlError> =>
540 Layer.effectContext(
541 Effect.map(makeMemory(config), (client) =>
542 Context.make(SqliteClient, client).pipe(
543 Context.add(Client.SqlClient, client)
544 ))
545 ).pipe(Layer.provide(Reactivity.layer))
546
547 /**
548 * Builds a layer from a worker-backed SQLite WASM client configuration, providing both `SqliteClient` and the generic `SqlClient` service.
549 *
550 * @stability unstable
551 * @category layers
552 * @since 4.0.0
553 */
554 export const layer = (
555 config: SqliteClientConfig
556 ): Layer.Layer<SqliteClient | Client.SqlClient, SqlError> =>
557 Layer.effectContext(
558 Effect.map(make(config), (client) =>
559 Context.make(SqliteClient, client).pipe(
560 Context.add(Client.SqlClient, client)
561 ))
562 ).pipe(Layer.provide(Reactivity.layer))
563
564 /**
565 * Builds a layer from an Effect `Config` value, providing both the worker-backed SQLite WASM `SqliteClient` service and the generic `SqlClient` service.
566 *
567 * @stability unstable
568 * @category layers
569 * @since 4.0.0
570 */
571 export const layerConfig = (
572 config: Config.Wrap<SqliteClientConfig>
573 ): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError | SqlError> =>
574 Layer.effectContext(
575 Config.unwrap(config).pipe(
576 Effect.flatMap(make),
577 Effect.map((client) =>
578 Context.make(SqliteClient, client).pipe(
579 Context.add(Client.SqlClient, client)
580 )
581 )
582 )
583 ).pipe(Layer.provide(Reactivity.layer))