@noego/sqlego 0.1.4 → 0.1.6-dev.0
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 +49 -5
- package/dist/client.d.ts +3 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +142 -58
- package/dist/protocol.d.ts +36 -0
- package/package.json +6 -6
- package/src/cli.ts +12 -2
- package/src/client.ts +9 -6
- package/src/index.ts +1 -0
- package/src/paths.ts +3 -2
- package/src/protocol.ts +189 -50
- package/src/reader_pool.ts +408 -0
- package/src/reader_worker.ts +189 -0
- package/src/server.ts +422 -180
- package/src/statement_cache.ts +131 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { Database, SQLQueryBindings, Statement as SqliteStatement } from 'bun:sqlite';
|
|
2
|
+
import { Result, Trust } from '@noego/trust';
|
|
3
|
+
import { errorMessage } from './protocol.ts';
|
|
4
|
+
|
|
5
|
+
/** A statement SQLite refused or failed; `code` is SQLite's result code name (`SQLITE_READONLY`) when it gave one. */
|
|
6
|
+
export type SqliteFailure = { readonly kind: 'sqlite'; readonly message: string; readonly code?: string };
|
|
7
|
+
|
|
8
|
+
export const sqliteFailure = (cause: unknown): SqliteFailure => ({
|
|
9
|
+
kind: 'sqlite', message: errorMessage(cause),
|
|
10
|
+
code: typeof cause === 'object' && cause !== null && 'code' in cause && typeof cause.code === 'string' ? cause.code : undefined,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/** Distinct statements each connection keeps prepared; past this, the least recently used is finalized. */
|
|
14
|
+
export const STATEMENT_CACHE_SIZE = 256;
|
|
15
|
+
|
|
16
|
+
export type PreparedStatement = SqliteStatement<unknown, SQLQueryBindings[]>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The prepared statements one connection reuses. Every statement handed out
|
|
20
|
+
* here is finalized here, when evicted and by `clear()` before the connection
|
|
21
|
+
* closes, never left to garbage collection: bun:sqlite's `close()` is
|
|
22
|
+
* sqlite3_close_v2, which keeps a connection with an unfinalized statement
|
|
23
|
+
* open, descriptors and all, until that statement is collected. bun:sqlite's
|
|
24
|
+
* own `db.query()` cache cannot serve: it keeps its first 20 statements and
|
|
25
|
+
* returns every later one uncached and unfinalized (2026-10-17: owners kept
|
|
26
|
+
* thousands of descriptors on released, deleted files).
|
|
27
|
+
*
|
|
28
|
+
* SQL with a `*` anywhere is prepared for one use and finalized after it,
|
|
29
|
+
* never cached. Its result columns (`SELECT *`, `t.*`, `RETURNING *`) follow
|
|
30
|
+
* the schema, and a reused bun:sqlite statement keeps the column names it
|
|
31
|
+
* last read whenever the schema changes without bun:sqlite seeing it: on the
|
|
32
|
+
* reader after the writer's DDL, on the writer after a rollback undoes DDL
|
|
33
|
+
* (2026-10-18, Bun 1.3.6: a cached `SELECT *` on the reader left out a column
|
|
34
|
+
* the writer had just added). A column named in the SQL cannot change that
|
|
35
|
+
* way; one that no longer exists fails when SQLite prepares the statement again.
|
|
36
|
+
*
|
|
37
|
+
* Shared by the owner thread (its writer and in-thread reader) and every
|
|
38
|
+
* reader worker (one cache per file it has open).
|
|
39
|
+
*/
|
|
40
|
+
export class StatementCache {
|
|
41
|
+
private readonly statements = new Map<string, PreparedStatement>();
|
|
42
|
+
private readonly db: Database;
|
|
43
|
+
private readonly onFinalizeFailed: (message: string) => void;
|
|
44
|
+
private used = 0;
|
|
45
|
+
|
|
46
|
+
constructor(db: Database, onFinalizeFailed: (message: string) => void) {
|
|
47
|
+
this.db = db;
|
|
48
|
+
this.onFinalizeFailed = onFinalizeFailed;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** How many statements `use` has run on this connection, failed ones included. */
|
|
52
|
+
get runs(): number {
|
|
53
|
+
return this.used;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Runs `work` on the prepared statement for `sql` and returns its outcome;
|
|
58
|
+
* SQL that SQLite cannot prepare is a sqlite failure.
|
|
59
|
+
*/
|
|
60
|
+
use<T, E>(sql: string, work: (statement: PreparedStatement) => Result<T, E>) {
|
|
61
|
+
this.used += 1;
|
|
62
|
+
if (sql.includes('*')) return this.useOnce(sql, work);
|
|
63
|
+
const prepared = this.prepare(sql);
|
|
64
|
+
if (!prepared.ok) return prepared;
|
|
65
|
+
return work(prepared.value);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Finalizes every statement, so the connection can then close for real. */
|
|
69
|
+
clear(): void {
|
|
70
|
+
for (const statement of this.statements.values()) this.finalize(statement);
|
|
71
|
+
this.statements.clear();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The cached statement for `sql`, prepared on first use. */
|
|
75
|
+
private prepare(sql: string) {
|
|
76
|
+
const cached = this.statements.get(sql);
|
|
77
|
+
if (cached !== undefined) {
|
|
78
|
+
// Re-inserted so the first key is always the least recently used.
|
|
79
|
+
this.statements.delete(sql);
|
|
80
|
+
this.statements.set(sql, cached);
|
|
81
|
+
return Result.ok(cached);
|
|
82
|
+
}
|
|
83
|
+
const prepared = Trust.try((): PreparedStatement => this.db.prepare<unknown, SQLQueryBindings[]>(sql), sqliteFailure);
|
|
84
|
+
if (!prepared.ok) return prepared;
|
|
85
|
+
this.statements.set(sql, prepared.value);
|
|
86
|
+
if (this.statements.size > STATEMENT_CACHE_SIZE) this.evictLeastRecentlyUsed();
|
|
87
|
+
return prepared;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** `use` for SQL that is never cached: prepared, run, finalized. */
|
|
91
|
+
private useOnce<T, E>(sql: string, work: (statement: PreparedStatement) => Result<T, E>) {
|
|
92
|
+
const prepared = Trust.try((): PreparedStatement => this.db.prepare<unknown, SQLQueryBindings[]>(sql), sqliteFailure);
|
|
93
|
+
if (!prepared.ok) return prepared;
|
|
94
|
+
const outcome = work(prepared.value);
|
|
95
|
+
this.finalize(prepared.value);
|
|
96
|
+
return outcome;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private evictLeastRecentlyUsed(): void {
|
|
100
|
+
const oldest = this.statements.entries().next();
|
|
101
|
+
if (oldest.done) return;
|
|
102
|
+
const [sql, statement] = oldest.value;
|
|
103
|
+
this.statements.delete(sql);
|
|
104
|
+
this.finalize(statement);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private finalize(statement: PreparedStatement): void {
|
|
108
|
+
const finalized = Trust.try(() => statement.finalize(), sqliteFailure);
|
|
109
|
+
if (!finalized.ok && finalized.error.kind === 'sqlite') this.onFinalizeFailed(finalized.error.message);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Steps one statement on `db` through `all()`, then reads its columns: when
|
|
115
|
+
* another connection changed the schema, SQLite prepares the statement again
|
|
116
|
+
* as it steps, and only then do its columns match the rows. Read before the
|
|
117
|
+
* step they were the old ones, and `SELECT *` on the reader dropped the
|
|
118
|
+
* columns the writer had just added (2026-10-18). A statement without
|
|
119
|
+
* result columns steps to its end in `all()` just as in `run()`.
|
|
120
|
+
*/
|
|
121
|
+
export function stepStatement(statements: StatementCache, sql: string, args: SQLQueryBindings[]) {
|
|
122
|
+
return statements.use(sql, (statement) => Trust.try(
|
|
123
|
+
() => {
|
|
124
|
+
const rows: unknown = statement.all(...args);
|
|
125
|
+
const columns: unknown = statement.columnNames;
|
|
126
|
+
if (!Array.isArray(columns)) throw new Error('SQLite returned invalid column metadata');
|
|
127
|
+
return { reader: columns.length > 0, rows };
|
|
128
|
+
},
|
|
129
|
+
sqliteFailure,
|
|
130
|
+
));
|
|
131
|
+
}
|