sqlstack 3.6.0 → 3.7.0-dev.1

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.
@@ -2,5 +2,5 @@ export { createSqliteDb } from './sqlite';
2
2
  export { createPgDb } from './pg';
3
3
  export { createMysqlDb } from './mysql';
4
4
  export { createSqlegoDb } from './sqlego';
5
- export type { SqlegoClientLike, SqlegoTransactionLike, SqlegoDbOptions } from './sqlego';
5
+ export type { SqlegoClientLike, SqlegoTransactionLike, SqlegoTargetLike, SqlegoDbOptions, SqlegoParams, SqlegoValue, } from './sqlego';
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,YAAY,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,YAAY,EACV,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,WAAW,GACZ,MAAM,UAAU,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/index.ts"],"names":[],"mappings":";;;AAAA,mCAA0C;AAAjC,wGAAA,cAAc,OAAA;AACvB,2BAAkC;AAAzB,gGAAA,UAAU,OAAA;AACnB,iCAAwC;AAA/B,sGAAA,aAAa,OAAA;AACtB,mCAA0C;AAAjC,wGAAA,cAAc,OAAA","sourcesContent":["export { createSqliteDb } from './sqlite';\nexport { createPgDb } from './pg';\nexport { createMysqlDb } from './mysql';\nexport { createSqlegoDb } from './sqlego';\nexport type { SqlegoClientLike, SqlegoTransactionLike, SqlegoDbOptions } from './sqlego';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adapters/index.ts"],"names":[],"mappings":";;;AAAA,mCAA0C;AAAjC,wGAAA,cAAc,OAAA;AACvB,2BAAkC;AAAzB,gGAAA,UAAU,OAAA;AACnB,iCAAwC;AAA/B,sGAAA,aAAa,OAAA;AACtB,mCAA0C;AAAjC,wGAAA,cAAc,OAAA","sourcesContent":["export { createSqliteDb } from './sqlite';\nexport { createPgDb } from './pg';\nexport { createMysqlDb } from './mysql';\nexport { createSqlegoDb } from './sqlego';\nexport type {\n SqlegoClientLike,\n SqlegoTransactionLike,\n SqlegoTargetLike,\n SqlegoDbOptions,\n SqlegoParams,\n SqlegoValue,\n} from './sqlego';\n"]}
@@ -1,41 +1,94 @@
1
1
  import type { Database } from '../registry';
2
+ type Row = Record<string, unknown>;
3
+ type RunInfo = {
4
+ changes: number;
5
+ lastInsertRowid: number | bigint;
6
+ };
7
+ /** sqlego protocol 3 `statement`: rows and changes for one statement of any kind. */
8
+ type StatementInfo = {
9
+ reader: boolean;
10
+ rows: Row[];
11
+ changes: number;
12
+ lastInsertRowid: number | bigint;
13
+ };
14
+ /** sqlego protocol 3 `exec`: a whole script, no parameters. */
15
+ type ExecInfo = {
16
+ statements: number;
17
+ changes: number;
18
+ lastInsertRowid: number | bigint;
19
+ };
20
+ /** A value sqlego binds (mirrors `@noego/sqlego`'s `SqlValue`). */
21
+ export type SqlegoValue = string | number | bigint | boolean | null | Uint8Array;
2
22
  /**
3
- * The subset of `@noego/sqlego`'s `SqlegoClient` this adapter uses. Typed
4
- * structurally so sqlstack carries no hard dependency on sqlego.
23
+ * Bound parameters as sqlego accepts them (mirrors `@noego/sqlego`'s
24
+ * `SqlParams`). The client's methods take exactly this, so the interfaces
25
+ * below must too: a wider parameter type such as `unknown[]` would reject a
26
+ * real `SqlegoClient`, whose methods can't promise to accept arbitrary values.
5
27
  */
6
- export interface SqlegoClientLike {
7
- query(sql: string, params?: readonly unknown[]): Promise<Record<string, unknown>[]>;
8
- run(sql: string, params?: readonly unknown[]): Promise<{
9
- changes: number;
10
- lastInsertRowid: number | bigint;
11
- }>;
28
+ export type SqlegoParams = ReadonlyArray<SqlegoValue> | Readonly<Record<string, SqlegoValue>>;
29
+ /**
30
+ * What both a client and a transaction expose. `statement`/`exec` arrive with
31
+ * protocol 3 (`@noego/sqlego` >= 0.0.3).
32
+ */
33
+ export interface SqlegoTargetLike {
34
+ query(sql: string, params?: SqlegoParams): Promise<Row[]>;
35
+ run(sql: string, params?: SqlegoParams): Promise<RunInfo>;
36
+ statement?(sql: string, params?: SqlegoParams): Promise<StatementInfo>;
37
+ exec?(sql: string): Promise<ExecInfo>;
38
+ }
39
+ /**
40
+ * The subset of `@noego/sqlego`'s `SqlegoClient` this adapter uses, typed
41
+ * structurally so sqlstack carries no runtime dependency on sqlego. A real
42
+ * `SqlegoClient` satisfies it as is (see tests/sqlego.types.test.ts).
43
+ */
44
+ export interface SqlegoClientLike extends SqlegoTargetLike {
12
45
  begin(options?: {
13
46
  mode?: 'deferred' | 'immediate';
14
47
  leaseMs?: number;
15
48
  }): Promise<SqlegoTransactionLike>;
16
49
  close(): Promise<void> | void;
17
50
  }
18
- export interface SqlegoTransactionLike {
19
- query(sql: string, params?: readonly unknown[]): Promise<Record<string, unknown>[]>;
20
- run(sql: string, params?: readonly unknown[]): Promise<{
21
- changes: number;
22
- lastInsertRowid: number | bigint;
23
- }>;
51
+ export interface SqlegoTransactionLike extends SqlegoTargetLike {
24
52
  commit(): Promise<void>;
25
53
  rollback(): Promise<void>;
26
54
  }
27
55
  export interface SqlegoDbOptions {
28
- /** Lease requested for each transaction (the server caps it). */
56
+ /**
57
+ * Lease requested for each transaction: the server rolls it back when no
58
+ * statement arrives for this long. The server caps it at its own lease
59
+ * (sqlego default 10 000 ms), so it can only be shortened here.
60
+ */
29
61
  leaseMs?: number;
30
62
  }
31
63
  /**
32
- * SQLite through a sqlego owner process. Behaves like a single SQLite
33
- * connection: `BEGIN` opens a leased sqlego transaction and every statement
34
- * runs inside it until `COMMIT`/`ROLLBACK`, so sqlstack transactions,
35
- * savepoints, `rollback()` and `afterCommit` work unchanged.
64
+ * SQLite through a sqlego owner process.
65
+ *
66
+ * sqlego binds a transaction to the connection that opened it, and every
67
+ * statement that connection sends while it is open runs inside it. So each
68
+ * sqlstack transaction gets its own handle (`transactionHandle`): BEGIN opens
69
+ * a leased sqlego transaction there, its statements carry the transaction id,
70
+ * and COMMIT/ROLLBACK end it. Statements other flows send to the shared
71
+ * handle wait until it ends instead of joining it, and transactions from
72
+ * different flows take turns. Savepoints pass through as SQL, so
73
+ * `@transaction`, nested scopes, `rollback()` and `afterCommit` work as on
74
+ * any SQLite connection.
75
+ *
76
+ * Lease failures surface as TransactionExpiredError (`tx-expired`: the server
77
+ * rolled the transaction back — idle past the lease, past the hard cap, or
78
+ * the connection dropped) and TransactionBusyError (`tx-busy`: another
79
+ * process's transaction kept this request waiting past the server's queue
80
+ * timeout; nothing ran). A ROLLBACK of a transaction the server already
81
+ * rolled back succeeds.
82
+ *
83
+ * With a protocol-3 client (`@noego/sqlego` >= 0.0.3) statements run through
84
+ * `statement`, which tells rows from writes using SQLite's own column
85
+ * metadata and returns 64-bit integers as `bigint` and blobs as
86
+ * `Uint8Array`; parameterless multi-statement scripts run through `exec`.
87
+ * Older clients fall back to `query`/`run` chosen from the SQL text.
36
88
  *
37
89
  * Pass a connected `SqlegoClient`, or a database file path (requires the
38
90
  * optional `@noego/sqlego` package).
39
91
  */
40
92
  export declare function createSqlegoDb(fileOrClient: string | SqlegoClientLike, options?: SqlegoDbOptions): Promise<Database>;
93
+ export {};
41
94
  //# sourceMappingURL=sqlego.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sqlego.d.ts","sourceRoot":"","sources":["../../../src/adapters/sqlego.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAe,MAAM,aAAa,CAAC;AAGzD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;IACpF,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC,CAAC;IAC9G,KAAK,CAAC,OAAO,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,UAAU,GAAG,WAAW,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;IACvG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC/B;AAED,MAAM,WAAW,qBAAqB;IACpC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;IACpF,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAA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ineProperty(exports, "__esModule", { value: true });
36
36
  exports.createSqlegoDb = createSqlegoDb;
37
+ const errors_1 = require("../core/errors");
37
38
  const sqlite_1 = require("./sqlite");
38
39
  // Top-level transaction control only; `ROLLBACK TO [SAVEPOINT] x`, SAVEPOINT
39
40
  // and RELEASE are ordinary SQL inside a sqlego transaction.
@@ -48,11 +49,74 @@ function transactionControl(sql) {
48
49
  return { kind: 'rollback' };
49
50
  return null;
50
51
  }
52
+ function errorCode(err) {
53
+ return err?.code;
54
+ }
55
+ /** sqlego's lease errors become typed sqlstack errors; anything else passes through. */
56
+ function mapError(err) {
57
+ const code = errorCode(err);
58
+ if (code !== 'tx-expired' && code !== 'tx-busy')
59
+ return err;
60
+ const message = err?.message;
61
+ const text = typeof message === 'string' && message ? message : String(err);
62
+ return code === 'tx-expired' ? new errors_1.TransactionExpiredError(text, err) : new errors_1.TransactionBusyError(text, err);
63
+ }
64
+ /** `statement` refuses text holding several statements, before running any of it. */
65
+ function isSeveralStatementsRefusal(err) {
66
+ const message = err?.message;
67
+ return errorCode(err) === 'bad-request' && typeof message === 'string' && /use exec for several/.test(message);
68
+ }
69
+ async function execute(target, sql, rawParams) {
70
+ // sqlstack binds positional values as unknown[]; the sqlego client checks
71
+ // and encodes each value on send and rejects any it can't bind.
72
+ const params = rawParams;
73
+ if (typeof target.statement === 'function') {
74
+ try {
75
+ const out = await target.statement(sql, params);
76
+ if (out.reader)
77
+ return out.rows;
78
+ return { rowsAffected: Number(out.changes ?? 0), lastInsertId: out.lastInsertRowid };
79
+ }
80
+ catch (err) {
81
+ // Parameterless multi-statement text (a schema or migration script)
82
+ // runs through `exec`, which splits it the way SQLite does.
83
+ if (params.length > 0 || typeof target.exec !== 'function' || !isSeveralStatementsRefusal(err))
84
+ throw err;
85
+ const out = await target.exec(sql);
86
+ return { rowsAffected: Number(out.changes ?? 0), lastInsertId: out.lastInsertRowid };
87
+ }
88
+ }
89
+ // Protocol-2 clients report no column metadata: decide from the SQL text.
90
+ if ((0, sqlite_1.producesRows)(sql))
91
+ return await target.query(sql, params);
92
+ const info = await target.run(sql, params);
93
+ return { rowsAffected: Number(info?.changes ?? 0), lastInsertId: info?.lastInsertRowid };
94
+ }
51
95
  /**
52
- * SQLite through a sqlego owner process. Behaves like a single SQLite
53
- * connection: `BEGIN` opens a leased sqlego transaction and every statement
54
- * runs inside it until `COMMIT`/`ROLLBACK`, so sqlstack transactions,
55
- * savepoints, `rollback()` and `afterCommit` work unchanged.
96
+ * SQLite through a sqlego owner process.
97
+ *
98
+ * sqlego binds a transaction to the connection that opened it, and every
99
+ * statement that connection sends while it is open runs inside it. So each
100
+ * sqlstack transaction gets its own handle (`transactionHandle`): BEGIN opens
101
+ * a leased sqlego transaction there, its statements carry the transaction id,
102
+ * and COMMIT/ROLLBACK end it. Statements other flows send to the shared
103
+ * handle wait until it ends instead of joining it, and transactions from
104
+ * different flows take turns. Savepoints pass through as SQL, so
105
+ * `@transaction`, nested scopes, `rollback()` and `afterCommit` work as on
106
+ * any SQLite connection.
107
+ *
108
+ * Lease failures surface as TransactionExpiredError (`tx-expired`: the server
109
+ * rolled the transaction back — idle past the lease, past the hard cap, or
110
+ * the connection dropped) and TransactionBusyError (`tx-busy`: another
111
+ * process's transaction kept this request waiting past the server's queue
112
+ * timeout; nothing ran). A ROLLBACK of a transaction the server already
113
+ * rolled back succeeds.
114
+ *
115
+ * With a protocol-3 client (`@noego/sqlego` >= 0.0.3) statements run through
116
+ * `statement`, which tells rows from writes using SQLite's own column
117
+ * metadata and returns 64-bit integers as `bigint` and blobs as
118
+ * `Uint8Array`; parameterless multi-statement scripts run through `exec`.
119
+ * Older clients fall back to `query`/`run` chosen from the SQL text.
56
120
  *
57
121
  * Pass a connected `SqlegoClient`, or a database file path (requires the
58
122
  * optional `@noego/sqlego` package).
@@ -76,37 +140,127 @@ async function createSqlegoDb(fileOrClient, options = {}) {
76
140
  else {
77
141
  client = fileOrClient;
78
142
  }
79
- let tx;
80
- async function execute(target, sql, params) {
81
- if ((0, sqlite_1.producesRows)(sql))
82
- return await target.query(sql, params);
83
- const info = await target.run(sql, params);
84
- return { rowsAffected: Number(info?.changes ?? 0), lastInsertId: info?.lastInsertRowid };
143
+ // Settles when the transaction open on this client ends; null while none is
144
+ // open. A sqlego connection holds at most one transaction at a time.
145
+ let gate = null;
146
+ // Callers re-check `gate` and act in the same synchronous step after the
147
+ // loop: an extra `await` in between would let another flow open a
148
+ // transaction first.
149
+ async function takeTurn() {
150
+ while (gate)
151
+ await gate;
152
+ let open;
153
+ gate = new Promise((resolve) => {
154
+ open = resolve;
155
+ });
156
+ let done = false;
157
+ return () => {
158
+ if (done)
159
+ return;
160
+ done = true;
161
+ gate = null;
162
+ open();
163
+ };
164
+ }
165
+ /**
166
+ * One connection-like handle: statements after its own BEGIN join that
167
+ * transaction; without one open, its statements wait out any other
168
+ * handle's transaction.
169
+ */
170
+ function handle() {
171
+ let tx;
172
+ let endTurn;
173
+ let failedCommit = false;
174
+ const finish = () => {
175
+ tx = undefined;
176
+ const end = endTurn;
177
+ endTurn = undefined;
178
+ end?.();
179
+ };
180
+ return {
181
+ dialect: 'sqlite',
182
+ conn: client,
183
+ close() { },
184
+ async query(sql, params) {
185
+ const control = transactionControl(sql);
186
+ if (control?.kind === 'begin') {
187
+ if (tx)
188
+ throw new Error('cannot start a transaction within a transaction');
189
+ const end = await takeTurn();
190
+ try {
191
+ tx = await client.begin({ mode: control.mode, leaseMs: options.leaseMs });
192
+ }
193
+ catch (err) {
194
+ end();
195
+ throw mapError(err);
196
+ }
197
+ endTurn = end;
198
+ return { rowsAffected: 0 };
199
+ }
200
+ if (control) {
201
+ const open = tx;
202
+ if (!open) {
203
+ // A failed COMMIT already ended the transaction on the server
204
+ // (sqlego rolls it back); the caller's follow-up ROLLBACK is moot.
205
+ if (control.kind === 'rollback' && failedCommit) {
206
+ failedCommit = false;
207
+ return { rowsAffected: 0 };
208
+ }
209
+ throw new Error(`cannot ${control.kind} - no transaction is active`);
210
+ }
211
+ failedCommit = false;
212
+ try {
213
+ if (control.kind === 'commit') {
214
+ try {
215
+ await open.commit();
216
+ }
217
+ catch (err) {
218
+ failedCommit = true;
219
+ throw err;
220
+ }
221
+ }
222
+ else {
223
+ try {
224
+ await open.rollback();
225
+ }
226
+ catch (err) {
227
+ // Already rolled back by the server (lease, cap, abort).
228
+ const code = errorCode(err);
229
+ if (code !== 'tx-expired' && code !== 'tx-invalid')
230
+ throw err;
231
+ }
232
+ }
233
+ }
234
+ catch (err) {
235
+ throw mapError(err);
236
+ }
237
+ finally {
238
+ finish();
239
+ }
240
+ return { rowsAffected: 0 };
241
+ }
242
+ try {
243
+ if (tx)
244
+ return await execute(tx, sql, params);
245
+ // Wait out another flow's transaction, then send at once: requests
246
+ // on one sqlego connection are served in order, so this statement
247
+ // reaches the server before any BEGIN queued after it.
248
+ while (gate)
249
+ await gate;
250
+ return await execute(client, sql, params);
251
+ }
252
+ catch (err) {
253
+ throw mapError(err);
254
+ }
255
+ },
256
+ };
85
257
  }
258
+ const shared = handle();
86
259
  return {
87
260
  dialect: 'sqlite',
88
- async query(sql, params) {
89
- const control = transactionControl(sql);
90
- if (control?.kind === 'begin') {
91
- if (tx)
92
- throw new Error('cannot start a transaction within a transaction');
93
- tx = await client.begin({ mode: control.mode, leaseMs: options.leaseMs });
94
- return { rowsAffected: 0 };
95
- }
96
- if (control) {
97
- const open = tx;
98
- if (!open)
99
- throw new Error(`cannot ${control.kind} - no transaction is active`);
100
- tx = undefined;
101
- if (control.kind === 'commit')
102
- await open.commit();
103
- else
104
- await open.rollback();
105
- return { rowsAffected: 0 };
106
- }
107
- return await execute(tx ?? client, sql, params);
108
- },
109
261
  conn: client,
262
+ query: shared.query,
263
+ transactionHandle: () => handle(),
110
264
  async close() {
111
265
  if (owned)
112
266
  await client.close();
@@ -1 +1 @@
1
- {"version":3,"file":"sqlego.js","sourceRoot":"","sources":["../../../src/adapters/sqlego.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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C,IAAI;oBAAE,MAAM,IAAI,KAAK,CAAC,UAAU,OAAO,CAAC,IAAI,6BAA6B,CAAC,CAAC;gBAChF,EAAE,GAAG,SAAS,CAAC;gBACf,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ;oBAAE,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;;oBAC9C,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;gBAC3B,OAAO,EAAE,YAAY,EAAE,CAAC,EAAE,CAAC;YAC7B,CAAC;YACD,OAAO,MAAM,OAAO,CAAC,EAAE,IAAI,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;QAClD,CAAC;QACD,IAAI,EAAE,MAAM;QACZ,KAAK,CAAC,KAAK;YACT,IAAI,KAAK;gBAAE,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QAClC,CAAC;KACU,CAAC;AAChB,CAAC","sourcesContent":["import type { Database, QueryResult } from '../registry';\nimport { producesRows, stripLeadingComments } from './sqlite';\n\n/**\n * The subset of `@noego/sqlego`'s `SqlegoClient` this adapter uses. Typed\n * structurally so sqlstack carries no hard dependency on sqlego.\n */\nexport interface SqlegoClientLike {\n query(sql: string, params?: readonly unknown[]): Promise<Record<string, unknown>[]>;\n run(sql: string, params?: readonly unknown[]): Promise<{ changes: number; lastInsertRowid: number | bigint }>;\n begin(options?: { mode?: 'deferred' | 'immediate'; leaseMs?: number }): Promise<SqlegoTransactionLike>;\n close(): Promise<void> | void;\n}\n\nexport interface SqlegoTransactionLike {\n query(sql: string, params?: readonly unknown[]): Promise<Record<string, unknown>[]>;\n run(sql: string, params?: readonly unknown[]): Promise<{ changes: number; lastInsertRowid: number | bigint }>;\n commit(): Promise<void>;\n rollback(): Promise<void>;\n}\n\nexport interface SqlegoDbOptions {\n /** Lease requested for each transaction (the server caps it). */\n leaseMs?: number;\n}\n\ntype Control = { kind: 'begin'; mode: 'deferred' | 'immediate' } | { kind: 'commit' } | { kind: 'rollback' };\n\n// Top-level transaction control only; `ROLLBACK TO [SAVEPOINT] x`, SAVEPOINT\n// and RELEASE are ordinary SQL inside a sqlego transaction.\nfunction transactionControl(sql: string): Control | null {\n const text = stripLeadingComments(String(sql)).trim().replace(/;\\s*$/, '').trim().toUpperCase().replace(/\\s+/g, ' ');\n const begin = /^BEGIN(?: (DEFERRED|IMMEDIATE|EXCLUSIVE))?(?: TRANSACTION)?$/.exec(text);\n if (begin) return { kind: 'begin', mode: begin[1] === undefined || begin[1] === 'DEFERRED' ? 'deferred' : 'immediate' };\n if (/^(?:COMMIT|END)(?: TRANSACTION)?$/.test(text)) return { kind: 'commit' };\n if (/^ROLLBACK(?: TRANSACTION)?$/.test(text)) return { kind: 'rollback' };\n return null;\n}\n\n/**\n * SQLite through a sqlego owner process. Behaves like a single SQLite\n * connection: `BEGIN` opens a leased sqlego transaction and every statement\n * runs inside it until `COMMIT`/`ROLLBACK`, so sqlstack transactions,\n * savepoints, `rollback()` and `afterCommit` work unchanged.\n *\n * Pass a connected `SqlegoClient`, or a database file path (requires the\n * optional `@noego/sqlego` package).\n */\nexport async function createSqlegoDb(fileOrClient: string | SqlegoClientLike, options: SqlegoDbOptions = {}): Promise<Database> {\n let client: SqlegoClientLike;\n let owned = false;\n if (typeof fileOrClient === 'string') {\n let mod: any;\n try {\n const packageName = '@noego/sqlego';\n mod = await import(/* @vite-ignore */ packageName);\n } catch {\n throw new Error('createSqlegoDb(file) requires the @noego/sqlego package. Install it or pass a connected SqlegoClient.');\n }\n const SqlegoClient = mod.SqlegoClient ?? mod.default?.SqlegoClient;\n client = await SqlegoClient.connect(fileOrClient);\n owned = true;\n } else {\n client = fileOrClient;\n }\n\n let tx: SqlegoTransactionLike | undefined;\n\n async function execute(target: SqlegoClientLike | SqlegoTransactionLike, sql: string, params: unknown[]): Promise<QueryResult> {\n if (producesRows(sql)) return await target.query(sql, params);\n const info = await target.run(sql, params);\n return { rowsAffected: Number(info?.changes ?? 0), lastInsertId: info?.lastInsertRowid };\n }\n\n return {\n dialect: 'sqlite',\n async query(sql: string, params: unknown[]) {\n const control = transactionControl(sql);\n if (control?.kind === 'begin') {\n if (tx) throw new Error('cannot start a transaction within a transaction');\n tx = await client.begin({ mode: control.mode, leaseMs: options.leaseMs });\n return { rowsAffected: 0 };\n }\n if (control) {\n const open = tx;\n if (!open) throw new Error(`cannot ${control.kind} - no transaction is active`);\n tx = undefined;\n if (control.kind === 'commit') await open.commit();\n else await open.rollback();\n return { rowsAffected: 0 };\n }\n return await execute(tx ?? client, sql, params);\n },\n conn: client,\n async close() {\n if (owned) await client.close();\n },\n } as Database;\n}\n"]}
1
+ {"version":3,"file":"sqlego.js","sourceRoot":"","sources":["../../../src/adapters/sqlego.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4IA,wCAuIC;AAlRD,2CAA+E;AAC/E,qCAA8D;AAwD9D,6EAA6E;AAC7E,4DAA4D;AAC5D,SAAS,kBAAkB,CAAC,GAAW;IACrC,MAAM,IAAI,GAAG,IAAA,6BAAoB,EAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACrH,MAAM,KAAK,GAAG,8DAA8D,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxF,IAAI,KAAK;QAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;IACxH,IAAI,mCAAmC,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IAC9E,IAAI,6BAA6B,CAAC,IAAI,CAAC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,EAAE,MAAM;QACZ,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,iBAAiB,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE;QACjC,KAAK,CAAC,KAAK;YACT,IAAI,KAAK;gBAAE,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QAClC,CAAC;KACU,CAAC;AAChB,CAAC","sourcesContent":["import type { Database, QueryResult } from '../registry';\nimport { TransactionBusyError, TransactionExpiredError } from '../core/errors';\nimport { producesRows, stripLeadingComments } from './sqlite';\n\ntype Row = Record<string, unknown>;\ntype RunInfo = { changes: number; lastInsertRowid: number | bigint };\n/** sqlego protocol 3 `statement`: rows and changes for one statement of any kind. */\ntype StatementInfo = { reader: boolean; rows: Row[]; changes: number; lastInsertRowid: number | bigint };\n/** sqlego protocol 3 `exec`: a whole script, no parameters. */\ntype ExecInfo = { statements: number; changes: number; lastInsertRowid: number | bigint };\n\n/** A value sqlego binds (mirrors `@noego/sqlego`'s `SqlValue`). */\nexport type SqlegoValue = string | number | bigint | boolean | null | Uint8Array;\n/**\n * Bound parameters as sqlego accepts them (mirrors `@noego/sqlego`'s\n * `SqlParams`). The client's methods take exactly this, so the interfaces\n * below must too: a wider parameter type such as `unknown[]` would reject a\n * real `SqlegoClient`, whose methods can't promise to accept arbitrary values.\n */\nexport type SqlegoParams = ReadonlyArray<SqlegoValue> | Readonly<Record<string, SqlegoValue>>;\n\n/**\n * What both a client and a transaction expose. `statement`/`exec` arrive with\n * protocol 3 (`@noego/sqlego` >= 0.0.3).\n */\nexport interface SqlegoTargetLike {\n query(sql: string, params?: SqlegoParams): Promise<Row[]>;\n run(sql: string, params?: SqlegoParams): Promise<RunInfo>;\n statement?(sql: string, params?: SqlegoParams): Promise<StatementInfo>;\n exec?(sql: string): Promise<ExecInfo>;\n}\n\n/**\n * The subset of `@noego/sqlego`'s `SqlegoClient` this adapter uses, typed\n * structurally so sqlstack carries no runtime dependency on sqlego. A real\n * `SqlegoClient` satisfies it as is (see tests/sqlego.types.test.ts).\n */\nexport interface SqlegoClientLike extends SqlegoTargetLike {\n begin(options?: { mode?: 'deferred' | 'immediate'; leaseMs?: number }): Promise<SqlegoTransactionLike>;\n close(): Promise<void> | void;\n}\n\nexport interface SqlegoTransactionLike extends SqlegoTargetLike {\n commit(): Promise<void>;\n rollback(): Promise<void>;\n}\n\nexport interface SqlegoDbOptions {\n /**\n * Lease requested for each transaction: the server rolls it back when no\n * statement arrives for this long. The server caps it at its own lease\n * (sqlego default 10 000 ms), so it can only be shortened here.\n */\n leaseMs?: number;\n}\n\ntype Control = { kind: 'begin'; mode: 'deferred' | 'immediate' } | { kind: 'commit' } | { kind: 'rollback' };\n\n// Top-level transaction control only; `ROLLBACK TO [SAVEPOINT] x`, SAVEPOINT\n// and RELEASE are ordinary SQL inside a sqlego transaction.\nfunction transactionControl(sql: string): Control | null {\n const text = stripLeadingComments(String(sql)).trim().replace(/;\\s*$/, '').trim().toUpperCase().replace(/\\s+/g, ' ');\n const begin = /^BEGIN(?: (DEFERRED|IMMEDIATE|EXCLUSIVE))?(?: TRANSACTION)?$/.exec(text);\n if (begin) return { kind: 'begin', mode: begin[1] === undefined || begin[1] === 'DEFERRED' ? 'deferred' : 'immediate' };\n if (/^(?:COMMIT|END)(?: TRANSACTION)?$/.test(text)) return { kind: 'commit' };\n if (/^ROLLBACK(?: TRANSACTION)?$/.test(text)) return { kind: 'rollback' };\n return null;\n}\n\nfunction errorCode(err: unknown): unknown {\n return (err as { code?: unknown } | null)?.code;\n}\n\n/** sqlego's lease errors become typed sqlstack errors; anything else passes through. */\nfunction mapError(err: unknown): unknown {\n const code = errorCode(err);\n if (code !== 'tx-expired' && code !== 'tx-busy') return err;\n const message = (err as { message?: unknown } | null)?.message;\n const text = typeof message === 'string' && message ? message : String(err);\n return code === 'tx-expired' ? new TransactionExpiredError(text, err) : new TransactionBusyError(text, err);\n}\n\n/** `statement` refuses text holding several statements, before running any of it. */\nfunction isSeveralStatementsRefusal(err: unknown): boolean {\n const message = (err as { message?: unknown } | null)?.message;\n return errorCode(err) === 'bad-request' && typeof message === 'string' && /use exec for several/.test(message);\n}\n\nasync function execute(target: SqlegoTargetLike, sql: string, rawParams: unknown[]): Promise<QueryResult> {\n // sqlstack binds positional values as unknown[]; the sqlego client checks\n // and encodes each value on send and rejects any it can't bind.\n const params = rawParams as ReadonlyArray<SqlegoValue>;\n if (typeof target.statement === 'function') {\n try {\n const out = await target.statement(sql, params);\n if (out.reader) return out.rows;\n return { rowsAffected: Number(out.changes ?? 0), lastInsertId: out.lastInsertRowid };\n } catch (err) {\n // Parameterless multi-statement text (a schema or migration script)\n // runs through `exec`, which splits it the way SQLite does.\n if (params.length > 0 || typeof target.exec !== 'function' || !isSeveralStatementsRefusal(err)) throw err;\n const out = await target.exec(sql);\n return { rowsAffected: Number(out.changes ?? 0), lastInsertId: out.lastInsertRowid };\n }\n }\n // Protocol-2 clients report no column metadata: decide from the SQL text.\n if (producesRows(sql)) return await target.query(sql, params);\n const info = await target.run(sql, params);\n return { rowsAffected: Number(info?.changes ?? 0), lastInsertId: info?.lastInsertRowid };\n}\n\n/**\n * SQLite through a sqlego owner process.\n *\n * sqlego binds a transaction to the connection that opened it, and every\n * statement that connection sends while it is open runs inside it. So each\n * sqlstack transaction gets its own handle (`transactionHandle`): BEGIN opens\n * a leased sqlego transaction there, its statements carry the transaction id,\n * and COMMIT/ROLLBACK end it. Statements other flows send to the shared\n * handle wait until it ends instead of joining it, and transactions from\n * different flows take turns. Savepoints pass through as SQL, so\n * `@transaction`, nested scopes, `rollback()` and `afterCommit` work as on\n * any SQLite connection.\n *\n * Lease failures surface as TransactionExpiredError (`tx-expired`: the server\n * rolled the transaction back — idle past the lease, past the hard cap, or\n * the connection dropped) and TransactionBusyError (`tx-busy`: another\n * process's transaction kept this request waiting past the server's queue\n * timeout; nothing ran). A ROLLBACK of a transaction the server already\n * rolled back succeeds.\n *\n * With a protocol-3 client (`@noego/sqlego` >= 0.0.3) statements run through\n * `statement`, which tells rows from writes using SQLite's own column\n * metadata and returns 64-bit integers as `bigint` and blobs as\n * `Uint8Array`; parameterless multi-statement scripts run through `exec`.\n * Older clients fall back to `query`/`run` chosen from the SQL text.\n *\n * Pass a connected `SqlegoClient`, or a database file path (requires the\n * optional `@noego/sqlego` package).\n */\nexport async function createSqlegoDb(fileOrClient: string | SqlegoClientLike, options: SqlegoDbOptions = {}): Promise<Database> {\n let client: SqlegoClientLike;\n let owned = false;\n if (typeof fileOrClient === 'string') {\n let mod: any;\n try {\n const packageName = '@noego/sqlego';\n mod = await import(/* @vite-ignore */ packageName);\n } catch {\n throw new Error('createSqlegoDb(file) requires the @noego/sqlego package. Install it or pass a connected SqlegoClient.');\n }\n const SqlegoClient = mod.SqlegoClient ?? mod.default?.SqlegoClient;\n client = await SqlegoClient.connect(fileOrClient);\n owned = true;\n } else {\n client = fileOrClient;\n }\n\n // Settles when the transaction open on this client ends; null while none is\n // open. A sqlego connection holds at most one transaction at a time.\n let gate: Promise<void> | null = null;\n\n // Callers re-check `gate` and act in the same synchronous step after the\n // loop: an extra `await` in between would let another flow open a\n // transaction first.\n async function takeTurn(): Promise<() => void> {\n while (gate) await gate;\n let open!: () => void;\n gate = new Promise<void>((resolve) => {\n open = resolve;\n });\n let done = false;\n return () => {\n if (done) return;\n done = true;\n gate = null;\n open();\n };\n }\n\n /**\n * One connection-like handle: statements after its own BEGIN join that\n * transaction; without one open, its statements wait out any other\n * handle's transaction.\n */\n function handle(): Database {\n let tx: SqlegoTransactionLike | undefined;\n let endTurn: (() => void) | undefined;\n let failedCommit = false;\n\n const finish = () => {\n tx = undefined;\n const end = endTurn;\n endTurn = undefined;\n end?.();\n };\n\n return {\n dialect: 'sqlite',\n conn: client,\n close() {},\n async query(sql: string, params: unknown[]) {\n const control = transactionControl(sql);\n if (control?.kind === 'begin') {\n if (tx) throw new Error('cannot start a transaction within a transaction');\n const end = await takeTurn();\n try {\n tx = await client.begin({ mode: control.mode, leaseMs: options.leaseMs });\n } catch (err) {\n end();\n throw mapError(err);\n }\n endTurn = end;\n return { rowsAffected: 0 };\n }\n if (control) {\n const open = tx;\n if (!open) {\n // A failed COMMIT already ended the transaction on the server\n // (sqlego rolls it back); the caller's follow-up ROLLBACK is moot.\n if (control.kind === 'rollback' && failedCommit) {\n failedCommit = false;\n return { rowsAffected: 0 };\n }\n throw new Error(`cannot ${control.kind} - no transaction is active`);\n }\n failedCommit = false;\n try {\n if (control.kind === 'commit') {\n try {\n await open.commit();\n } catch (err) {\n failedCommit = true;\n throw err;\n }\n } else {\n try {\n await open.rollback();\n } catch (err) {\n // Already rolled back by the server (lease, cap, abort).\n const code = errorCode(err);\n if (code !== 'tx-expired' && code !== 'tx-invalid') throw err;\n }\n }\n } catch (err) {\n throw mapError(err);\n } finally {\n finish();\n }\n return { rowsAffected: 0 };\n }\n try {\n if (tx) return await execute(tx, sql, params);\n // Wait out another flow's transaction, then send at once: requests\n // on one sqlego connection are served in order, so this statement\n // reaches the server before any BEGIN queued after it.\n while (gate) await gate;\n return await execute(client, sql, params);\n } catch (err) {\n throw mapError(err);\n }\n },\n } as Database;\n }\n\n const shared = handle();\n return {\n dialect: 'sqlite',\n conn: client,\n query: shared.query,\n transactionHandle: () => handle(),\n async close() {\n if (owned) await client.close();\n },\n } as Database;\n}\n"]}
@@ -33,6 +33,28 @@ export declare class DatabaseError extends SqlStackError {
33
33
  /** Driver error code copied from `cause` when it is a string. */
34
34
  code?: string;
35
35
  }
36
+ /**
37
+ * The database ended the transaction on its own and rolled it back: a sqlego
38
+ * lease ran out (no statement within the lease), the transaction hit the
39
+ * server's hard cap on its duration, or the client connection dropped.
40
+ * `code` is `'tx-expired'`. Statements fail with it (wrapped in
41
+ * DatabaseError, which copies the code); COMMIT fails with it directly.
42
+ */
43
+ export declare class TransactionExpiredError extends SqlStackError {
44
+ constructor(message: string, cause?: unknown);
45
+ readonly code = "tx-expired";
46
+ cause?: unknown;
47
+ }
48
+ /**
49
+ * Another process held the database in a transaction for longer than the
50
+ * server lets a request wait (sqlego's queue timeout). Nothing ran; the
51
+ * statement or BEGIN can be retried. `code` is `'tx-busy'`.
52
+ */
53
+ export declare class TransactionBusyError extends SqlStackError {
54
+ constructor(message: string, cause?: unknown);
55
+ readonly code = "tx-busy";
56
+ cause?: unknown;
57
+ }
36
58
  export declare class RollbackTransactionError extends SqlStackError {
37
59
  constructor(message?: string, cause?: unknown);
38
60
  cause?: unknown;
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/core/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,aAAc,SAAQ,KAAK;gBAC1B,OAAO,CAAC,EAAE,MAAM;CAI7B;AAED,qBAAa,kBAAmB,SAAQ,aAAa;gBACvC,WAAW,EAAE,MAAM;CAIhC;AAED,qBAAa,WAAY,SAAQ,aAAa;;CAK7C;AAED,qBAAa,cAAe,SAAQ,aAAa;gBACnC,WAAW,EAAE,MAAM;CAIhC;AAKD,qBAAa,iBAAkB,SAAQ,aAAa;gBACtC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAUzC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,eAAgB,SAAQ,aAAa;gBACpC,OAAO,EAAE,MAAM;IAK3B,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;GAMG;AACH,qBAAa,aAAc,SAAQ,aAAa;gBAClC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK;IAQvD,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAID,qBAAa,wBAAyB,SAAQ,aAAa;gBAC7C,OAAO,SAA4B,EAAE,KAAK,CAAC,EAAE,OAAO;IAKhE,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":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AK,CAAC,EAAE,OAAO,CAAC;CACjB"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RollbackTransactionError = exports.DatabaseError = exports.ValidationError = exports.ExpectedRowsError = exports.RowsExistError = exports.NoRowsError = exports.ExactlyOneRowError = exports.SqlStackError = void 0;
3
+ exports.RollbackTransactionError = exports.TransactionBusyError = exports.TransactionExpiredError = exports.DatabaseError = exports.ValidationError = exports.ExpectedRowsError = exports.RowsExistError = exports.NoRowsError = exports.ExactlyOneRowError = exports.SqlStackError = void 0;
4
4
  class SqlStackError extends Error {
5
5
  constructor(message) {
6
6
  super(message);
@@ -70,6 +70,36 @@ class DatabaseError extends SqlStackError {
70
70
  }
71
71
  }
72
72
  exports.DatabaseError = DatabaseError;
73
+ /**
74
+ * The database ended the transaction on its own and rolled it back: a sqlego
75
+ * lease ran out (no statement within the lease), the transaction hit the
76
+ * server's hard cap on its duration, or the client connection dropped.
77
+ * `code` is `'tx-expired'`. Statements fail with it (wrapped in
78
+ * DatabaseError, which copies the code); COMMIT fails with it directly.
79
+ */
80
+ class TransactionExpiredError extends SqlStackError {
81
+ constructor(message, cause) {
82
+ super(message);
83
+ this.code = 'tx-expired';
84
+ this.cause = cause;
85
+ this.name = 'TransactionExpiredError';
86
+ }
87
+ }
88
+ exports.TransactionExpiredError = TransactionExpiredError;
89
+ /**
90
+ * Another process held the database in a transaction for longer than the
91
+ * server lets a request wait (sqlego's queue timeout). Nothing ran; the
92
+ * statement or BEGIN can be retried. `code` is `'tx-busy'`.
93
+ */
94
+ class TransactionBusyError extends SqlStackError {
95
+ constructor(message, cause) {
96
+ super(message);
97
+ this.code = 'tx-busy';
98
+ this.cause = cause;
99
+ this.name = 'TransactionBusyError';
100
+ }
101
+ }
102
+ exports.TransactionBusyError = TransactionBusyError;
73
103
  // Signals that the current transaction should be rolled back.
74
104
  // If `cause` is provided, wrappers may rethrow it after rollback.
75
105
  class RollbackTransactionError extends Sq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aC;AAED,MAAa,eAAgB,SAAQ,aAAa;IAChD,YAAY,OAAe;QACzB,KAAK,CAAC,sBAAsB,OAAO,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CAEF;AAPD,0CAOC;AAED;;;;;;GAMG;AACH,MAAa,aAAc,SAAQ,aAAa;IAC9C,YAAY,OAAe,EAAE,GAAW,EAAE,KAAa;QACrD,KAAK,CAAC,GAAG,OAAO,UAAU,GAAG,EAAE,CAAC,CAAC;QACjC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,MAAM,IAAI,GAAI,KAAwC,EAAE,IAAI,CAAC;QAC7D,IAAI,OAAO,IAAI,KAAK,QAAQ;YAAE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAC/C,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CAKF;AAbD,sCAaC;AAED,8DAA8D;AAC9D,kEAAkE;AAClE,MAAa,wBAAyB,SAAQ,aAAa;IACzD,YAAY,OAAO,GAAG,yBAAyB,EAAE,KAAe;QAC9D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CAEF;AAPD,4DAOC","sourcesContent":["export class SqlStackError extends Error {\n constructor(message?: string) {\n super(message);\n this.name = 'SqlStackError';\n }\n}\n\nexport class ExactlyOneRowError extends SqlStackError {\n constructor(actualCount: number) {\n super(`Expected exactly one row, got ${actualCount}`);\n this.name = 'ExactlyOneRowError';\n }\n}\n\nexport class NoRowsError extends SqlStackError {\n constructor() {\n super('Expected at least one row, got 0');\n this.name = 'NoRowsError';\n }\n}\n\nexport class RowsExistError extends SqlStackError {\n constructor(actualCount: number) {\n super(`Expected no rows, got ${actualCount}`);\n this.name = 'RowsExistError';\n }\n}\n\n// A result-shape decorator (@Single/@Only/@Exist/@None) was applied to a\n// method whose statement produced no result set (a plain write). Shapes\n// require rows; use `... RETURNING` or drop the decorator.\nexport class ExpectedRowsError extends SqlStackError {\n constructor(method: string, shape: string) {\n const decorator = '@' + shape.charAt(0).toUpperCase() + shape.slice(1);\n super(\n `${decorator} on ${method} requires a result set, but the statement produced a write result ` +\n `({ rowsAffected }). Add a RETURNING clause or remove the shape decorator.`\n );\n this.method = method;\n this.shape = shape;\n this.name = 'ExpectedRowsError';\n }\n method: string;\n shape: string;\n}\n\nexport class ValidationError extends SqlStackError {\n constructor(details: string) {\n super(`Validation failed: ${details}`);\n this.details = details;\n this.name = 'ValidationError';\n }\n details: string;\n}\n\n/**\n * A statement failed: the driver rejected it, or (for the `run(...)` forms\n * that take arguments) the SQL could not be compiled, bound, or routed to a\n * database. The original error is kept in `.cause`; when the cause carries a\n * string driver code (`SQLITE_CONSTRAINT_UNIQUE`, `23505`, `ER_DUP_ENTRY`)\n * it is copied to `.code` so existing `err.code` checks keep working.\n */\nexport class DatabaseError extends SqlStackError {\n constructor(message: string, sql: string, cause?: Error) {\n super(`${message}\\nSQL: ${sql}`);\n this.sql = sql;\n this.cause = cause;\n const code = (cause as { code?: unknown } | undefined)?.code;\n if (typeof code === 'string') this.code = code;\n this.name = 'DatabaseError';\n }\n sql: string;\n cause?: Error;\n /** Driver error code copied from `cause` when it is a string. */\n code?: string;\n}\n\n// Signals that the current transaction should be rolled back.\n// If `cause` is provided, wrappers may rethrow it after rollback.\nexport class RollbackTransactionError extends SqlStackError {\n constructor(message = 'Transaction rolled back', cause?: unknown) {\n super(message);\n this.cause = cause;\n this.name = 'RollbackTransactionError';\n }\n cause?: unknown;\n}\n"]}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/core/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAgB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QACnB,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CAEF;AAPD,4DAOC","sourcesContent":["export class SqlStackError extends Error {\n constructor(message?: string) {\n super(message);\n this.name = 'SqlStackError';\n }\n}\n\nexport class ExactlyOneRowError extends SqlStackError {\n constructor(actualCount: number) {\n super(`Expected exactly one row, got ${actualCount}`);\n this.name = 'ExactlyOneRowError';\n }\n}\n\nexport class NoRowsError extends SqlStackError {\n constructor() {\n super('Expected at least one row, got 0');\n this.name = 'NoRowsError';\n }\n}\n\nexport class RowsExistError extends SqlStackError {\n constructor(actualCount: number) {\n super(`Expected no rows, got ${actualCount}`);\n this.name = 'RowsExistError';\n }\n}\n\n// A result-shape decorator (@Single/@Only/@Exist/@None) was applied to a\n// method whose statement produced no result set (a plain write). Shapes\n// require rows; use `... RETURNING` or drop the decorator.\nexport class ExpectedRowsError extends SqlStackError {\n constructor(method: string, shape: string) {\n const decorator = '@' + shape.charAt(0).toUpperCase() + shape.slice(1);\n super(\n `${decorator} on ${method} requires a result set, but the statement produced a write result ` +\n `({ rowsAffected }). Add a RETURNING clause or remove the shape decorator.`\n );\n this.method = method;\n this.shape = shape;\n this.name = 'ExpectedRowsError';\n }\n method: string;\n shape: string;\n}\n\nexport class ValidationError extends SqlStackError {\n constructor(details: string) {\n super(`Validation failed: ${details}`);\n this.details = details;\n this.name = 'ValidationError';\n }\n details: string;\n}\n\n/**\n * A statement failed: the driver rejected it, or (for the `run(...)` forms\n * that take arguments) the SQL could not be compiled, bound, or routed to a\n * database. The original error is kept in `.cause`; when the cause carries a\n * string driver code (`SQLITE_CONSTRAINT_UNIQUE`, `23505`, `ER_DUP_ENTRY`)\n * it is copied to `.code` so existing `err.code` checks keep working.\n */\nexport class DatabaseError extends SqlStackError {\n constructor(message: string, sql: string, cause?: Error) {\n super(`${message}\\nSQL: ${sql}`);\n this.sql = sql;\n this.cause = cause;\n const code = (cause as { code?: unknown } | undefined)?.code;\n if (typeof code === 'string') this.code = code;\n this.name = 'DatabaseError';\n }\n sql: string;\n cause?: Error;\n /** Driver error code copied from `cause` when it is a string. */\n code?: string;\n}\n\n/**\n * The database ended the transaction on its own and rolled it back: a sqlego\n * lease ran out (no statement within the lease), the transaction hit the\n * server's hard cap on its duration, or the client connection dropped.\n * `code` is `'tx-expired'`. Statements fail with it (wrapped in\n * DatabaseError, which copies the code); COMMIT fails with it directly.\n */\nexport class TransactionExpiredError extends SqlStackError {\n constructor(message: string, cause?: unknown) {\n super(message);\n this.cause = cause;\n this.name = 'TransactionExpiredError';\n }\n readonly code = 'tx-expired';\n cause?: unknown;\n}\n\n/**\n * Another process held the database in a transaction for longer than the\n * server lets a request wait (sqlego's queue timeout). Nothing ran; the\n * statement or BEGIN can be retried. `code` is `'tx-busy'`.\n */\nexport class TransactionBusyError extends SqlStackError {\n constructor(message: string, cause?: unknown) {\n super(message);\n this.cause = cause;\n this.name = 'TransactionBusyError';\n }\n readonly code = 'tx-busy';\n cause?: unknown;\n}\n\n// Signals that the current transaction should be rolled back.\n// If `cause` is provided, wrappers may rethrow it after rollback.\nexport class RollbackTransactionError extends SqlStackError {\n constructor(message = 'Transaction rolled back', cause?: unknown) {\n super(message);\n this.cause = cause;\n this.name = 'RollbackTransactionError';\n }\n cause?: unknown;\n}\n"]}
@@ -11,6 +11,14 @@ export interface Database {
11
11
  query(sql: string, params: unknown[]): Promise<QueryResult>;
12
12
  conn: unknown;
13
13
  close: () => Promise<void> | void;
14
+ /**
15
+ * Optional, SQLite dialect only: a fresh handle for one transaction. When
16
+ * present, a transaction runs `BEGIN … COMMIT` on this handle instead of on
17
+ * the shared `Database`, so statements other flows send to the shared
18
+ * handle stay outside it (the role a pooled connection plays for
19
+ * Postgres/MySQL). Called once per transaction.
20
+ */
21
+ transactionHandle?: () => Database;
14
22
  }
15
23
  declare class Registry {
16
24
  private map;
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/registry.ts"],"names":[],"mappings":"AAAA,MA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U,CAAC,IAAY;QACrB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,IAAI,EAAE,CAAC,CAAC;QACjF,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,QAAQ,CAAC,IAAa;QACpB,MAAM,GAAG,GAAG,IAAI,IAAI,IAAI,CAAC,WAAW,CAAC;QACrC,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QACrD,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,GAAG,EAAE,CAAC,CAAC;QAChE,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;IAC3B,CAAC;IAED,GAAG,CAAC,IAAa;QACf,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;IAChC,CAAC;CACF;AAED,8DAA8D;AAC9D,MAAM,CAAC,GAAG,UAAiB,CAAC;AAC5B,IAAI,CAAC,CAAC,CAAC,sBAAsB,EAAE,CAAC;IAC9B,CAAC,CAAC,sBAAsB,GAAG,IAAI,QAAQ,EAAE,CAAC;AAC5C,CAAC;AAEY,QAAA,UAAU,GAAa,CAAC,CAAC,sBAAsB,CAAC","sourcesContent":["export type Dialect = 'sqlite' | 'mysql' | 'postgres';\n\n// Result for statements that produce no result set (INSERT/UPDATE/DELETE\n// without RETURNING, DDL). Statements that DO produce a result set — SELECT,\n// CTEs, and `... RETURNING` — return rows (`any[]`) instead; the affected\n// count is `rows.length`.\nexport interface WriteResult {\n rowsAffected: number;\n lastInsertId?: unknown;\n}\n\n/** @deprecated RETURNING now yields rows directly; this is identical to WriteResult. */\nexport type PostgresWriteResult = WriteResult;\n\nexport type QueryResult = any[] | WriteResult;\n\nexport interface Database {\n dialect: Dialect;\n query(sql: string, params: unknown[]): Promise<QueryResult>;\n // Underlying driver connection or pool (driver-specific)\n conn: unknown;\n close: () => Promise<void> | void;\n}\n\nclass Registry {\n private map = new Map<string, Database>();\n private defaultName?: string;\n\n register(name: string, db: Database) {\n this.map.set(name, db);\n return this;\n }\n\n setDefault(name: string) {\n if (!this.map.has(name)) throw new Error(`No database registered under ${name}`);\n this.defaultName = name;\n return this;\n }\n\n getEntry(name?: string): { name: string; db: Database } {\n const key = name ?? this.defaultName;\n if (!key) throw new Error('No default database set');\n const db = this.map.get(key);\n if (!db) throw new Error(`No database registered under ${key}`);\n return { name: key, db };\n }\n\n get(name?: string): Database {\n return this.getEntry(name).db;\n }\n}\n\n// Ensure a single shared instance across module copies/usages\nconst g = globalThis as any;\nif (!g.__SQLSTACK_SINGLETON__) {\n g.__SQLSTACK_SINGLETON__ = new Registry();\n}\n\nexport const SqlStackDB: Registry = g.__SQLSTACK_SINGLETON__;\n\nexport type IStackDB = typeof SqlStackDB;\n"]}
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/registry.ts"],"names":[],"mappings":";;;AAgCA,MAAM,QAAQ;IAAd;QACU,QAAG,GAAG,IAAI,GAAG,EAAoB,CAAC;IAyB5C,CAAC;IAtBC,QAAQ,CAAC,IAAY,EAAE,EAAY;QACjC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,UAAU,CAAC,IAAY;QACrB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,IAAI,EAAE,CAAC,CAAC;QACjF,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,QAAQ,CAAC,IAAa;QACpB,MAAM,GAAG,GAAG,IAAI,IAAI,IAAI,CAAC,WAAW,CAAC;QACrC,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QACrD,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,GAAG,EAAE,CAAC,CAAC;QAChE,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;IAC3B,CAAC;IAED,GAAG,CAAC,IAAa;QACf,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;IAChC,CAAC;CACF;AAED,8DAA8D;AAC9D,MAAM,CAAC,GAAG,UAAiB,CAAC;AAC5B,IAAI,CAAC,CAAC,CAAC,sBAAsB,EAAE,CAAC;IAC9B,CAAC,CAAC,sBAAsB,GAAG,IAAI,QAAQ,EAAE,CAAC;AAC5C,CAAC;AAEY,QAAA,UAAU,GAAa,CAAC,CAAC,sBAAsB,CAAC","sourcesContent":["export type Dialect = 'sqlite' | 'mysql' | 'postgres';\n\n// Result for statements that produce no result set (INSERT/UPDATE/DELETE\n// without RETURNING, DDL). Statements that DO produce a result set — SELECT,\n// CTEs, and `... RETURNING` — return rows (`any[]`) instead; the affected\n// count is `rows.length`.\nexport interface WriteResult {\n rowsAffected: number;\n lastInsertId?: unknown;\n}\n\n/** @deprecated RETURNING now yields rows directly; this is identical to WriteResult. */\nexport type PostgresWriteResult = WriteResult;\n\nexport type QueryResult = any[] | WriteResult;\n\nexport interface Database {\n dialect: Dialect;\n query(sql: string, params: unknown[]): Promise<QueryResult>;\n // Underlying driver connection or pool (driver-specific)\n conn: unknown;\n close: () => Promise<void> | void;\n /**\n * Optional, SQLite dialect only: a fresh handle for one transaction. When\n * present, a transaction runs `BEGIN … COMMIT` on this handle instead of on\n * the shared `Database`, so statements other flows send to the shared\n * handle stay outside it (the role a pooled connection plays for\n * Postgres/MySQL). Called once per transaction.\n */\n transactionHandle?: () => Database;\n}\n\nclass Registry {\n private map = new Map<string, Database>();\n private defaultName?: string;\n\n register(name: string, db: Database) {\n this.map.set(name, db);\n return this;\n }\n\n setDefault(name: string) {\n if (!this.map.has(name)) throw new Error(`No database registered under ${name}`);\n this.defaultName = name;\n return this;\n }\n\n getEntry(name?: string): { name: string; db: Database } {\n const key = name ?? this.defaultName;\n if (!key) throw new Error('No default database set');\n const db = this.map.get(key);\n if (!db) throw new Error(`No database registered under ${key}`);\n return { name: key, db };\n }\n\n get(name?: string): Database {\n return this.getEntry(name).db;\n }\n}\n\n// Ensure a single shared instance across module copies/usages\nconst g = globalThis as any;\nif (!g.__SQLSTACK_SINGLETON__) {\n g.__SQLSTACK_SINGLETON__ = new Registry();\n}\n\nexport const SqlStackDB: Registry = g.__SQLSTACK_SINGLETON__;\n\nexport type IStackDB = typeof SqlStackDB;\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"transactions.d.ts","sourceRoot":"","sources":["../../src/transactions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG7C,MAAM,MAAM,kBAAkB,GAAG;IAC/B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,+EAA+E;AAC/E,MAAM,MAAM,eAAe,GAAG,MAAM,OAAO,CAAC;AAE5C,8DAA8D;AAC9D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,IAAI,EAAE,qCAAqC,CAAC;IACrD,kEAAkE;IAClE,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CACvC,CAAC;AAEF,MAAM,M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