sqlstack 3.7.1 → 3.7.2-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.
Files changed (49) hide show
  1. package/dist/cjs/adapters/execSqlite.d.ts +34 -0
  2. package/dist/cjs/adapters/execSqlite.d.ts.map +1 -0
  3. package/dist/cjs/adapters/execSqlite.js +36 -0
  4. package/dist/cjs/adapters/execSqlite.js.map +1 -0
  5. package/dist/cjs/adapters/index.d.ts +2 -0
  6. package/dist/cjs/adapters/index.d.ts.map +1 -1
  7. package/dist/cjs/adapters/index.js +3 -1
  8. package/dist/cjs/adapters/index.js.map +1 -1
  9. package/dist/cjs/core/errors.d.ts +13 -0
  10. package/dist/cjs/core/errors.d.ts.map +1 -1
  11. package/dist/cjs/core/errors.js +18 -1
  12. package/dist/cjs/core/errors.js.map +1 -1
  13. package/dist/cjs/family.d.ts +6 -0
  14. package/dist/cjs/family.d.ts.map +1 -1
  15. package/dist/cjs/family.js +126 -33
  16. package/dist/cjs/family.js.map +1 -1
  17. package/dist/cjs/familyBinding.d.ts +4 -0
  18. package/dist/cjs/familyBinding.d.ts.map +1 -1
  19. package/dist/cjs/familyBinding.js.map +1 -1
  20. package/dist/cjs/index.d.ts +1 -1
  21. package/dist/cjs/index.d.ts.map +1 -1
  22. package/dist/cjs/index.js.map +1 -1
  23. package/dist/cjs/registry.d.ts +26 -0
  24. package/dist/cjs/registry.d.ts.map +1 -1
  25. package/dist/cjs/registry.js.map +1 -1
  26. package/dist/cjs/stack.d.ts.map +1 -1
  27. package/dist/cjs/stack.js +29 -4
  28. package/dist/cjs/stack.js.map +1 -1
  29. package/dist/cjs/transactions.d.ts +66 -0
  30. package/dist/cjs/transactions.d.ts.map +1 -1
  31. package/dist/cjs/transactions.js +237 -40
  32. package/dist/cjs/transactions.js.map +1 -1
  33. package/dist/esm/adapters/execSqlite.js +33 -0
  34. package/dist/esm/adapters/execSqlite.js.map +1 -0
  35. package/dist/esm/adapters/index.js +1 -0
  36. package/dist/esm/adapters/index.js.map +1 -1
  37. package/dist/esm/core/errors.js +16 -0
  38. package/dist/esm/core/errors.js.map +1 -1
  39. package/dist/esm/family.js +127 -34
  40. package/dist/esm/family.js.map +1 -1
  41. package/dist/esm/familyBinding.js.map +1 -1
  42. package/dist/esm/index.js.map +1 -1
  43. package/dist/esm/registry.js.map +1 -1
  44. package/dist/esm/stack.js +29 -4
  45. package/dist/esm/stack.js.map +1 -1
  46. package/dist/esm/transactions.js +237 -40
  47. package/dist/esm/transactions.js.map +1 -1
  48. package/package.json +1 -1
  49. package/readme.md +33 -0
@@ -0,0 +1,34 @@
1
+ import type { Database, TransactionHooks } from '../registry';
2
+ /**
3
+ * Cursor returned by an exec-style call. Structural: matches Cloudflare
4
+ * Durable Object `SqlStorageCursor` without depending on its types.
5
+ */
6
+ export interface ExecSqliteCursorLike {
7
+ /** All result rows as objects (DO cursor: `toArray()`). */
8
+ toArray(): Record<string, unknown>[];
9
+ /** Column names; a non-empty list means the statement produced a result set. */
10
+ readonly columnNames?: readonly string[];
11
+ /** Rows written by the statement (DO cursor: `rowsWritten`). */
12
+ readonly rowsWritten?: number;
13
+ }
14
+ /** A handle with one `exec(sql, ...params)` call (DO: `ctx.storage.sql`). */
15
+ export interface ExecSqliteHandleLike {
16
+ exec(sql: string, ...params: unknown[]): ExecSqliteCursorLike | Promise<ExecSqliteCursorLike>;
17
+ }
18
+ export interface ExecSqliteDbOptions {
19
+ /**
20
+ * Host transaction API. Hosts that reject `BEGIN`/`COMMIT`/`SAVEPOINT`
21
+ * statements (Durable Objects) must supply these; see docs/transactions.md.
22
+ */
23
+ transactionHooks?: TransactionHooks;
24
+ /** Called by `Database.close()`. The handle itself is never closed. */
25
+ close?: () => void | Promise<void>;
26
+ }
27
+ /**
28
+ * Wrap an exec-style SQLite handle. Row-producing statements return rows;
29
+ * others return `{ rowsAffected }` from the cursor's `rowsWritten`.
30
+ * Rows vs write is decided from `columnNames` when the cursor exposes it,
31
+ * falling back to the SQL text (leading SELECT/WITH or a RETURNING clause).
32
+ */
33
+ export declare function createExecSqliteDb(handle: ExecSqliteHandleLike, options?: ExecSqliteDbOptions): Database;
34
+ //# sourceMappingURL=execSqlite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execSqlite.d.ts","sourceRoot":"","sources":["../../../src/adapters/execSqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG9D;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,2DAA2D;IAC3D,OAAO,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IACrC,gFAAgF;IAChF,QAAQ,CAAC,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC,gEAAgE;IAChE,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,6EAA6E;AAC7E,MAAM,WAAW,oBAAoB;IACnC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;CAC/F;AAED,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,oBAAoB,EAAE,OAAO,GAAE,mBAAwB,GAAG,QAAQ,CAqB5G"}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createExecSqliteDb = createExecSqliteDb;
4
+ const sqlite_1 = require("./sqlite");
5
+ /**
6
+ * Wrap an exec-style SQLite handle. Row-producing statements return rows;
7
+ * others return `{ rowsAffected }` from the cursor's `rowsWritten`.
8
+ * Rows vs write is decided from `columnNames` when the cursor exposes it,
9
+ * falling back to the SQL text (leading SELECT/WITH or a RETURNING clause).
10
+ */
11
+ function createExecSqliteDb(handle, options = {}) {
12
+ if (!handle || typeof handle.exec !== 'function') {
13
+ throw new TypeError('createExecSqliteDb requires a handle with exec(sql, ...params)');
14
+ }
15
+ const db = {
16
+ dialect: 'sqlite',
17
+ async query(sql, params) {
18
+ const cursor = await handle.exec(sql, ...(params ?? []));
19
+ const rows = cursor.toArray();
20
+ const columns = cursor.columnNames;
21
+ const hasResultSet = Array.isArray(columns) ? columns.length > 0 : (0, sqlite_1.producesRows)(sql);
22
+ if (hasResultSet)
23
+ return rows;
24
+ return { rowsAffected: Number(cursor.rowsWritten ?? 0) };
25
+ },
26
+ conn: handle,
27
+ async close() {
28
+ if (options.close)
29
+ await options.close();
30
+ },
31
+ };
32
+ if (options.transactionHooks)
33
+ db.transactionHooks = options.transactionHooks;
34
+ return db;
35
+ }
36
+ //# sourceMappingURL=execSqlite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execSqlite.js","sourceRoot":"","sources":["../../../src/adapters/execSqlite.ts"],"names":[],"mappings":";;AAqCA,gDAqBC;AAzDD,qCAAwC;AA8BxC;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,MAA4B,EAAE,UAA+B,EAAE;IAChG,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;QACjD,MAAM,IAAI,SAAS,CAAC,gEAAgE,CAAC,CAAC;IACxF,CAAC;IACD,MAAM,EAAE,GAAa;QACnB,OAAO,EAAE,QAAQ;QACjB,KAAK,CAAC,KAAK,CAAC,GAAW,EAAE,MAAiB;YACxC,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC;YACzD,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YAC9B,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC;YACnC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAA,qBAAY,EAAC,GAAG,CAAC,CAAC;YACrF,IAAI,YAAY;gBAAE,OAAO,IAAI,CAAC;YAC9B,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC3D,CAAC;QACD,IAAI,EAAE,MAAM;QACZ,KAAK,CAAC,KAAK;YACT,IAAI,OAAO,CAAC,KAAK;gBAAE,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;QAC3C,CAAC;KACF,CAAC;IACF,IAAI,OAAO,CAAC,gBAAgB;QAAE,EAAE,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAC7E,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["import type { Database, TransactionHooks } from '../registry';\nimport { producesRows } from './sqlite';\n\n/**\n * Cursor returned by an exec-style call. Structural: matches Cloudflare\n * Durable Object `SqlStorageCursor` without depending on its types.\n */\nexport interface ExecSqliteCursorLike {\n /** All result rows as objects (DO cursor: `toArray()`). */\n toArray(): Record<string, unknown>[];\n /** Column names; a non-empty list means the statement produced a result set. */\n readonly columnNames?: readonly string[];\n /** Rows written by the statement (DO cursor: `rowsWritten`). */\n readonly rowsWritten?: number;\n}\n\n/** A handle with one `exec(sql, ...params)` call (DO: `ctx.storage.sql`). */\nexport interface ExecSqliteHandleLike {\n exec(sql: string, ...params: unknown[]): ExecSqliteCursorLike | Promise<ExecSqliteCursorLike>;\n}\n\nexport interface ExecSqliteDbOptions {\n /**\n * Host transaction API. Hosts that reject `BEGIN`/`COMMIT`/`SAVEPOINT`\n * statements (Durable Objects) must supply these; see docs/transactions.md.\n */\n transactionHooks?: TransactionHooks;\n /** Called by `Database.close()`. The handle itself is never closed. */\n close?: () => void | Promise<void>;\n}\n\n/**\n * Wrap an exec-style SQLite handle. Row-producing statements return rows;\n * others return `{ rowsAffected }` from the cursor's `rowsWritten`.\n * Rows vs write is decided from `columnNames` when the cursor exposes it,\n * falling back to the SQL text (leading SELECT/WITH or a RETURNING clause).\n */\nexport function createExecSqliteDb(handle: ExecSqliteHandleLike, options: ExecSqliteDbOptions = {}): Database {\n if (!handle || typeof handle.exec !== 'function') {\n throw new TypeError('createExecSqliteDb requires a handle with exec(sql, ...params)');\n }\n const db: Database = {\n dialect: 'sqlite',\n async query(sql: string, params: unknown[]) {\n const cursor = await handle.exec(sql, ...(params ?? []));\n const rows = cursor.toArray();\n const columns = cursor.columnNames;\n const hasResultSet = Array.isArray(columns) ? columns.length > 0 : producesRows(sql);\n if (hasResultSet) return rows;\n return { rowsAffected: Number(cursor.rowsWritten ?? 0) };\n },\n conn: handle,\n async close() {\n if (options.close) await options.close();\n },\n };\n if (options.transactionHooks) db.transactionHooks = options.transactionHooks;\n return db;\n}\n"]}
@@ -2,5 +2,7 @@ export { createSqliteDb } from './sqlite';
2
2
  export { createPgDb } from './pg';
3
3
  export { createMysqlDb } from './mysql';
4
4
  export { createSqlegoDb } from './sqlego';
5
+ export { createExecSqliteDb } from './execSqlite';
6
+ export type { ExecSqliteHandleLike, ExecSqliteCursorLike, ExecSqliteDbOptions } from './execSqlite';
5
7
  export type { SqlegoClientLike, SqlegoTransactionLike, SqlegoTargetLike, SqlegoDbOptions, SqlegoParams, SqlegoValue, } from './sqlego';
6
8
  //# 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,EACV,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,WAAW,GACZ,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,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACpG,YAAY,EACV,gBAAgB,EAChB,qBAAqB,EACrB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,WAAW,GACZ,MAAM,UAAU,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSqlegoDb = exports.createMysqlDb = exports.createPgDb = exports.createSqliteDb = void 0;
3
+ exports.createExecSqliteDb = exports.createSqlegoDb = exports.createMysqlDb = exports.createPgDb = exports.createSqliteDb = void 0;
4
4
  var sqlite_1 = require("./sqlite");
5
5
  Object.defineProperty(exports, "createSqliteDb", { enumerable: true, get: function () { return sqlite_1.createSqliteDb; } });
6
6
  var pg_1 = require("./pg");
@@ -9,4 +9,6 @@ var mysql_1 = require("./mysql");
9
9
  Object.defineProperty(exports, "createMysqlDb", { enumerable: true, get: function () { return mysql_1.createMysqlDb; } });
10
10
  var sqlego_1 = require("./sqlego");
11
11
  Object.defineProperty(exports, "createSqlegoDb", { enumerable: true, get: function () { return sqlego_1.createSqlegoDb; } });
12
+ var execSqlite_1 = require("./execSqlite");
13
+ Object.defineProperty(exports, "createExecSqliteDb", { enumerable: true, get: function () { return execSqlite_1.createExecSqliteDb; } });
12
14
  //# sourceMappingURL=index.js.map
@@ -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 {\n SqlegoClientLike,\n SqlegoTransactionLike,\n SqlegoTargetLike,\n SqlegoDbOptions,\n SqlegoParams,\n SqlegoValue,\n} 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;AACvB,2CAAkD;AAAzC,gHAAA,kBAAkB,OAAA","sourcesContent":["export { createSqliteDb } from './sqlite';\nexport { createPgDb } from './pg';\nexport { createMysqlDb } from './mysql';\nexport { createSqlegoDb } from './sqlego';\nexport { createExecSqliteDb } from './execSqlite';\nexport type { ExecSqliteHandleLike, ExecSqliteCursorLike, ExecSqliteDbOptions } from './execSqlite';\nexport type {\n SqlegoClientLike,\n SqlegoTransactionLike,\n SqlegoTargetLike,\n SqlegoDbOptions,\n SqlegoParams,\n SqlegoValue,\n} from './sqlego';\n"]}
@@ -55,6 +55,19 @@ export declare class TransactionBusyError extends SqlStackError {
55
55
  readonly code = "tx-busy";
56
56
  cause?: unknown;
57
57
  }
58
+ /**
59
+ * A database family's provider.open() did not settle within the family's
60
+ * `openTimeoutMs`. The attempt was abandoned: waiting acquires are rejected
61
+ * and their leases rolled back, and a handle that arrives later is closed.
62
+ * `code` is `'family-open-timeout'`.
63
+ */
64
+ export declare class FamilyOpenTimeoutError extends SqlStackError {
65
+ constructor(family: string, keyId: string, timeoutMs: number);
66
+ readonly code = "family-open-timeout";
67
+ family: string;
68
+ keyId: string;
69
+ timeoutMs: number;
70
+ }
58
71
  export declare class RollbackTransactionError extends SqlStackError {
59
72
  constructor(message?: string, cause?: unknown);
60
73
  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;AAED;;;;;;GAMG;A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ACd,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;GAMG;AACH,qBAAa,uBAAwB,SAAQ,aAAa;gBAC5C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;IAK5C,QAAQ,CAAC,IAAI,gBAAgB;IAC7B,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;GAIG;AACH,qBAAa,oBAAqB,SAAQ,aAAa;gBACzC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;IAK5C,QAAQ,CAAC,IAAI,aAAa;IAC1B,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;GAKG;AACH,qBAAa,sBAAuB,SAAQ,aAAa;gBAC3C,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;IAO5D,QAAQ,CAAC,IAAI,yBAAyB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB;AAID,qBAAa,wBAAyB,SAAQ,aAAa;gBAC7C,OAAO,SAA4B,EAAE,KAAK,CAAC,EAAE,OAAO;IAKhE,KAAK,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.TransactionBusyError = exports.TransactionExpiredError = exports.DatabaseError = exports.ValidationError = exports.ExpectedRowsError = exports.RowsExistError = exports.NoRowsError = exports.ExactlyOneRowError = exports.SqlStackError = void 0;
3
+ exports.RollbackTransactionError = exports.FamilyOpenTimeoutError = 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);
@@ -100,6 +100,23 @@ class TransactionBusyError extends SqlStackError {
100
100
  }
101
101
  }
102
102
  exports.TransactionBusyError = TransactionBusyError;
103
+ /**
104
+ * A database family's provider.open() did not settle within the family's
105
+ * `openTimeoutMs`. The attempt was abandoned: waiting acquires are rejected
106
+ * and their leases rolled back, and a handle that arrives later is closed.
107
+ * `code` is `'family-open-timeout'`.
108
+ */
109
+ class FamilyOpenTimeoutError extends SqlStackError {
110
+ constructor(family, keyId, timeoutMs) {
111
+ super(`Opening "${family}/${keyId}" timed out after ${timeoutMs}ms; the attempt was abandoned.`);
112
+ this.code = 'family-open-timeout';
113
+ this.family = family;
114
+ this.keyId = keyId;
115
+ this.timeoutMs = timeoutMs;
116
+ this.name = 'FamilyOpenTimeoutError';
117
+ }
118
+ }
119
+ exports.FamilyOpenTimeoutError = FamilyOpenTimeoutError;
103
120
  // Signals that the current transaction should be rolled back.
104
121
  // If `cause` is provided, wrappers may rethrow it after rollback.
105
122
  class RollbackTransactionError extends SqlStackError {
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/core/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AALD,sCAKC;AAED,MAAa,kBAAmB,SAAQ,aAAa;IACnD,YAAY,WAAmB;QAC7B,KAAK,CAAC,iCAAiC,WAAW,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AALD,gDAKC;AAED,MAAa,WAAY,SAAQ,aAAa;IAC5C;QACE,KAAK,CAAC,kCAAkC,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AALD,kCAKC;AAED,MAAa,cAAe,SAAQ,aAAa;IAC/C,YA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,eAAe,CAAC;IAC9B,CAAC;CAKF;AAbD,sCAaC;AAED;;;;;;GAMG;AACH,MAAa,uBAAwB,SAAQ,aAAa;IACxD,YAAY,OAAe,EAAE,KAAe;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;QAIR,SAAI,GAAG,YAAY,CAAC;QAH3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACxC,CAAC;CAGF;AARD,0DAQC;AAED;;;;GAIG;AACH,MAAa,oBAAqB,SAAQ,aAAa;IACrD,YAAY,OAAe,EAAE,KAAe;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;QAIR,SAAI,GAAG,SAAS,CAAC;QAHxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CAGF;AARD,oDAQC;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/**\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"]}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/core/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AALD,sCAKC;AAED,MAAa,kBAAmB,SAAQ,aAAa;IACnD,YAAY,WAAmB;QAC7B,KAAK,CAAC,iCAAiC,WAAW,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AALD,gDAKC;AAED,MAAa,WAAY,SAAQ,aAAa;IAC5C;QACE,KAAK,CAAC,kCAAkC,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AALD,kCAKC;AAED,MAAa,cAAe,SAAQ,aAAa;IAC/C,YAAY,WAAmB;QAC7B,KAAK,CAAC,yBAAyB,WAAW,EAAE,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;CACF;AALD,wCAKC;AAED,yEAAyE;AACzE,wEAAwE;AACxE,2DAA2D;AAC3D,MAAa,iBAAkB,SAAQ,aAAa;IAClD,YAAY,MAAc,EAAE,KAAa;QACvC,MAAM,SAAS,GAAG,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC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;;GAIG;AACH,MAAa,oBAAqB,SAAQ,aAAa;IACrD,YAAY,OAAe,EAAE,KAAe;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;QAIR,SAAI,GAAG,SAAS,CAAC;QAHxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CAGF;AARD,oDAQC;AAED;;;;;GAKG;AACH,MAAa,sBAAuB,SAAQ,aAAa;IACvD,YAAY,MAAc,EAAE,KAAa,EAAE,SAAiB;QAC1D,KAAK,CAAC,YAAY,MAAM,IAAI,KAAK,qBAAqB,SAAS,gCAAgC,CAAC,CAAC;QAM1F,SAAI,GAAG,qBAAqB,CAAC;QALpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CAKF;AAZD,wDAYC;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/**\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/**\n * A database family's provider.open() did not settle within the family's\n * `openTimeoutMs`. The attempt was abandoned: waiting acquires are rejected\n * and their leases rolled back, and a handle that arrives later is closed.\n * `code` is `'family-open-timeout'`.\n */\nexport class FamilyOpenTimeoutError extends SqlStackError {\n constructor(family: string, keyId: string, timeoutMs: number) {\n super(`Opening \"${family}/${keyId}\" timed out after ${timeoutMs}ms; the attempt was abandoned.`);\n this.family = family;\n this.keyId = keyId;\n this.timeoutMs = timeoutMs;\n this.name = 'FamilyOpenTimeoutError';\n }\n readonly code = 'family-open-timeout';\n family: string;\n keyId: string;\n timeoutMs: number;\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"]}
@@ -32,6 +32,12 @@ export interface FamilyConfig<K = string> {
32
32
  /** Max concurrently open members (default 16). Leased members never evict. */
33
33
  maxOpen?: number;
34
34
  };
35
+ /**
36
+ * Max ms to wait for provider.open(). On expiry waiting acquires reject with
37
+ * FamilyOpenTimeoutError and a handle that arrives later is closed.
38
+ * Default: no timeout.
39
+ */
40
+ openTimeoutMs?: number;
35
41
  }
36
42
  /** Concrete member identity: owning stack + family registration + key. */
37
43
  export interface FamilyMemberEntry extends DatabaseEntry {
@@ -1 +1 @@
1
- {"version":3,"file":"family.d.ts","sourceRoot":"","sources":["../../src/family.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY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EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IASvF,8EAA8E;IACxE,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAYhD,sFAAsF;IAChF,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAc3C,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAQ1D,0EAA0E;IACpE,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAmKhC"}
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _FamilyRuntime_instances, _FamilyRuntime_stack, _FamilyRuntime_provider, _FamilyRuntime_maxOpen, _FamilyRuntime_members, _FamilyRuntime_useTick, _FamilyRuntime_closed, _FamilyRuntime_member, _FamilyRuntime_open, _FamilyRuntime_closeHandle, _FamilyRuntime_evictIfNeeded, _FamilyRuntime_assertOpen;
13
+ var _FamilyRuntime_instances, _FamilyRuntime_stack, _FamilyRuntime_provider, _FamilyRuntime_maxOpen, _FamilyRuntime_openTimeoutMs, _FamilyRuntime_members, _FamilyRuntime_entries, _FamilyRuntime_reaper, _FamilyRuntime_useTick, _FamilyRuntime_closed, _FamilyRuntime_entry, _FamilyRuntime_record, _FamilyRuntime_pruneIfIdle, _FamilyRuntime_open, _FamilyRuntime_startOpen, _FamilyRuntime_closeHandle, _FamilyRuntime_evictIfNeeded, _FamilyRuntime_assertOpen;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.FamilyRuntime = void 0;
16
16
  const errors_1 = require("./core/errors");
@@ -23,13 +23,29 @@ class FamilyRuntime {
23
23
  _FamilyRuntime_stack.set(this, void 0);
24
24
  _FamilyRuntime_provider.set(this, void 0);
25
25
  _FamilyRuntime_maxOpen.set(this, void 0);
26
+ _FamilyRuntime_openTimeoutMs.set(this, void 0);
27
+ /** Live records only: a record exists while it has a lease, an open handle, or an open in flight. */
26
28
  _FamilyRuntime_members.set(this, new Map());
29
+ // Entry identity outlives a pruned record for as long as anyone holds the
30
+ // entry (transactions key by entry object); a live record holds it strongly.
31
+ _FamilyRuntime_entries.set(this, new Map());
32
+ _FamilyRuntime_reaper.set(this, new FinalizationRegistry((keyId) => {
33
+ if (__classPrivateFieldGet(this, _FamilyRuntime_entries, "f").get(keyId)?.deref() === undefined)
34
+ __classPrivateFieldGet(this, _FamilyRuntime_entries, "f").delete(keyId);
35
+ }));
27
36
  _FamilyRuntime_useTick.set(this, 0);
28
37
  _FamilyRuntime_closed.set(this, false);
29
38
  __classPrivateFieldSet(this, _FamilyRuntime_stack, stack, "f");
30
39
  this.name = name;
31
40
  __classPrivateFieldSet(this, _FamilyRuntime_provider, config.provider, "f");
32
41
  __classPrivateFieldSet(this, _FamilyRuntime_maxOpen, config.cache?.maxOpen ?? 16, "f");
42
+ const timeout = config.openTimeoutMs;
43
+ // setTimeout fires after 1ms for delays above 2^31-1.
44
+ if (timeout !== undefined && !(Number.isFinite(timeout) && timeout > 0 && timeout <= 2147483647)) {
45
+ throw new errors_1.SqlStackError(`Database family "${name}": openTimeoutMs must be a positive number of milliseconds ` +
46
+ `(max 2147483647), got ${String(timeout)}.`);
47
+ }
48
+ __classPrivateFieldSet(this, _FamilyRuntime_openTimeoutMs, timeout, "f");
33
49
  }
34
50
  normalize(input) {
35
51
  const key = __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").normalizeKey(input);
@@ -38,7 +54,7 @@ class FamilyRuntime {
38
54
  }
39
55
  /** Stable concrete identity for a key; no I/O. */
40
56
  ref(input) {
41
- return __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_member).call(this, input).entry;
57
+ return __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_entry).call(this, this.normalize(input).keyId);
42
58
  }
43
59
  async exists(input) {
44
60
  const { key } = this.normalize(input);
@@ -48,36 +64,42 @@ class FamilyRuntime {
48
64
  }
49
65
  async create(input) {
50
66
  __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_assertOpen).call(this);
51
- const member = __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_member).call(this, input);
67
+ const { key } = this.normalize(input);
52
68
  if (!__classPrivateFieldGet(this, _FamilyRuntime_provider, "f").create)
53
69
  unsupported(this.name, 'create');
54
- const db = await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").create(member.key);
70
+ const db = await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").create(key);
55
71
  await db.close();
56
72
  }
57
73
  async ensure(input) {
58
74
  __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_assertOpen).call(this);
59
- const member = __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_member).call(this, input);
75
+ const { key } = this.normalize(input);
60
76
  if (!__classPrivateFieldGet(this, _FamilyRuntime_provider, "f").ensure)
61
77
  unsupported(this.name, 'ensure');
62
- const db = await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").ensure(member.key);
78
+ const db = await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").ensure(key);
63
79
  await db.close();
64
80
  }
65
81
  /** Acquire a lease on an opened (existing) member. */
66
82
  async acquire(input) {
67
83
  var _a;
68
84
  __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_assertOpen).call(this);
69
- const member = __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_member).call(this, input);
85
+ const { key, keyId } = this.normalize(input);
86
+ const member = __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_record).call(this, keyId);
70
87
  // Count the lease before awaiting the open. Otherwise, while this acquire
71
88
  // is suspended, a concurrent release on the same member (or any other)
72
89
  // runs eviction, sees zero leases, and closes the handle this acquire is
73
90
  // about to return — with maxOpen: 0 that hands the caller a closed member.
74
91
  member.leases++;
75
92
  try {
76
- await __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_open).call(this, member);
93
+ await __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_open).call(this, member, key);
94
+ // closeAll() may have run while this acquire was suspended (even on an
95
+ // already-cached member). It owns every handle from then on; never hand
96
+ // out a lease on a closed family.
97
+ __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_assertOpen).call(this);
77
98
  }
78
99
  catch (err) {
79
100
  member.leases = Math.max(0, member.leases - 1);
80
101
  __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_evictIfNeeded).call(this);
102
+ __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_pruneIfIdle).call(this, member);
81
103
  throw err;
82
104
  }
83
105
  member.lastUsed = __classPrivateFieldSet(this, _FamilyRuntime_useTick, (_a = __classPrivateFieldGet(this, _FamilyRuntime_useTick, "f"), ++_a), "f");
@@ -91,6 +113,7 @@ class FamilyRuntime {
91
113
  released = true;
92
114
  member.leases = Math.max(0, member.leases - 1);
93
115
  __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_evictIfNeeded).call(this);
116
+ __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_pruneIfIdle).call(this, member);
94
117
  },
95
118
  };
96
119
  }
@@ -117,27 +140,33 @@ class FamilyRuntime {
117
140
  /** Physically delete a member. Fails while leased; closes the cached handle first. */
118
141
  async deleteMember(input) {
119
142
  __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_assertOpen).call(this);
120
- const member = __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_member).call(this, input);
143
+ const { key, keyId } = this.normalize(input);
121
144
  if (!__classPrivateFieldGet(this, _FamilyRuntime_provider, "f").delete)
122
145
  unsupported(this.name, 'delete');
123
- if (member.leases > 0) {
124
- throw new errors_1.SqlStackError(`Cannot delete "${this.name}/${member.entry.keyId}": ${member.leases} lease(s) are active.`);
146
+ const member = __classPrivateFieldGet(this, _FamilyRuntime_members, "f").get(keyId);
147
+ if (member && member.leases > 0) {
148
+ throw new errors_1.SqlStackError(`Cannot delete "${this.name}/${keyId}": ${member.leases} lease(s) are active.`);
125
149
  }
126
- if (member.db)
150
+ if (member?.db)
127
151
  await __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_closeHandle).call(this, member);
128
- await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").delete(member.key);
129
- __classPrivateFieldGet(this, _FamilyRuntime_members, "f").delete(member.entry.keyId);
152
+ await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").delete(key);
130
153
  }
131
154
  async inspect(input) {
132
- const member = __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_member).call(this, input);
155
+ const { key, keyId } = this.normalize(input);
133
156
  if (!__classPrivateFieldGet(this, _FamilyRuntime_provider, "f").inspect)
134
157
  unsupported(this.name, 'inspect');
135
- const facts = await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").inspect(member.key);
136
- return { ...facts, cached: !!member.db, activeLeases: member.leases };
158
+ const facts = await __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").inspect(key);
159
+ const member = __classPrivateFieldGet(this, _FamilyRuntime_members, "f").get(keyId);
160
+ return { ...facts, cached: !!member?.db, activeLeases: member?.leases ?? 0 };
137
161
  }
138
162
  /** Close every cached handle (root disposal). Databases are preserved. */
139
163
  async closeAll() {
140
164
  __classPrivateFieldSet(this, _FamilyRuntime_closed, true, "f");
165
+ // Abandon in-flight opens before any await: waiters reject, and a handle
166
+ // that lands later is closed by its attempt instead of leaking.
167
+ for (const member of __classPrivateFieldGet(this, _FamilyRuntime_members, "f").values()) {
168
+ member.opening?.abandon(new errors_1.SqlStackError(`Database family "${this.name}" was closed while "${this.name}/${member.entry.keyId}" was opening.`));
169
+ }
141
170
  const failures = [];
142
171
  for (const member of __classPrivateFieldGet(this, _FamilyRuntime_members, "f").values()) {
143
172
  if (!member.db)
@@ -150,18 +179,18 @@ class FamilyRuntime {
150
179
  }
151
180
  }
152
181
  __classPrivateFieldGet(this, _FamilyRuntime_members, "f").clear();
182
+ __classPrivateFieldGet(this, _FamilyRuntime_entries, "f").clear();
153
183
  if (failures.length) {
154
184
  throw new errors_1.SqlStackError(`Family close failed for ${failures.join(', ')}`);
155
185
  }
156
186
  }
157
187
  }
158
188
  exports.FamilyRuntime = FamilyRuntime;
159
- _FamilyRuntime_stack = new WeakMap(), _FamilyRuntime_provider = new WeakMap(), _FamilyRuntime_maxOpen = new WeakMap(), _FamilyRuntime_members = new WeakMap(), _FamilyRuntime_useTick = new WeakMap(), _FamilyRuntime_closed = new WeakMap(), _FamilyRuntime_instances = new WeakSet(), _FamilyRuntime_member = function _FamilyRuntime_member(input) {
160
- const { key, keyId } = this.normalize(input);
161
- let member = __classPrivateFieldGet(this, _FamilyRuntime_members, "f").get(keyId);
162
- if (!member) {
189
+ _FamilyRuntime_stack = new WeakMap(), _FamilyRuntime_provider = new WeakMap(), _FamilyRuntime_maxOpen = new WeakMap(), _FamilyRuntime_openTimeoutMs = new WeakMap(), _FamilyRuntime_members = new WeakMap(), _FamilyRuntime_entries = new WeakMap(), _FamilyRuntime_reaper = new WeakMap(), _FamilyRuntime_useTick = new WeakMap(), _FamilyRuntime_closed = new WeakMap(), _FamilyRuntime_instances = new WeakSet(), _FamilyRuntime_entry = function _FamilyRuntime_entry(keyId) {
190
+ let entry = __classPrivateFieldGet(this, _FamilyRuntime_entries, "f").get(keyId)?.deref();
191
+ if (!entry) {
163
192
  const self = this;
164
- const entry = {
193
+ entry = {
165
194
  name: this.name,
166
195
  family: this.name,
167
196
  keyId,
@@ -176,27 +205,91 @@ _FamilyRuntime_stack = new WeakMap(), _FamilyRuntime_provider = new WeakMap(), _
176
205
  return state.db;
177
206
  },
178
207
  };
179
- member = { entry, leases: 0, lastUsed: 0 };
208
+ __classPrivateFieldGet(this, _FamilyRuntime_entries, "f").set(keyId, new WeakRef(entry));
209
+ __classPrivateFieldGet(this, _FamilyRuntime_reaper, "f").register(entry, keyId);
210
+ }
211
+ return entry;
212
+ }, _FamilyRuntime_record = function _FamilyRuntime_record(keyId) {
213
+ let member = __classPrivateFieldGet(this, _FamilyRuntime_members, "f").get(keyId);
214
+ if (!member) {
215
+ member = { entry: __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_entry).call(this, keyId), leases: 0, lastUsed: 0 };
180
216
  __classPrivateFieldGet(this, _FamilyRuntime_members, "f").set(keyId, member);
181
217
  }
182
- return Object.assign(member, { key: this.normalize(input).key });
183
- }, _FamilyRuntime_open = async function _FamilyRuntime_open(member) {
218
+ return member;
219
+ }, _FamilyRuntime_pruneIfIdle = function _FamilyRuntime_pruneIfIdle(member) {
220
+ if (member.leases > 0 || member.db || member.opening)
221
+ return;
222
+ if (__classPrivateFieldGet(this, _FamilyRuntime_members, "f").get(member.entry.keyId) === member)
223
+ __classPrivateFieldGet(this, _FamilyRuntime_members, "f").delete(member.entry.keyId);
224
+ }, _FamilyRuntime_open = async function _FamilyRuntime_open(member, key) {
184
225
  if (member.db)
185
226
  return;
186
- if (!member.opening) {
187
- member.opening = __classPrivateFieldGet(this, _FamilyRuntime_provider, "f").open(member.key).finally(() => {
188
- member.opening = undefined;
189
- });
190
- member.opening.catch(() => undefined); // surfaced to awaiting callers
191
- }
192
- const db = await member.opening;
193
- member.db = db;
227
+ member.opening ?? (member.opening = __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_startOpen).call(this, member, key));
228
+ await member.opening.promise;
229
+ }, _FamilyRuntime_startOpen = function _FamilyRuntime_startOpen(member, key) {
230
+ let settled = false;
231
+ let timer;
232
+ let resolve;
233
+ let reject;
234
+ const promise = new Promise((res, rej) => {
235
+ resolve = res;
236
+ reject = rej;
237
+ });
238
+ promise.catch(() => undefined); // surfaced to awaiting callers
239
+ // Exactly one of open/error/timeout/close wins; later outcomes are no-ops.
240
+ const settle = () => {
241
+ if (settled)
242
+ return false;
243
+ settled = true;
244
+ if (timer !== undefined)
245
+ clearTimeout(timer);
246
+ if (member.opening === attempt)
247
+ member.opening = undefined; // never clobber a newer attempt
248
+ return true;
249
+ };
250
+ const attempt = {
251
+ promise,
252
+ abandon: (reason) => {
253
+ if (settle())
254
+ reject(reason);
255
+ },
256
+ };
257
+ if (__classPrivateFieldGet(this, _FamilyRuntime_openTimeoutMs, "f") !== undefined) {
258
+ const ms = __classPrivateFieldGet(this, _FamilyRuntime_openTimeoutMs, "f");
259
+ timer = setTimeout(() => attempt.abandon(new errors_1.FamilyOpenTimeoutError(this.name, member.entry.keyId, ms)), ms);
260
+ }
261
+ let pending;
262
+ try {
263
+ pending = Promise.resolve(__classPrivateFieldGet(this, _FamilyRuntime_provider, "f").open(key));
264
+ }
265
+ catch (err) {
266
+ pending = Promise.reject(err);
267
+ }
268
+ pending.then((db) => {
269
+ if (!settle()) {
270
+ // Late arrival (timed out or family closed): nobody owns this handle.
271
+ void Promise.resolve()
272
+ .then(() => db.close())
273
+ .catch(() => undefined);
274
+ return;
275
+ }
276
+ member.db = db; // single publication point, while waiters still hold leases
277
+ resolve(db);
278
+ }, (err) => {
279
+ if (settle())
280
+ reject(err);
281
+ });
282
+ return attempt;
194
283
  }, _FamilyRuntime_closeHandle = async function _FamilyRuntime_closeHandle(member) {
195
284
  const db = member.db;
196
285
  member.db = undefined;
286
+ __classPrivateFieldGet(this, _FamilyRuntime_instances, "m", _FamilyRuntime_pruneIfIdle).call(this, member);
197
287
  if (db)
198
288
  await db.close();
199
289
  }, _FamilyRuntime_evictIfNeeded = function _FamilyRuntime_evictIfNeeded() {
290
+ // Once closed, closeAll owns every handle: it awaits the close and reports failures.
291
+ if (__classPrivateFieldGet(this, _FamilyRuntime_closed, "f"))
292
+ return;
200
293
  const open = [...__classPrivateFieldGet(this, _FamilyRuntime_members, "f").values()].filter((m) => m.db);
201
294
  if (open.length <= __classPrivateFieldGet(this, _FamilyRuntime_maxOpen, "f"))
202
295
  return;