Skip to content
Source
Sign inSign up
Open History to see the last commit

nodeSqliteClient.ts

1,331 lines48.1 KB

1import * as NodeSqlite from "node:sqlite";
2import * as NodeUtil from "node:util";
3import * as NodeWorkerThreads from "node:worker_threads";
4​
5import * as Cache from "effect/Cache";
6import * as Clock from "effect/Clock";
7import * as Duration from "effect/Duration";
8import * as Effect from "effect/Effect";
9import * as Result from "effect/Result";
10import * as Exit from "effect/Exit";
11import * as Fiber from "effect/Fiber";
12import { identity } from "effect/Function";
13import * as Layer from "effect/Layer";
14import * as Option from "effect/Option";
15import * as Predicate from "effect/Predicate";
16import * as References from "effect/References";
17import * as Schema from "effect/Schema";
18import * as Scheduler from "effect/Scheduler";
19import * as Scope from "effect/Scope";
20import * as Semaphore from "effect/Semaphore";
21import * as Context from "effect/Context";
22import * as Stream from "effect/Stream";
23import * as Tracer from "effect/Tracer";
24import * as Reactivity from "effect/reactivity/Reactivity";
25import * as Client from "effect/sql/SqlClient";
26import type { Borrower, Connection } from "effect/sql/SqlConnection";
27import { SqlError, classifySqliteError } from "effect/sql/SqlError";
28import * as Statement from "effect/sql/Statement";
29​
30const ATTR_DB_SYSTEM_NAME = "db.system.name";
31​
32export interface SqliteClientConfig {
33 readonly filename: string;
34 readonly readonly?: boolean | undefined;
35 readonly spanAttributes?: Record<string, unknown> | undefined;
36 /** Ignored for in-memory databases, which cannot share a second connection. */
37 readonly readerWorker?: boolean | undefined;
38 /** Ignored for in-memory databases. */
39 readonly checkpointWorker?: boolean | undefined;
41​
42// SqlClient masks transaction acquisition, so readOnly carries the caller's interruption policy.
43const ReadOnly = Context.Reference<typeof Effect.interruptible | undefined>(
44 "@t3tools/shared/nodeSqliteClient/ReadOnly",
45 { defaultValue: () => undefined },
46);
47​
48/** Wrap the whole transaction; existing write transactions retain the writer, and writes on the reader fail as readonly. */
49export const readOnly = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
50 Effect.uninterruptibleMask((restore) =>
51 restore(Effect.provideService(effect, ReadOnly, restore)),
52 );
53​
54interface Acquired {
55 readonly connection: "writer" | "reader";
56 readonly lockWaitMs: number;
58​
59interface ConnectionOwner {
60 readonly fiberId: number;
61 readonly name: string;
62 readonly traceId?: string;
63 readonly spanId?: string;
65​
66const connectionOwner = (fiber: Fiber.Fiber<unknown, unknown>): ConnectionOwner => {
67 const current = Context.getOrUndefined(fiber.context, Tracer.ParentSpan);
68 if (current?._tag !== "Span") return { fiberId: fiber.id, name: "untraced" };
69 let span: Tracer.Span = current;
70 while (span.name === "sqlite" || span.name === "sql.transaction") {
71 const parent: Tracer.AnySpan | undefined = Option.getOrUndefined(span.parent);
72 if (parent?._tag !== "Span") break;
73 span = parent;
74 }
75 return { fiberId: fiber.id, name: span.name, traceId: span.traceId, spanId: span.spanId };
76};
77​
78const annotateAcquired = (
79 fiber: Fiber.Fiber<unknown, unknown>,
80 acquired: Acquired | undefined,
81 ownerAtWaitStart?: ConnectionOwner,
82) => {
83 const span = Context.getOrUndefined(fiber.context, Tracer.ParentSpan);
84 if (span?._tag !== "Span" || acquired === undefined) return;
85 span.attribute("db.connection", acquired.connection);
86 span.attribute("db.lock_wait_ms", acquired.lockWaitMs);
87 if (ownerAtWaitStart !== undefined) {
88 span.attribute("db.lock_owner_at_start.fiber_id", ownerAtWaitStart.fiberId);
89 span.attribute("db.lock_owner_at_start.name", ownerAtWaitStart.name);
90 if (ownerAtWaitStart.traceId !== undefined) {
91 span.attribute("db.lock_owner_at_start.trace_id", ownerAtWaitStart.traceId);
92 }
93 if (ownerAtWaitStart.spanId !== undefined) {
94 span.attribute("db.lock_owner_at_start.span_id", ownerAtWaitStart.spanId);
95 }
96 }
97};
98​
99const TransactionAcquire = Context.Reference<
100 | {
101 acquired?: Acquired;
102 ownerAtWaitStart: ConnectionOwner | undefined;
103 restore: typeof Effect.interruptible | undefined;
104 }
105 | undefined
106>("@t3tools/shared/nodeSqliteClient/TransactionAcquire", { defaultValue: () => undefined });
107​
108export class UnsupportedNodeSqliteVersionError extends Schema.TaggedError<UnsupportedNodeSqliteVersionError>()(
109 "UnsupportedNodeSqliteVersionError",
110 {
111 nodeVersion: Schema.String,
112 requirement: Schema.String,
113 },
114) {
115 override get message(): string {
116 return `Node.js ${this.nodeVersion} is missing required node:sqlite APIs. Upgrade to ${this.requirement}.`;
117 }
119​
120export class UnsupportedNodeSqliteOperationError extends Schema.TaggedError<UnsupportedNodeSqliteOperationError>()(
121 "UnsupportedNodeSqliteOperationError",
122 {},
123) {
124 override get message(): string {
125 return "Node SQLite does not support executeStream.";
126 }
128​
129const checkNodeSqliteCompat = Effect.fn("checkNodeSqliteCompat")(function () {
130 const parts = process.versions.node.split(".").map(Number);
131 const major = parts[0] ?? 0;
132 const minor = parts[1] ?? 0;
133 const supported = (major === 22 && minor >= 16) || (major === 23 && minor >= 11) || major >= 24;
134​
135 if (!supported) {
136 return Effect.die(
137 new UnsupportedNodeSqliteVersionError({
138 nodeVersion: process.versions.node,
139 requirement: "Node.js >=22.16, >=23.11, or >=24",
140 }),
141 );
142 }
143 return Effect.void;
144});
145​
146// node:sqlite supplies errcode; classifySqliteError expects errno.
147const classifyError = (cause: unknown, message: string, operation: string) => {
148 if (
149 Predicate.hasProperty(cause, "errcode") &&
150 typeof cause.errcode === "number" &&
151 !Predicate.hasProperty(cause, "errno")
152 ) {
153 Object.assign(cause, { errno: cause.errcode });
154 }
155 return classifySqliteError(cause, { message, operation });
156};
157​
158// SQLite retains text/blob bindings until rebind or finalization; node:sqlite cannot clear them.
159const LARGE_BINDING_SIZE = 64 * 1024;
160​
161const isBlob = (value: unknown): value is ArrayBufferView | ArrayBufferLike =>
162 ArrayBuffer.isView(value) || NodeUtil.types.isAnyArrayBuffer(value);
163​
164const bindingSize = (value: unknown): number => {
165 if (typeof value === "string") {
166 return value.length;
167 }
168 return isBlob(value) ? value.byteLength : 0;
169};
170​
171const hasLargeBinding = (params: ReadonlyArray<unknown>): boolean => {
172 try {
173 let size = 0;
174 for (const param of params) {
175 if (
176 (typeof param === "object" || typeof param === "function") &&
177 param !== null &&
178 !isBlob(param)
179 ) {
180 // Named parameters may be callable objects; read data properties only to avoid invoking getters twice.
181 for (const property of Object.values(Object.getOwnPropertyDescriptors(param))) {
182 if (!property.enumerable) continue;
183 if (!("value" in property)) return true;
184 size += bindingSize(property.value);
185 }
186 } else {
187 size += bindingSize(param);
188 }
189 }
190 return size > LARGE_BINDING_SIZE;
191 } catch {
192 return true;
193 }
194};
195​
196// Inline worker source also runs in bundles and single-executable builds without module files.
197const readerWorkerSource = `
198const { parentPort, workerData } = require("node:worker_threads");
199const { DatabaseSync } = require("node:sqlite");
200let db;
201try {
202 // A read-only open cannot be undone by SQL, unlike PRAGMA query_only.
203 const next = new DatabaseSync(workerData.filename, {
204 readOnly: workerData.readOnly,
205 allowExtension: workerData.allowExtension,
206 });
207 next.exec("PRAGMA busy_timeout = 5000;");
208 // Without WAL a reader's shared lock blocks the writer's commits, so refuse.
209 const mode = String(next.prepare("PRAGMA journal_mode").get().journal_mode).toLowerCase();
210 if (mode !== "wal") throw new Error("database uses journal_mode " + mode + ", not wal");
211 db = next;
212 parentPort.postMessage({ type: "ready" });
213} catch (error) {
214 parentPort.postMessage({ type: "unavailable", message: String(error && error.message ? error.message : error) });
216const statements = new Map();
217const prepare = (sql, cached) => {
218 if (!cached) return db.prepare(sql);
219 let statement = statements.get(sql);
220 if (statement === undefined) {
221 statement = db.prepare(sql);
222 if (statements.size >= workerData.prepareCacheSize) statements.delete(statements.keys().next().value);
223 } else {
224 statements.delete(sql);
225 }
226 statements.set(sql, statement);
227 return statement;
228};
229parentPort.on("message", ({ id, sql, params, cached, values, raw, safeIntegers }) => {
230 let operation = "open";
231 try {
232 if (db === undefined) throw new Error("database reader is unavailable");
233 operation = "prepare";
234 const statement = prepare(sql, cached);
235 operation = "execute";
236 statement.setReadBigInts(safeIntegers);
237 if (statement.columns().length > 0) {
238 statement.setReturnArrays(values);
239 parentPort.postMessage({ id, rows: statement.all(...params) });
240 } else {
241 const result = statement.run(...params);
242 parentPort.postMessage({ id, rows: raw ? result : [] });
243 }
244 } catch (error) {
245 parentPort.postMessage({
246 id,
247 error: {
248 operation,
249 message: String(error && error.message ? error.message : error),
250 code: error && error.code,
251 errcode: error && error.errcode,
252 errstr: error && error.errstr,
253 },
254 });
255 }
256});
257`;
258​
259interface ReaderRequest {
260 readonly sql: string;
261 readonly params: ReadonlyArray<unknown>;
262 readonly cached: boolean;
263 readonly values: boolean;
264 readonly raw: boolean;
266​
267type ReaderStartup =
268 | { readonly type: "ready" }
269 | { readonly type: "unavailable"; readonly message: string };
270​
271type ReaderReply =
272 | { readonly id: number; readonly rows: ReadonlyArray<any> }
273 | {
274 readonly id: number;
275 readonly error: {
276 readonly operation: "open" | "prepare" | "execute";
277 readonly message: string;
278 readonly code?: unknown;
279 readonly errcode?: unknown;
280 readonly errstr?: unknown;
281 };
282 };
283​
284const readerFailureMessages = {
285 open: "Failed to open database",
286 prepare: "Failed to prepare statement",
287 execute: "Failed to execute statement",
288} as const;
289​
290const failed = (cause: unknown, message: string, operation: string) =>
291 new SqlError({ reason: classifyError(cause, message, operation) });
292​
293// Read-transaction cleanup succeeds after worker exit because it can no longer change anything.
294const endsReadTransaction = (sql: string) => /^\s*ROLLBACK\b|^\s*COMMIT\b/i.test(sql);
295​
296// WAL readers run concurrently; one worker serialized every read behind the slowest transaction.
297const READER_POOL_SIZE = 4;
298​
299// Bind transactions to one worker; fail after its exit instead of resuming outside the transaction on a replacement.
300const makeReader = Effect.fnUntraced(function* (
301 options: SqliteClientConfig,
302 pool: { unavailable: boolean },
303 readOnly = true,
304) {
305 let current:
306 | {
307 readonly worker: NodeWorkerThreads.Worker;
308 readonly connection: Connection;
309 readonly started: Promise<ReaderStartup>;
310 }
311 | undefined;
312 let nextRequestId = 0;
313​
314 const start = () => {
315 const worker = new NodeWorkerThreads.Worker(readerWorkerSource, {
316 eval: true,
317 workerData: {
318 filename: options.filename,
319 readOnly,
320 allowExtension: false,
321 prepareCacheSize: 200,
322 },
323 });
324 let announce: (startup: ReaderStartup) => void = () => {};
325 const started = new Promise<ReaderStartup>((resolve) => {
326 announce = resolve;
327 });
328 let stoppedMessage: string | undefined;
329 const pending = new Map<
330 number,
331 { readonly endsTransaction: boolean; readonly resume: (reply: ReaderReply) => void }
332 >();
333 const stop = (message: string) => {
334 stoppedMessage = message;
335 announce({ type: "unavailable", message });
336 if (current?.worker === worker) current = undefined;
337 for (const [id, request] of pending) {
338 request.resume(
339 request.endsTransaction
340 ? { id, rows: [] }
341 : { id, error: { operation: "execute", message } },
342 );
343 }
344 pending.clear();
345 };
346 worker.on("message", (reply: ReaderReply | ReaderStartup) => {
347 if ("type" in reply) {
348 announce(reply);
349 if (pending.size === 0) worker.unref();
350 return;
351 }
352 const request = pending.get(reply.id);
353 if (request === undefined) return;
354 pending.delete(reply.id);
355 if (pending.size === 0) worker.unref();
356 request.resume(reply);
357 });
358 worker.on("error", (cause) => stop(`Database reader failed: ${cause.message}`));
359 worker.on("exit", (code) => stop(`Database reader exited with code ${code}`));
360​
361 const request = (input: ReaderRequest) =>
362 Effect.withFiber<ReadonlyArray<any>, SqlError>((fiber) => {
363 // A COMMIT that never reached a writer must fail; only its ROLLBACK is moot.
364 const endsTransaction = readOnly
365 ? endsReadTransaction(input.sql)
366 : /^\s*ROLLBACK\b/i.test(input.sql);
367 if (stoppedMessage !== undefined) {
368 return endsTransaction
369 ? Effect.succeed([])
370 : Effect.fail(
371 failed(new Error(stoppedMessage), readerFailureMessages.execute, "execute"),
372 );
373 }
374 const safeIntegers = Boolean(Context.get(fiber.context, Client.SafeIntegers));
375 return Effect.callback<ReadonlyArray<any>, SqlError>((resume) => {
376 const id = nextRequestId++;
377 pending.set(id, {
378 endsTransaction,
379 resume: (reply) => {
380 if ("rows" in reply) {
381 resume(Effect.succeed(reply.rows));
382 return;
383 }
384 const { operation, message, ...codes } = reply.error;
385 resume(
386 Effect.fail(
387 failed(
388 Object.assign(new Error(message), codes),
389 readerFailureMessages[operation],
390 operation,
391 ),
392 ),
393 );
394 },
395 });
396 if (pending.size === 1) worker.ref();
397 const cached = input.cached && !hasLargeBinding(input.params);
398 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
399 worker.postMessage({ id, safeIntegers, ...input, cached });
400 });
401 });
402​
403 const connection = identity<Connection>({
404 execute(sql, params, rowTransform) {
405 const effect = request({ sql, params, cached: true, values: false, raw: false });
406 return rowTransform ? Effect.map(effect, rowTransform) : effect;
407 },
408 executeRaw(sql, params) {
409 return request({ sql, params, cached: true, values: false, raw: true });
410 },
411 executeValues(sql, params) {
412 return request({ sql, params, cached: true, values: true, raw: false });
413 },
414 executeValuesUnprepared(sql, params) {
415 return request({ sql, params: params ?? [], cached: false, values: true, raw: false });
416 },
417 executeUnprepared(sql, params, rowTransform) {
418 // Read snapshots need deferred BEGIN; readonly connections reject IMMEDIATE and EXCLUSIVE.
419 const statement = readOnly && /^\s*BEGIN\b/i.test(sql) ? "BEGIN" : sql;
420 const effect = request({
421 sql: statement,
422 params: params ?? [],
423 cached: false,
424 values: false,
425 raw: false,
426 });
427 return rowTransform ? Effect.map(effect, rowTransform) : effect;
428 },
429 executeStream(_sql, _params) {
430 return Stream.die(new UnsupportedNodeSqliteOperationError());
431 },
432 });
433 current = { worker, connection, started };
434 return current;
435 };
436​
437 yield* Effect.addFinalizer(() =>
438 Effect.promise(async () => {
439 await current?.worker.terminate();
440 }),
441 );
442​
443 const startupTimeout = Duration.seconds(10);
444​
445 const turnOff = (reader: typeof current, reason: string) =>
446 Effect.suspend(() => {
447 if (reader !== undefined) {
448 if (current === reader) current = undefined;
449 void reader.worker.terminate();
450 }
451 if (pool.unavailable) return Effect.undefined;
452 pool.unavailable = true;
453 return Effect.logWarning("SQLite reader worker unavailable; reads use the main connection", {
454 reason,
455 }).pipe(Effect.as(undefined));
456 });
457​
458 return Effect.suspend((): Effect.Effect<Connection | undefined> => {
459 if (pool.unavailable) return Effect.undefined;
460 let reader: NonNullable<typeof current>;
461 try {
462 reader = current ?? start();
463 } catch (cause) {
464 return turnOff(undefined, String(cause));
465 }
466 return Effect.flatMap(
467 Effect.withFiber((fiber) => {
468 const restore = Context.get(fiber.context, ReadOnly) ?? identity;
469 return restore(
470 Effect.timeoutOption(
471 Effect.promise(() => reader.started),
472 startupTimeout,
473 ),
474 );
475 }),
476 (startup): Effect.Effect<Connection | undefined> =>
477 // A late ready event must not hand out a worker retired by another acquire timeout.
478 pool.unavailable || current !== reader
479 ? Effect.undefined
480 : Option.isSome(startup) && startup.value.type === "ready"
481 ? Effect.succeed(reader.connection)
482 : turnOff(
483 reader,
484 Option.isSome(startup) && startup.value.type === "unavailable"
485 ? startup.value.message
486 : `worker did not open the database within ${Duration.toMillis(startupTimeout)} ms`,
487 ),
488 );
489 });
490});
491​
492const CHECKPOINT_FRAMES = 1000;
493const CHECKPOINT_IDLE_MS = 100;
494const CHECKPOINT_BACKPRESSURE_FRAMES = 32_768;
495​
496const readSafeSqliteInteger = (value: unknown) => {
497 const number = typeof value === "bigint" ? Number(value) : value;
498 return typeof number === "number" && Number.isSafeInteger(number) ? number : undefined;
499};
500​
501// Evaluated source also works in bundles and single-executable builds.
502const checkpointWorkerSource = `
503const { parentPort, workerData } = require("node:worker_threads");
504const { DatabaseSync } = require("node:sqlite");
505const { statSync } = require("node:fs");
506let db;
507let frames;
508let checkpoint;
509let truncate;
510let journalSizeLimit = -1;
511let generation = 0;
512let nextTruncateId = 0;
513let pendingTruncate;
514let pendingBarriers = 0;
515let idle;
516const initialize = () => {
517 if (db !== undefined) return;
518 // The writer switches to WAL after constructing the client.
519 db = new DatabaseSync(workerData.filename);
520 db.exec("PRAGMA busy_timeout = 5000;");
521 const mode = String(db.prepare("PRAGMA journal_mode").get().journal_mode).toLowerCase();
522 if (mode !== "wal") throw new Error("database uses journal_mode " + mode + ", not wal");
523 // macOS requires F_FULLFSYNC to flush the drive cache.
524 db.exec("PRAGMA checkpoint_fullfsync = ON;");
525 frames = db.prepare("PRAGMA wal_checkpoint(NOOP)");
526 checkpoint = db.prepare("PRAGMA wal_checkpoint(PASSIVE)");
527 truncate = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)");
528};
529const atCeiling = (state) => state.log >= workerData.backpressureFrames;
530const exceedsSizeLimit = () =>
531 journalSizeLimit >= 0 && statSync(workerData.filename + "-wal").size > journalSizeLimit;
532// Retry a WAL past its limits even with admission open, so it resets once a pinning reader closes.
533const schedule = (state, retry) => {
534 if (retry || state.log - state.checkpointed >= workerData.frames) {
535 idle = setTimeout(() => check(undefined, undefined, true), workerData.idleMs);
536 }
537};
538const check = (id, nextJournalSizeLimit, force, nextGeneration = generation) => {
539 initialize();
540 if (Number.isSafeInteger(nextGeneration)) generation = Math.max(generation, nextGeneration);
541 if (Number.isSafeInteger(nextJournalSizeLimit)) journalSizeLimit = nextJournalSizeLimit;
542 let state = frames.get();
543 const oversized = exceedsSizeLimit();
544 let pinned = false;
545 if (force || state.log - state.checkpointed >= workerData.frames || oversized) {
546 const result = checkpoint.get();
547 // A reader's snapshot stopped the backfill; holding writes cannot let it catch up.
548 pinned = result.checkpointed < result.log;
549 state = frames.get();
550 }
551 if (state.checkpointed === state.log && (atCeiling(state) || oversized)) {
552 pendingTruncate = { truncateId: ++nextTruncateId, checkId: id, generation, state };
553 parentPort.postMessage({
554 type: "truncate-request",
555 truncateId: pendingTruncate.truncateId,
556 checkId: id,
557 generation,
558 ...state,
559 });
560 return;
561 }
562 const blocked = !pinned && (atCeiling(state) || oversized) && state.checkpointed < state.log;
563 parentPort.postMessage({ type: "checkpoint", id, generation, ...state, blocked });
564 schedule(state, atCeiling(state) || oversized);
565};
566const finishTruncate = (truncateId) => {
567 if (pendingTruncate === undefined || pendingTruncate.truncateId !== truncateId) return;
568 const pending = pendingTruncate;
569 parentPort.postMessage({ type: "truncate-start", truncateId });
570 // Admission is paused here; a pinned reader must not make truncation wait.
571 db.exec("PRAGMA busy_timeout = 0;");
572 try {
573 truncate.get();
574 } finally {
575 db.exec("PRAGMA busy_timeout = 5000;");
576 }
577 const state = frames.get();
578 pendingTruncate = undefined;
579 parentPort.postMessage({
580 type: "checkpoint",
581 id: pending.checkId,
582 generation: pending.generation,
583 truncateId,
584 ...state,
585 // With writes held, only a reader can stop the reset, and holding them longer would not
586 // release it.
587 blocked: false,
588 });
589 while (pendingBarriers > 0) {
590 pendingBarriers--;
591 parentPort.postMessage("barrier");
592 }
593 schedule(state, atCeiling(state) || exceedsSizeLimit());
594};
595parentPort.on("message", (request) => {
596 clearTimeout(idle);
597 if (request && request.type === "truncate") {
598 finishTruncate(request.truncateId);
599 return;
600 }
601 const isBarrier = request === "barrier";
602 if (pendingTruncate !== undefined) {
603 if (isBarrier) pendingBarriers++;
604 else if (request && request.type === "check") {
605 // Retire the request without labeling the pending snapshot with a newer generation.
606 parentPort.postMessage({
607 type: "checkpoint",
608 id: request.id,
609 generation: pendingTruncate.generation,
610 ...pendingTruncate.state,
611 blocked: true,
612 });
613 }
614 return;
615 }
616 const input = request && typeof request === "object" ? request : {};
617 check(input.id, input.journalSizeLimit, input.force, input.generation);
618 if (isBarrier) {
619 if (pendingTruncate !== undefined) pendingBarriers++;
620 else parentPort.postMessage("barrier");
621 }
622});
623`;
624​
625const startCheckpointWorker = Effect.fnUntraced(function* (
626 filename: string,
627 writer: NodeSqlite.DatabaseSync,
628) {
629 const warn = (reason: string) =>
630 Effect.logWarning(
631 "SQLite checkpoint worker unavailable; checkpoints run on the main connection",
632 { reason },
633 );
634 if (typeof writer.isTransaction !== "boolean" || typeof writer.setAuthorizer !== "function") {
635 yield* warn("Node SQLite lacks transaction-state or statement-authorizer support");
636 return undefined;
637 }
638 // These are the writer's first statements, before callers set their own wait. A file that is
639 // not a database first fails here.
640 const { version, frames, sizeLimit } = yield* Effect.try({
641 try: () => {
642 writer.exec("PRAGMA busy_timeout = 5000;");
643 return {
644 version: String(writer.prepare("SELECT sqlite_version() AS version").get()?.version),
645 frames: writer.prepare("PRAGMA wal_checkpoint(NOOP)"),
646 sizeLimit: writer.prepare("PRAGMA journal_size_limit"),
647 };
648 },
649 catch: (cause) => failed(cause, "Failed to open database", "open"),
650 });
651 const [major = 0, minor = 0] = version.split(".").map(Number);
652 // Older SQLite treats NOOP as PASSIVE, which would checkpoint on the writer.
653 if (major < 3 || (major === 3 && minor < 51)) {
654 yield* warn(`SQLite ${version} has no NOOP checkpoint`);
655 return undefined;
656 }
657 const started = Result.try(
658 () =>
659 new NodeWorkerThreads.Worker(checkpointWorkerSource, {
660 eval: true,
661 workerData: {
662 filename,
663 frames: CHECKPOINT_FRAMES,
664 backpressureFrames: CHECKPOINT_BACKPRESSURE_FRAMES,
665 idleMs: CHECKPOINT_IDLE_MS,
666 },
667 }),
668 );
669 if (Result.isFailure(started)) {
670 yield* warn(String(started.failure));
671 return undefined;
672 }
673 const worker = started.success;
674 const runFork = Effect.runForkWith(yield* Effect.context<never>());
675 let stopped = false;
676 let generation = 0;
677 let nextId = 0;
678 let inFlight: number | undefined;
679 let checkAgain = false;
680 let blocked = false;
681 let log = 0;
682 let checkpointed = 0;
683 let unknown = false;
684 let truncate: { id: number; acknowledged: boolean } | undefined;
685 type Waiter = { resume: () => void; cancelled: boolean };
686 const waiters: Array<Waiter> = [];
687 let elected: Waiter | undefined;
688 const gated = () =>
689 !stopped && !writer.isTransaction && (unknown || blocked || truncate !== undefined);
690 const wake = () => {
691 if (!gated() && elected === undefined) {
692 elected = waiters.shift();
693 elected?.resume();
694 }
695 if (waiters.length === 0 && elected === undefined) worker.unref();
696 };
697 const release = (waiter: Waiter) => {
698 waiter.cancelled = true;
699 const index = waiters.indexOf(waiter);
700 if (index !== -1) waiters.splice(index, 1);
701 if (elected === waiter) elected = undefined;
702 wake();
703 };
704 const handBack = (reason: string) => {
705 if (stopped) return;
706 stopped = true;
707 truncate = undefined;
708 try {
709 writer.exec(`PRAGMA wal_autocheckpoint = ${CHECKPOINT_FRAMES};`);
710 } catch {}
711 wake();
712 runFork(warn(reason));
713 };
714 const send = (message: unknown) => {
715 try {
716 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
717 worker.postMessage(message);
718 } catch (cause) {
719 handBack(String(cause));
720 }
721 };
722 const acknowledge = () => {
723 if (truncate === undefined || truncate.acknowledged || stopped || writer.isTransaction) return;
724 truncate.acknowledged = true;
725 send({ type: "truncate", truncateId: truncate.id });
726 };
727 const observe = () => {
728 try {
729 const state = frames.get();
730 const nextLog = readSafeSqliteInteger(state?.log);
731 const nextCheckpointed = readSafeSqliteInteger(state?.checkpointed);
732 if (nextLog === undefined || nextCheckpointed === undefined)
733 throw new Error("Invalid WAL frame counts");
734 log = nextLog;
735 checkpointed = nextCheckpointed;
736 unknown = false;
737 // Past the ceiling, hold writes until the worker judges this WAL; a reader can pin it there.
738 if (log >= CHECKPOINT_BACKPRESSURE_FRAMES) blocked = true;
739 } catch {
740 unknown = true;
741 }
742 };
743 const request = (force = false) => {
744 if (stopped || writer.isTransaction) return;
745 if (truncate !== undefined) {
746 acknowledge();
747 return;
748 }
749 if (inFlight !== undefined) {
750 checkAgain = true;
751 return;
752 }
753 if (!force && !gated() && log - checkpointed < CHECKPOINT_FRAMES) return;
754 inFlight = ++nextId;
755 try {
756 send({
757 type: "check",
758 id: inFlight,
759 generation,
760 journalSizeLimit: readSafeSqliteInteger(sizeLimit.get()?.journal_size_limit),
761 force: force || gated(),
762 });
763 } catch (cause) {
764 handBack(String(cause));
765 }
766 };
767 const check = () => {
768 if (stopped || writer.isTransaction) return;
769 generation++;
770 observe();
771 request();
772 wake();
773 };
774 worker.on("error", (cause) => handBack(cause.message));
775 worker.on("exit", (code) => handBack(`exited with code ${code}`));
776 worker.on("message", (message: unknown) => {
777 if (stopped || typeof message !== "object" || message === null || !("type" in message)) return;
778 if (
779 message.type === "truncate-request" &&
780 "truncateId" in message &&
781 typeof message.truncateId === "number"
782 ) {
783 truncate = { id: message.truncateId, acknowledged: false };
784 blocked = true;
785 acknowledge();
786 return;
787 }
788 if (message.type !== "checkpoint") return;
789 const completedTruncate =
790 truncate !== undefined && "truncateId" in message && message.truncateId === truncate.id;
791 if (completedTruncate) truncate = undefined;
792 // An acknowledged truncate excludes new writer mutations, so its result is current.
793 if (
794 (completedTruncate || ("generation" in message && message.generation === generation)) &&
795 "log" in message &&
796 typeof message.log === "number" &&
797 "checkpointed" in message &&
798 typeof message.checkpointed === "number" &&
799 "blocked" in message &&
800 typeof message.blocked === "boolean"
801 ) {
802 log = message.log;
803 checkpointed = message.checkpointed;
804 blocked = message.blocked;
805 unknown = false;
806 }
807 // Even a stale snapshot must retire its request so a newer check can run.
808 if ("id" in message && message.id === inFlight) inFlight = undefined;
809 if (checkAgain && inFlight === undefined) {
810 checkAgain = false;
811 request(true);
812 }
813 wake();
814 });
815 yield* Effect.addFinalizer(() =>
816 Effect.promise(() => {
817 stopped = true;
818 truncate = undefined;
819 wake();
820 return worker.terminate();
821 }),
822 );
823 writer.exec("PRAGMA wal_autocheckpoint = 0;");
824 observe();
825 worker.unref();
826​
827 const runWithAdmission = <A, E>(
828 execute: () => Exit.Exit<A, E>,
829 admitted?: Waiter,
830 ): Effect.Effect<A, E> =>
831 Effect.suspend(() => {
832 if (!gated()) {
833 // Release the election after executing; its successor must see the new WAL state.
834 try {
835 return execute();
836 } finally {
837 if (admitted !== undefined) release(admitted);
838 }
839 }
840 if (admitted !== undefined) release(admitted);
841 const waiter: Waiter = { resume: () => {}, cancelled: false };
842 const wait = Effect.uninterruptibleMask((restore) =>
843 Effect.withFiber((fiber) =>
844 (fiber.getRef(TransactionAcquire)?.restore ?? restore)(
845 Effect.callback<void>(function (this: Scheduler.Scheduler, resume) {
846 const dispatcher = this.makeDispatcher();
847 waiter.resume = () => {
848 dispatcher.scheduleTask(() => {
849 if (!waiter.cancelled) resume(Effect.void);
850 }, 0);
851 };
852 waiters.push(waiter);
853 worker.ref();
854 request(true);
855 wake();
856 return Effect.sync(() => release(waiter));
857 }),
858 ),
859 ),
860 );
861 return wait.pipe(
862 Effect.flatMap(() => runWithAdmission(execute, waiter)),
863 Effect.onInterrupt(() => Effect.sync(() => release(waiter))),
864 );
865 });
866 return { check, runWithAdmission };
867});
868​
869const make = Effect.fn("makeWithDatabase")(function* (
870 options: SqliteClientConfig,
871): Effect.fn.Return<Client.SqlClient, SqlError, Scope.Scope | Reactivity.Reactivity> {
872 yield* checkNodeSqliteCompat();
873​
874 const compiler = Statement.makeCompilerSqlite();
875 const isFileDatabase = options.filename !== ":memory:" && options.filename !== "";
876​
877 const makeConnection = Effect.gen(function* () {
878 const scope = yield* Effect.scope;
879 const db = yield* Effect.try({
880 try: () =>
881 new NodeSqlite.DatabaseSync(options.filename, {
882 readOnly: options.readonly ?? false,
883 allowExtension: false,
884 }),
885 catch: (cause) =>
886 new SqlError({
887 reason: classifyError(cause, "Failed to open database", "open"),
888 }),
889 });
890 yield* Scope.addFinalizer(
891 scope,
892 Effect.try({
893 try: () => db.close(),
894 catch: (cause) =>
895 new SqlError({
896 reason: classifyError(cause, "Failed to close database", "close"),
897 }),
898 }).pipe(Effect.orDie),
899 );
900 // Started after the close finalizer, so the worker stops before the writer closes.
901 const checkpoint =
902 options.checkpointWorker && isFileDatabase
903 ? yield* startCheckpointWorker(options.filename, db)
904 : undefined;
905​
906 const statementReaderCache = new WeakMap<NodeSqlite.StatementSync, boolean>();
907 interface CheckpointClassification {
908 backpressure: boolean;
909 check: boolean;
910 }
911 const checkpointClassifications = new WeakMap<
912 NodeSqlite.StatementSync,
913 CheckpointClassification
914 >();
915 const mutatingPragmasWithValues = new Set([
916 "application_id",
917 "auto_vacuum",
918 "journal_mode",
919 "schema_version",
920 "user_version",
921 ]);
922 const mutatesPragma = (name: string | null, value: string | null) => {
923 const pragma = name?.toLowerCase();
924 if (pragma === undefined) return false;
925 if (pragma === "incremental_vacuum" || pragma === "optimize") return true;
926 if (pragma === "wal_checkpoint") return value?.toLowerCase() !== "noop";
927 return value !== null && mutatingPragmasWithValues.has(pragma);
928 };
929 let activeClassification: CheckpointClassification | undefined;
930 const withClassification = <A>(
931 classification: CheckpointClassification | undefined,
932 f: () => A,
933 ): A => {
934 if (classification === undefined) return f();
935 const previous = activeClassification;
936 activeClassification = classification;
937 try {
938 return f();
939 } finally {
940 activeClassification = previous;
941 }
942 };
943 if (checkpoint !== undefined) {
944 // SQLite may reprepare an expired statement while it executes. Keep this
945 // callback installed and collect only around that statement's prepare/run.
946 db.setAuthorizer((actionCode, action, argument) => {
947 const classification = activeClassification;
948 if (classification === undefined) return NodeSqlite.constants.SQLITE_OK;
949 const constants = NodeSqlite.constants;
950 if (actionCode === constants.SQLITE_TRANSACTION) {
951 classification.backpressure ||= action === "BEGIN";
952 classification.check ||= action === "COMMIT" || action === "ROLLBACK";
953 } else if (actionCode === constants.SQLITE_SAVEPOINT) {
954 classification.backpressure ||= action === "BEGIN";
955 classification.check = true;
956 } else if (actionCode === constants.SQLITE_PRAGMA && mutatesPragma(action, argument)) {
957 classification.backpressure = true;
958 classification.check = true;
959 } else if (
960 actionCode !== constants.SQLITE_READ &&
961 actionCode !== constants.SQLITE_SELECT &&
962 actionCode !== constants.SQLITE_FUNCTION &&
963 actionCode !== constants.SQLITE_RECURSIVE &&
964 actionCode !== constants.SQLITE_PRAGMA
965 ) {
966 classification.backpressure = true;
967 classification.check = true;
968 }
969 return constants.SQLITE_OK;
970 });
971 }
972 const hasRows = (statement: NodeSqlite.StatementSync): boolean => {
973 const cached = statementReaderCache.get(statement);
974 if (cached !== undefined) {
975 return cached;
976 }
977 const value = statement.columns().length > 0;
978 statementReaderCache.set(statement, value);
979 return value;
980 };
981​
982 const prepare = (sql: string) =>
983 Effect.try({
984 try: () => {
985 const classification = { backpressure: false, check: false };
986 const statement = withClassification(classification, () => db.prepare(sql));
987 if (checkpoint !== undefined) checkpointClassifications.set(statement, classification);
988 return statement;
989 },
990 catch: (cause) =>
991 new SqlError({
992 reason: classifyError(cause, "Failed to prepare statement", "prepare"),
993 }),
994 });
995​
996 const prepareCache = yield* Cache.makeWith(prepare, {
997 capacity: 200,
998 // A transient prepare failure must not outlive the lock or missing schema.
999 timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.minutes(10) : Duration.zero),
1000 });
1001​
1002 const statementFor = (sql: string, params: ReadonlyArray<unknown>) =>
1003 hasLargeBinding(params) ? prepare(sql) : Cache.get(prepareCache, sql);
1004​
1005 const runStatement = (
1006 statement: NodeSqlite.StatementSync,
1007 params: ReadonlyArray<unknown>,
1008 mode: "rows" | "raw" | "values" = "rows",
1009 ) =>
1010 Effect.withFiber<ReadonlyArray<any>, SqlError>((fiber) => {
1011 const classification = checkpointClassifications.get(statement);
1012 const execute = () => {
1013 let arrays = false;
1014 let result: Exit.Exit<ReadonlyArray<unknown>, SqlError>;
1015 try {
1016 const value = withClassification(classification, () => {
1017 statement.setReadBigInts(Boolean(Context.get(fiber.context, Client.SafeIntegers)));
1018 if (hasRows(statement)) {
1019 arrays = mode === "values";
1020 if (arrays) statement.setReturnArrays(true);
1021 return statement.all(...(params as any));
1023 const result = statement.run(...(params as any));
1024 return mode === "raw" ? (result as unknown as ReadonlyArray<unknown>) : [];
1025 });
1026 result = Exit.succeed(value);
1027 } catch (cause) {
1028 result = Exit.fail(
1029 new SqlError({
1030 reason: classifyError(cause, "Failed to execute statement", "execute"),
1031 }),
1032 );
1033 } finally {
1034 // OR FAIL may have committed earlier rows even when execution throws.
1035 if (classification?.check) checkpoint?.check();
1037 if (arrays) {
1038 try {
1039 statement.setReturnArrays(false);
1040 } catch (cause) {
1041 return Exit.die(
1042 new SqlError({
1043 reason: classifyError(
1044 cause,
1045 "Failed to reset statement result mode",
1046 "resetResultMode",
1047 ),
1048 }),
1049 );
1052 return result;
1053 };
1054 return checkpoint !== undefined && classification?.backpressure
1055 ? checkpoint.runWithAdmission(execute)
1056 : execute();
1057 });
1058​
1059 const run = (sql: string, params: ReadonlyArray<unknown>, raw = false) =>
1060 Effect.flatMap(statementFor(sql, params), (statement) =>
1061 runStatement(statement, params, raw ? "raw" : "rows"),
1062 );
1063 const runValues = (sql: string, params: ReadonlyArray<unknown>) =>
1064 Effect.flatMap(statementFor(sql, params), (statement) =>
1065 runStatement(statement, params, "values"),
1066 );
1067​
1068 return identity<Connection>({
1069 execute(sql, params, rowTransform) {
1070 return rowTransform ? Effect.map(run(sql, params), rowTransform) : run(sql, params);
1071 },
1072 executeRaw(sql, params) {
1073 return run(sql, params, true);
1074 },
1075 executeValues(sql, params) {
1076 return runValues(sql, params);
1077 },
1078 executeValuesUnprepared(sql, params) {
1079 return Effect.flatMap(prepare(sql), (statement) =>
1080 runStatement(statement, params ?? [], "values"),
1081 );
1082 },
1083 executeUnprepared(sql, params, rowTransform) {
1084 const effect = prepare(sql).pipe(
1085 Effect.flatMap((statement) => runStatement(statement, params ?? [])),
1086 );
1087 return rowTransform ? Effect.map(effect, rowTransform) : effect;
1088 },
1089 executeStream(_sql, _params) {
1090 return Stream.die(new UnsupportedNodeSqliteOperationError());
1091 },
1092 });
1093 });
1094​
1095 const semaphore = yield* Semaphore.make(1);
1096 const connection = yield* makeConnection;
1097 // A reader that cannot start turns off the whole pool, so routing never picks a dead slot.
1098 const readerPool = { unavailable: false };
1099 const readers =
1100 options.readerWorker && isFileDatabase
1101 ? yield* Effect.forEach(Array.from({ length: READER_POOL_SIZE }), () =>
1102 Effect.map(
1103 Effect.all([makeReader(options, readerPool), Semaphore.make(1)]),
1104 ([acquire, lock]) => ({
1105 acquire,
1106 lock,
1107 claims: 0,
1108 }),
1109 ),
1111 : [];
1112​
1113 const writer = { name: "writer" as const, lock: semaphore, connection };
1114 let activeWriter: ConnectionOwner | undefined;
1115 // Callers must run route uninterruptibly and pass `claimed` a finalizer that runs `release` once.
1116 const route = (claimed: (release: Effect.Effect<void>) => Effect.Effect<void>) =>
1117 Effect.withFiber((fiber) => {
1118 if (readers.length === 0 || readerPool.unavailable || !Context.get(fiber.context, ReadOnly)) {
1119 return Effect.succeed(writer);
1121 // Least claimed wins and ties go to the lowest slot, so idle slots never start a worker.
1122 let slot = readers[0]!;
1123 for (const candidate of readers) if (candidate.claims < slot.claims) slot = candidate;
1124 slot.claims++;
1125 const reader = slot;
1126 return Effect.andThen(
1127 claimed(
1128 Effect.sync(() => {
1129 reader.claims--;
1130 }),
1131 ),
1132 Effect.map(reader.acquire, (readerConnection) =>
1133 readerConnection === undefined
1134 ? writer
1135 : { name: "reader" as const, lock: reader.lock, connection: readerConnection },
1136 ),
1137 );
1138 });
1139​
1140 const acquired = (
1141 target: { readonly name: Acquired["connection"] },
1142 clock: Clock.Clock,
1143 start: bigint,
1144 ): Acquired => ({
1145 connection: target.name,
1146 lockWaitMs: Number((clock.monotonicTimeNanosUnsafe() - start) / 1000n) / 1000,
1147 });
1148​
1149 const writerBorrowBatchSize = 16;
1150 let writerBorrowsSinceYield = 0;
1151 const borrower: Borrower = (use) =>
1152 Effect.uninterruptibleMask((restore) => {
1153 let release: Effect.Effect<void> = Effect.void;
1154 const claimed = (next: Effect.Effect<void>) =>
1155 Effect.sync(() => {
1156 release = next;
1157 });
1158 return Effect.flatMap(route(claimed), (target) => restore(borrowTarget(target, use))).pipe(
1159 Effect.ensuring(Effect.suspend(() => release)),
1160 );
1161 });
1162 const borrowTarget = <A, E, R>(
1163 target: {
1164 readonly name: Acquired["connection"];
1165 readonly lock: Semaphore.Semaphore;
1166 readonly connection: Connection;
1167 },
1168 use: (connection: Connection) => Effect.Effect<A, E, R>,
1169 ): Effect.Effect<A, E, R> =>
1170 Effect.withFiber((fiber) => {
1171 const clock = fiber.getRef(Clock.Clock);
1172 const start = clock.monotonicTimeNanosUnsafe();
1173 const ownerAtWaitStart = target.name === "writer" ? activeWriter : undefined;
1174 const owner = target.name === "writer" ? connectionOwner(fiber) : undefined;
1175 const useConnection = Effect.suspend(() => {
1176 if (target.name === "writer") activeWriter = owner;
1177 if (
1178 fiber.getRef(References.TracerEnabled) &&
1179 fiber.getRef(Statement.SpanPropagationEnabled)
1180 ) {
1181 annotateAcquired(fiber, acquired(target, clock, start), ownerAtWaitStart);
1183 return use(target.connection);
1184 });
1185 const useOwnedConnection =
1186 target.name === "writer"
1187 ? Effect.onExit(useConnection, () =>
1188 Effect.sync(() => {
1189 if (activeWriter === owner) activeWriter = undefined;
1190 }),
1192 : useConnection;
1193 if (!options.checkpointWorker || target.name !== "writer") {
1194 return target.lock.withPermits(1)(useOwnedConnection);
1196 const writerUse = Effect.onExit(useOwnedConnection, (exit) =>
1197 Effect.sync(() => {
1198 writerBorrowsSinceYield = Exit.hasInterrupts(exit)
1199 ? 0
1200 : Math.min(writerBorrowsSinceYield + 1, writerBorrowBatchSize);
1201 }),
1202 );
1203 return Effect.flatMap(target.lock.withPermitsIfAvailable(1)(writerUse), (result) =>
1204 Option.isSome(result)
1205 ? Effect.succeed(result.value)
1206 : target.lock.withPermits(1)(
1207 Effect.andThen(
1208 // Failed SQL also consumes the batch; cancellation hands off immediately.
1209 Effect.suspend(() => {
1210 if (writerBorrowsSinceYield < writerBorrowBatchSize) return Effect.void;
1211 writerBorrowsSinceYield = 0;
1212 return Effect.callback<void>((resume) => {
1213 const immediate = setImmediate(() => resume(Effect.void));
1214 return Effect.sync(() => clearImmediate(immediate));
1215 });
1216 }),
1217 writerUse,
1218 ),
1219 ),
1220 );
1221 });
1222 const claimedUntilScopeCloses = (release: Effect.Effect<void>) =>
1223 Effect.withFiber((fiber) =>
1224 Scope.addFinalizer(Context.getUnsafe(fiber.context, Scope.Scope), release),
1225 );
1226 const transactionAcquirer = Effect.uninterruptible(route(claimedUntilScopeCloses)).pipe(
1227 Effect.flatMap((target) =>
1228 Effect.uninterruptibleMask((restore) => {
1229 const fiber = Fiber.getCurrent()!;
1230 const scope = Context.getUnsafe(fiber.context, Scope.Scope);
1231 const clock = fiber.getRef(Clock.Clock);
1232 const start = clock.monotonicTimeNanosUnsafe();
1233 const pending = fiber.getRef(TransactionAcquire);
1234 const ownerAtWaitStart = target.name === "writer" ? activeWriter : undefined;
1235 const owner = target.name === "writer" ? connectionOwner(fiber) : undefined;
1236 const acquire = Effect.tap((pending?.restore ?? restore)(target.lock.take(1)), () => {
1237 if (target.name === "writer") activeWriter = owner;
1238 if (pending !== undefined) {
1239 pending.acquired = acquired(target, clock, start);
1240 pending.ownerAtWaitStart = ownerAtWaitStart;
1242 return Scope.addFinalizer(
1243 scope,
1244 Effect.sync(() => {
1245 if (target.name === "writer" && activeWriter === owner) activeWriter = undefined;
1246 }).pipe(Effect.andThen(target.lock.release(1))),
1247 );
1248 });
1249 return Effect.as(
1250 // A scheduler yield after taking the permit must not precede its release finalizer.
1251 Effect.provideService(acquire, Scheduler.PreventSchedulerYield, true),
1252 target.connection,
1253 );
1254 }),
1255 ),
1256 );
1257​
1258 const client = yield* Client.make({
1259 driver: "node:sqlite",
1260 acquirer: transactionAcquirer,
1261 borrower,
1262 compiler,
1263 transactionAcquirer,
1264 // BEGIN IMMEDIATE avoids read-then-write SQLITE_BUSY_SNAPSHOT, which busy_timeout cannot resolve.
1265 beginTransaction: options.readonly === true ? "BEGIN" : "BEGIN IMMEDIATE",
1266 spanAttributes: [
1267 ...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
1268 [ATTR_DB_SYSTEM_NAME, "sqlite"],
1269 ],
1270 });
1271 const withTransaction = client.withTransaction;
1272 return Object.assign(client, {
1273 withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
1274 Effect.uninterruptibleMask((restore) => {
1275 // SqlClient masks setup; acquisition must preserve the caller's interruptibility.
1276 const pending: {
1277 acquired?: Acquired;
1278 ownerAtWaitStart: ConnectionOwner | undefined;
1279 restore: typeof Effect.interruptible | undefined;
1280 } = {
1281 ownerAtWaitStart: undefined,
1282 restore,
1283 };
1284 const body = Effect.withFiber<A, E, R>((fiber) => {
1285 annotateAcquired(fiber, pending.acquired, pending.ownerAtWaitStart);
1286 pending.restore = undefined;
1287 return effect;
1288 });
1289 return restore(
1290 withTransaction(body).pipe(Effect.provideService(TransactionAcquire, pending)),
1291 );
1292 }),
1293 });
1294});
1295​
1296const makeWorker = Effect.fn("makeWorkerClient")(function* (filename: string) {
1297 yield* checkNodeSqliteCompat();
1298 const acquire = yield* makeReader({ filename }, { unavailable: false }, false);
1299 const connection = yield* acquire;
1300 if (connection === undefined) {
1301 return yield* failed(
1302 new Error("database worker is unavailable"),
1303 readerFailureMessages.open,
1304 "open",
1305 );
1307 const { acquirer, borrower, onCommitFailure, transactionAcquirer } = Client.makeSqliteAcquirers({
1308 connection: Effect.succeed(connection),
1309 semaphore: yield* Semaphore.make(1),
1310 });
1311 return yield* Client.make({
1312 driver: "node:sqlite",
1313 acquirer,
1314 borrower,
1315 compiler: Statement.makeCompilerSqlite(),
1316 transactionAcquirer,
1317 onCommitFailure,
1318 beginTransaction: "BEGIN IMMEDIATE",
1319 spanAttributes: [[ATTR_DB_SYSTEM_NAME, "sqlite"]],
1320 });
1321});
1322​
1323/**
1324 * A read-write client whose one connection runs in a worker thread, so long statements such as
1325 * index builds leave the event loop free. Requires a file database already in WAL mode.
1326 */
1327export const layerWorker = (filename: string): Layer.Layer<Client.SqlClient, SqlError> =>
1328 Layer.effect(Client.SqlClient, makeWorker(filename)).pipe(Layer.provide(Reactivity.layer));
1329​
1330export const layer = (config: SqliteClientConfig): Layer.Layer<Client.SqlClient, SqlError> =>
1331 Layer.effect(Client.SqlClient, make(config)).pipe(Layer.provide(Reactivity.layer));