tina4-nodejs 3.13.94 → 3.13.96

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 (123) hide show
  1. package/CLAUDE.md +158 -30
  2. package/README.md +1 -1
  3. package/package.json +3 -1
  4. package/packages/cli/dist/bin.js +30911 -28444
  5. package/packages/cli/src/commands/metrics.ts +17 -11
  6. package/packages/cli/src/commands/serve.ts +10 -9
  7. package/packages/core/dist/index.js +30810 -28261
  8. package/packages/core/public/css/tina4.min.css +1 -1
  9. package/packages/core/src/ai.ts +7 -1
  10. package/packages/core/src/auth.ts +191 -39
  11. package/packages/core/src/background.ts +19 -19
  12. package/packages/core/src/cache.ts +492 -49
  13. package/packages/core/src/devAdmin.ts +79 -32
  14. package/packages/core/src/dispatchPipeline.ts +285 -0
  15. package/packages/core/src/dotenv.ts +185 -40
  16. package/packages/core/src/index.ts +6 -7
  17. package/packages/core/src/logger.ts +257 -36
  18. package/packages/core/src/mcp.ts +1 -1
  19. package/packages/core/src/messenger.ts +294 -106
  20. package/packages/core/src/metrics.ts +199 -961
  21. package/packages/core/src/middleware.ts +390 -123
  22. package/packages/core/src/queue.ts +188 -32
  23. package/packages/core/src/queueBackends/kafkaBackend.ts +1 -1
  24. package/packages/core/src/queueBackends/liteBackend.ts +13 -0
  25. package/packages/core/src/queueBackends/mongoBackend.ts +101 -9
  26. package/packages/core/src/queueBackends/rabbitmqBackend.ts +22 -4
  27. package/packages/core/src/rateLimiter.ts +10 -5
  28. package/packages/core/src/request.ts +34 -16
  29. package/packages/core/src/response.ts +46 -1
  30. package/packages/core/src/router.ts +29 -4
  31. package/packages/core/src/server.ts +886 -421
  32. package/packages/core/src/session.ts +244 -27
  33. package/packages/core/src/sessionHandlers/databaseHandler.ts +338 -48
  34. package/packages/core/src/sessionHandlers/memcachedHandler.ts +181 -0
  35. package/packages/core/src/sessionHandlers/mongoClient.ts +293 -208
  36. package/packages/core/src/sessionHandlers/mongoHandler.ts +88 -8
  37. package/packages/core/src/sessionHandlers/respClient.ts +16 -147
  38. package/packages/core/src/sessionHandlers/sqlClient.ts +290 -0
  39. package/packages/core/src/sessionHandlers/syncBridge.ts +190 -0
  40. package/packages/core/src/sessionHandlers/syncSocket.ts +236 -0
  41. package/packages/core/src/testClient.ts +18 -5
  42. package/packages/core/src/trustedProxy.ts +249 -0
  43. package/packages/core/src/types.ts +29 -5
  44. package/packages/core/src/websocket.ts +66 -0
  45. package/packages/orm/dist/index.js +22717 -20168
  46. package/packages/orm/src/adapters/firebird.ts +183 -56
  47. package/packages/orm/src/adapters/mongodb.ts +25 -4
  48. package/packages/orm/src/adapters/mssql.ts +114 -29
  49. package/packages/orm/src/adapters/mysql.ts +103 -40
  50. package/packages/orm/src/adapters/odbc.ts +44 -21
  51. package/packages/orm/src/adapters/postgres.ts +118 -26
  52. package/packages/orm/src/adapters/sqlDialect.ts +120 -0
  53. package/packages/orm/src/adapters/sqlite.ts +60 -24
  54. package/packages/orm/src/autoCrud.ts +12 -10
  55. package/packages/orm/src/baseModel.ts +135 -40
  56. package/packages/orm/src/cachedDatabase.ts +43 -19
  57. package/packages/orm/src/connectTimeout.ts +265 -0
  58. package/packages/orm/src/database.ts +241 -197
  59. package/packages/orm/src/databaseResult.ts +51 -28
  60. package/packages/orm/src/databaseUrl.ts +484 -0
  61. package/packages/orm/src/docstore.ts +386 -145
  62. package/packages/orm/src/index.ts +13 -6
  63. package/packages/orm/src/migration.ts +44 -11
  64. package/packages/orm/src/model.ts +4 -0
  65. package/packages/orm/src/queryBuilder.ts +47 -6
  66. package/packages/orm/src/sqlTranslator.ts +310 -4
  67. package/packages/orm/src/types.ts +21 -77
  68. package/packages/swagger/dist/index.js +78 -20
  69. package/packages/swagger/src/generator.ts +172 -29
  70. package/types/core/src/ai.d.ts +1 -1
  71. package/types/core/src/auth.d.ts +28 -5
  72. package/types/core/src/background.d.ts +3 -3
  73. package/types/core/src/cache.d.ts +15 -12
  74. package/types/core/src/dispatchPipeline.d.ts +117 -0
  75. package/types/core/src/dotenv.d.ts +38 -16
  76. package/types/core/src/index.d.ts +6 -9
  77. package/types/core/src/logger.d.ts +93 -16
  78. package/types/core/src/messenger.d.ts +47 -6
  79. package/types/core/src/metrics.d.ts +25 -61
  80. package/types/core/src/middleware.d.ts +134 -11
  81. package/types/core/src/queue.d.ts +54 -5
  82. package/types/core/src/queueBackends/kafkaBackend.d.ts +1 -1
  83. package/types/core/src/queueBackends/liteBackend.d.ts +9 -0
  84. package/types/core/src/queueBackends/mongoBackend.d.ts +24 -2
  85. package/types/core/src/queueBackends/rabbitmqBackend.d.ts +3 -3
  86. package/types/core/src/router.d.ts +14 -3
  87. package/types/core/src/server.d.ts +15 -4
  88. package/types/core/src/session.d.ts +87 -2
  89. package/types/core/src/sessionHandlers/databaseHandler.d.ts +60 -5
  90. package/types/core/src/sessionHandlers/memcachedHandler.d.ts +60 -0
  91. package/types/core/src/sessionHandlers/mongoClient.d.ts +16 -5
  92. package/types/core/src/sessionHandlers/mongoHandler.d.ts +51 -3
  93. package/types/core/src/sessionHandlers/respClient.d.ts +2 -2
  94. package/types/core/src/sessionHandlers/sqlClient.d.ts +39 -0
  95. package/types/core/src/sessionHandlers/syncBridge.d.ts +91 -0
  96. package/types/core/src/sessionHandlers/syncSocket.d.ts +49 -0
  97. package/types/core/src/trustedProxy.d.ts +44 -0
  98. package/types/core/src/types.d.ts +28 -5
  99. package/types/core/src/websocket.d.ts +26 -0
  100. package/types/orm/src/adapters/firebird.d.ts +55 -10
  101. package/types/orm/src/adapters/mongodb.d.ts +2 -2
  102. package/types/orm/src/adapters/mssql.d.ts +18 -11
  103. package/types/orm/src/adapters/mysql.d.ts +11 -10
  104. package/types/orm/src/adapters/odbc.d.ts +9 -12
  105. package/types/orm/src/adapters/postgres.d.ts +11 -10
  106. package/types/orm/src/adapters/sqlDialect.d.ts +71 -0
  107. package/types/orm/src/adapters/sqlite.d.ts +15 -3
  108. package/types/orm/src/baseModel.d.ts +45 -9
  109. package/types/orm/src/cachedDatabase.d.ts +18 -5
  110. package/types/orm/src/connectTimeout.d.ts +100 -0
  111. package/types/orm/src/database.d.ts +78 -28
  112. package/types/orm/src/databaseResult.d.ts +29 -15
  113. package/types/orm/src/databaseUrl.d.ts +125 -0
  114. package/types/orm/src/docstore.d.ts +102 -43
  115. package/types/orm/src/index.d.ts +6 -4
  116. package/types/orm/src/migration.d.ts +4 -3
  117. package/types/orm/src/queryBuilder.d.ts +23 -3
  118. package/types/orm/src/sqlTranslator.d.ts +126 -2
  119. package/types/orm/src/types.d.ts +21 -38
  120. package/packages/core/src/scss.ts +0 -623
  121. package/packages/core/src/sessionHandlers/redisHandler.ts +0 -219
  122. package/types/core/src/scss.d.ts +0 -19
  123. package/types/core/src/sessionHandlers/redisHandler.d.ts +0 -60
@@ -7,11 +7,8 @@ export type {
7
7
  ColumnInfo,
8
8
  QueryOptions,
9
9
  RelationshipDefinition,
10
- PaginatedResult,
11
10
  } from "./types.js";
12
11
 
13
- export { FetchResult } from "./types.js";
14
-
15
12
  export { DatabaseResult } from "./databaseResult.js";
16
13
  export type { ColumnInfoResult } from "./databaseResult.js";
17
14
  export { Database, initDatabase, getAdapter, setAdapter, bindDatabase, createAdapterFromUrl, closeDatabase, parseDatabaseUrl, setNamedAdapter, getNamedAdapter, resolveDbPool, stripTrailingSemicolons, wrapWithCache, resetRequestCaches } from "./database.js";
@@ -21,7 +18,9 @@ export {
21
18
  adapterTableExists, adapterTables, adapterColumns, adapterCreateTable,
22
19
  extractLastInsertId,
23
20
  } from "./database.js";
24
- export type { DatabaseConfig, ParsedDatabaseUrl } from "./database.js";
21
+ export type { DatabaseConfig } from "./database.js";
22
+ export { DatabaseUrl, redactCredentials } from "./databaseUrl.js";
23
+ export type { DatabaseEngine } from "./databaseUrl.js";
25
24
  export { discoverModels } from "./model.js";
26
25
  export type { DiscoveredModel } from "./model.js";
27
26
  export {
@@ -54,6 +53,14 @@ export type { ValidationError } from "./validation.js";
54
53
  export { BaseModel, snakeToCamel, camelToSnake } from "./baseModel.js";
55
54
  export { QueryBuilder } from "./queryBuilder.js";
56
55
  export { SQLTranslator, QueryCache } from "./sqlTranslator.js";
56
+ export {
57
+ DEFAULT_DATABASE_CONNECT_TIMEOUT_SECONDS,
58
+ CONNECT_TIMEOUT_TOLERANCE_MS,
59
+ connectTimeoutMillis,
60
+ driverConnectTimeoutMillis,
61
+ connectTarget,
62
+ withConnectTimeout,
63
+ } from "./connectTimeout.js";
57
64
  export { CachedDatabaseAdapter } from "./cachedDatabase.js";
58
65
  export type { CachedAdapterOptions } from "./cachedDatabase.js";
59
66
  export { FakeData } from "./fakeData.js";
@@ -62,8 +69,8 @@ export type { SeedSummary, SeedOptions } from "./seeder.js";
62
69
 
63
70
  // DocStore — pymongo-style document store with a zero-config SQLite (JSON1) fallback
64
71
  export {
65
- ObjectId, InvalidId, SqliteDatabase, SqliteCollection, Cursor,
66
- getCollection, isServerless, resetDefaultStore,
72
+ ObjectId, InvalidId, DocStoreDriverMissing, SqliteDatabase, SqliteCollection, Cursor,
73
+ getCollection, isServerless, resetDefaultStore, closeDocStore,
67
74
  encodeValue, decodeValue, compileFilter,
68
75
  } from "./docstore.js";
69
76
  export type {
@@ -217,7 +217,14 @@ export async function syncModels(models: DiscoveredModel[]): Promise<void> {
217
217
  console.log(` Created table: ${tableName}`);
218
218
  } else {
219
219
  // Check for new columns. SQLite exposes the legacy getTableColumns/
220
- // addColumn helpers; other engines use columns()/ALTER TABLE.
220
+ // addColumn helpers; other engines use getColumns()/ALTER TABLE.
221
+ //
222
+ // Collapsing this into getColumns() looks obviously right and is NOT:
223
+ // it broke the legacy NOT NULL migration_id path, which is the bug that
224
+ // wedged every migration for ~20 releases (python#93). getTableColumns
225
+ // reads PRAGMA directly; getColumns goes through schema splitting and
226
+ // does not return the same thing here. Removing it needs its own change
227
+ // with that path tested, not a drive-by in an interface tidy-up.
221
228
  const existingCols = (adapter as any).getTableColumns
222
229
  ? (adapter as SQLiteAdapter).getTableColumns(tableName)
223
230
  : await adapterColumns(adapter, tableName);
@@ -508,8 +515,16 @@ async function recordApplied(
508
515
  */
509
516
  async function trackingColumns(db: DatabaseAdapter): Promise<Set<string>> {
510
517
  try {
511
- // The DatabaseAdapter contract exposes columns(), not getColumns().
512
- const cols = await (db as any).columns?.(MIGRATION_TABLE);
518
+ // The DatabaseAdapter contract exposes getColumns() (feature 3: renamed from
519
+ // columns() to match the other three frameworks and the get- prefix used
520
+ // everywhere else).
521
+ //
522
+ // This reads through `as any`, so the compiler could not catch the rename
523
+ // here - it went silently to undefined, `cols` came back empty, migration_id
524
+ // was left out of the insert and every migration failed on the legacy
525
+ // NOT NULL column. Exactly the failure python#93 caused, from the opposite
526
+ // direction. Kept optional-chained for adapters that predate the contract.
527
+ const cols = await (db as any).getColumns?.(MIGRATION_TABLE);
513
528
  if (!Array.isArray(cols)) return new Set();
514
529
  return new Set(
515
530
  cols.map((c: any) => String(c?.name ?? c ?? "").toLowerCase()).filter(Boolean),
@@ -1161,9 +1176,24 @@ export async function status(
1161
1176
  */
1162
1177
  export async function createMigration(
1163
1178
  description: string,
1164
- options?: { migrationsDir?: string; kind?: "sql" | "class" },
1179
+ options?: { migrationsDir?: string; kind?: "sql" | "code" | "class" },
1165
1180
  ): Promise<string | { upPath: string; downPath: string }> {
1166
- if (options?.kind === "class") {
1181
+ // MEASURED 2026-08-06: the accepted kind differed in every framework -
1182
+ // python "python", php "php", ruby "ruby" OR "python", node "class" - and
1183
+ // NONE validated it, so create_migration(..., kind="python") produced a code
1184
+ // migration in Python and Ruby and a SILENT .sql file in PHP and Node.
1185
+ // "code" is now the canonical spelling in all four; each keeps its own
1186
+ // language name as a legacy alias; anything else raises.
1187
+ const kind = (options?.kind ?? "sql").trim().toLowerCase();
1188
+ if (!["sql", "code", "class"].includes(kind)) {
1189
+ throw new Error(
1190
+ `Unknown migration kind "${kind}". Use "sql" (default) or "code" ` +
1191
+ `(alias: "class"). An unrecognised kind used to produce a .sql file ` +
1192
+ `silently, which is why this now throws.`,
1193
+ );
1194
+ }
1195
+
1196
+ if (kind === "code" || kind === "class") {
1167
1197
  return createClassMigration(description, options);
1168
1198
  }
1169
1199
  const dir = resolve(options?.migrationsDir ?? "migrations");
@@ -1322,15 +1352,18 @@ export class Migration {
1322
1352
  * Scaffold a new migration file.
1323
1353
  *
1324
1354
  * kind="sql" — creates {timestamp}_{description}.sql + .down.sql (default)
1325
- * kind="class" — creates {timestamp}_{description}.ts with a TypeScript class template
1355
+ * kind="code" — creates {timestamp}_{description}.ts with a TypeScript class
1356
+ * template. "class" is accepted as a legacy alias.
1326
1357
  *
1327
1358
  * Returns the path to the created up file (or class file).
1328
1359
  */
1329
- async create(description: string, kind: "sql" | "class" = "sql"): Promise<string | { upPath: string; downPath: string }> {
1330
- if (kind === "class") {
1331
- return createClassMigration(description, { migrationsDir: this.dir });
1332
- }
1333
- return createMigration(description, { migrationsDir: this.dir });
1360
+ async create(
1361
+ description: string,
1362
+ kind: "sql" | "code" | "class" = "sql",
1363
+ ): Promise<string | { upPath: string; downPath: string }> {
1364
+ // Route through createMigration so the validation lives in ONE place - a
1365
+ // second copy of the accepted set is a second place for it to drift.
1366
+ return createMigration(description, { migrationsDir: this.dir, kind });
1334
1367
  }
1335
1368
 
1336
1369
  /** Return list of completed (applied) migration filenames. */
@@ -38,6 +38,10 @@ export async function discoverModels(modelsDir: string): Promise<DiscoveredModel
38
38
 
39
39
  const definition: ModelDefinition = {
40
40
  tableName: ModelClass.tableName,
41
+ // The class name is the type name a generated OpenAPI client wants
42
+ // (`Item`, not `items`). Carry it so Swagger keys components.schemas by
43
+ // it. A model exported as `default` keeps its declared class name here.
44
+ className: typeof ModelClass.name === "string" && ModelClass.name ? ModelClass.name : undefined,
41
45
  fields: ModelClass.fields as Record<string, FieldDefinition>,
42
46
  fieldMapping: ModelClass.fieldMapping as Record<string, string> | undefined,
43
47
  softDelete: ModelClass.softDelete ?? false,
@@ -18,7 +18,8 @@
18
18
  */
19
19
 
20
20
  import type { DatabaseAdapter } from "./types.js";
21
- import { getAdapter, adapterFetch, adapterFetchOne } from "./database.js";
21
+ import { getAdapter, adapterFetch, adapterFetchOne, probeTotal } from "./database.js";
22
+ import { DatabaseResult } from "./databaseResult.js";
22
23
 
23
24
  export class QueryBuilder {
24
25
  private table: string;
@@ -198,21 +199,61 @@ export class QueryBuilder {
198
199
  }
199
200
 
200
201
  /**
201
- * Execute the query and return all matching rows.
202
+ * Execute the query and return a DatabaseResult.
202
203
  *
203
- * @returns Array of row objects.
204
+ * BREAKING (3.13.95, parity): this returned a bare array of rows. The other
205
+ * three frameworks all return the DatabaseResult that `db.fetch()` produces:
206
+ * Python get() -> DatabaseResult (orm/query_builder/__init__.py)
207
+ * PHP get(): mixed -> $this->db->fetch(...)
208
+ * Ruby get -> @db.fetch(...)
209
+ * Node was the odd one out, so the same builder chain returned a different
210
+ * TYPE per language and portable code could not read `.records`, `.count`,
211
+ * `.limit` or `.offset` off it.
212
+ *
213
+ * MIGRATION: read `.records` for the rows.
214
+ * before: const rows = await qb.get(); rows.length
215
+ * after: const result = await qb.get(); result.records.length
216
+ * DatabaseResult is iterable, so `for (const row of result)` and
217
+ * `[...result]` work unchanged, and `response()`/`res.json()` already
218
+ * auto-serialize it to a JSON array.
219
+ *
220
+ * No default LIMIT is applied when `.limit()` was never called (v3.13.39) --
221
+ * a silent cap here was a data-loss-on-read footgun. That is unchanged.
222
+ *
223
+ * @returns DatabaseResult carrying `.records`, `.count`, `.limit`, `.offset`.
204
224
  */
205
- async get<T = Record<string, unknown>>(): Promise<T[]> {
225
+ async get(): Promise<DatabaseResult> {
206
226
  this.ensureDb();
207
227
  const sql = this.toSql();
208
228
  const allParams = [...this.params, ...this.havingParams];
209
229
 
210
- return adapterFetch<T>(
230
+ const queryParams = allParams.length > 0 ? allParams : undefined;
231
+ const rows = await adapterFetch(
211
232
  this.db!,
212
233
  sql,
213
- allParams.length > 0 ? allParams : undefined,
234
+ queryParams,
235
+ this.limitVal,
236
+ this.offsetVal,
237
+ );
238
+
239
+ // Constructed exactly as Database._fetchWithLimit does, so a QueryBuilder
240
+ // result and a db.fetch() result are the same object in the same state --
241
+ // INCLUDING `count`, which is the TRUE total for the filter via the shared
242
+ // COUNT probe (ADR-0043), not rows-returned. Python's get() -> db.fetch()
243
+ // and Ruby's get -> @db.fetch() already carried the true total; Node used to
244
+ // leave it at the row count here, so `QueryBuilder.get().toPaginate()`
245
+ // under-reported `total` while `db.fetch().toPaginate()` did not. The probe
246
+ // is best-effort (undefined on any error -> falls back to rows.length) and
247
+ // only runs when a limit was applied, so an unlimited get() is one query.
248
+ const total = await probeTotal(this.db!, sql, queryParams, this.limitVal);
249
+ return new DatabaseResult(
250
+ rows as Record<string, unknown>[],
251
+ undefined,
252
+ total,
214
253
  this.limitVal,
215
254
  this.offsetVal,
255
+ this.db!,
256
+ sql,
216
257
  );
217
258
  }
218
259
 
@@ -19,6 +19,7 @@
19
19
 
20
20
  // ── SQL Translator ───────────────────────────────────────────
21
21
 
22
+ import { DatabaseUrl } from "./databaseUrl.js";
22
23
  export class SQLTranslator {
23
24
  /**
24
25
  * Convert LIMIT/OFFSET to Firebird ROWS...TO syntax.
@@ -172,6 +173,271 @@ export class SQLTranslator {
172
173
  if (idx === -1) return [null, name];
173
174
  return [name.slice(0, idx), name.slice(idx + 1)];
174
175
  }
176
+
177
+ /**
178
+ * Hard per-statement bind-parameter ceiling per engine. 0 = never collapse.
179
+ * Sourced from test/fixtures/batch_write_contract.json, byte-identical in all
180
+ * four frameworks.
181
+ */
182
+ static readonly MAX_BIND_PARAMS: Record<string, number> = {
183
+ sqlite: 999,
184
+ postgres: 65535,
185
+ mysql: 65535,
186
+ mssql: 2100,
187
+ firebird: 0,
188
+ odbc: 0,
189
+ mongodb: 0,
190
+ };
191
+
192
+ /**
193
+ * The four frameworks do not agree on what an engine calls itself — Python
194
+ * and PHP report "postgresql", Ruby and Node report "postgres". Without
195
+ * normalising, the cap lookup misses and the collapse silently does nothing
196
+ * on the engine with the largest win.
197
+ */
198
+ static readonly ENGINE_ALIASES: Record<string, string> = {
199
+ postgresql: "postgres",
200
+ pgsql: "postgres",
201
+ sqlite3: "sqlite",
202
+ sqlserver: "mssql",
203
+ sqlsrv: "mssql",
204
+ mariadb: "mysql",
205
+ };
206
+
207
+ // The `d` flag records group indices, so the head can be sliced at the exact
208
+ // start of the VALUES group rather than by hunting for a parenthesis (the
209
+ // column list has parentheses too).
210
+ private static readonly INSERT_VALUES =
211
+ /^\s*INSERT\s+INTO\s+.+?\s+VALUES\s*\(([^()]*)\)\s*$/dis;
212
+
213
+ /**
214
+ * Engines whose lastInsertId reports the FIRST generated id of a multi-row
215
+ * INSERT rather than the last. Verified live, not assumed: a 3-row insert
216
+ * into a fresh MySQL table reports 1 while MAX(id) is 3. SQLite, PostgreSQL
217
+ * and MSSQL already report the last, so collapsing does not change them.
218
+ */
219
+ static readonly FIRST_ID_ENGINES: readonly string[] = ["mysql"];
220
+
221
+ /**
222
+ * Normalise a collapsed batch's last id to the LAST row's id.
223
+ *
224
+ * A row-at-a-time batch reports the last row's id simply because the last
225
+ * statement inserted the last row. Collapsing rows into one statement changes
226
+ * that on any engine that reports the FIRST generated id, so this restores
227
+ * the contract instead of quietly redefining it. The ids in one statement are
228
+ * consecutive, so the last is `first + rows - 1`.
229
+ */
230
+ static batchLastId(reportedId: unknown, rowsInChunk: number, engine: string): unknown {
231
+ const lower = (engine ?? "").toLowerCase();
232
+ const name = SQLTranslator.ENGINE_ALIASES[lower] ?? lower;
233
+ if (!SQLTranslator.FIRST_ID_ENGINES.includes(name)) return reportedId;
234
+
235
+ const n = typeof reportedId === "bigint" ? Number(reportedId) : Number(reportedId);
236
+ if (reportedId === null || reportedId === undefined || Number.isNaN(n)) {
237
+ return reportedId; // UUID/ULID key — no successor
238
+ }
239
+ return n + Math.max(rowsInChunk, 1) - 1;
240
+ }
241
+
242
+ /**
243
+ * Collapse a row-at-a-time INSERT batch into chunked multi-row VALUES.
244
+ *
245
+ * A batch that loops one INSERT per row pays a full network round-trip per
246
+ * row, and the round-trip — not SQL building — is the entire cost of a batch
247
+ * write. Measured over 500 rows: PostgreSQL 9848ms row-at-a-time against
248
+ * 15.8ms as a single multi-row statement (625x), MySQL 216x, MSSQL 121x.
249
+ *
250
+ * PURE: no I/O and no engine contact, so the chunking rules are checkable
251
+ * without a database. The live-engine runners prove the rows land.
252
+ *
253
+ * @returns Statements to run INSTEAD of the loop, or an EMPTY array meaning
254
+ * "not collapsible — keep looping", which is always correct.
255
+ */
256
+ static buildBatchInserts(
257
+ sql: string,
258
+ paramSets: unknown[][],
259
+ engine: string,
260
+ ): Array<[string, unknown[]]> {
261
+ const rows = paramSets ?? [];
262
+ if (rows.length < 2) return [];
263
+
264
+ const lower = (engine ?? "").toLowerCase();
265
+ const name = SQLTranslator.ENGINE_ALIASES[lower] ?? lower;
266
+ const cap = SQLTranslator.MAX_BIND_PARAMS[name] ?? 0;
267
+ // Firebird has no multi-row VALUES syntax (verified against a live 5.0.4:
268
+ // -104 Token unknown); ODBC's real ceiling depends on the driver behind it.
269
+ // Emitting SQL the engine cannot parse to save a round-trip is not a trade
270
+ // worth making.
271
+ if (cap <= 0) return [];
272
+
273
+ const upper = sql.toUpperCase();
274
+ // A collapsed statement returns N rows where the caller expects one, and
275
+ // conflict arbitration changes once rows share a statement.
276
+ if (
277
+ upper.includes("RETURNING") ||
278
+ upper.includes("ON CONFLICT") ||
279
+ upper.includes("ON DUPLICATE KEY")
280
+ ) {
281
+ return [];
282
+ }
283
+
284
+ const match = SQLTranslator.INSERT_VALUES.exec(sql);
285
+ if (match === null) return [];
286
+
287
+ // Every slot must be a bare placeholder. `now()` repeated per row inside one
288
+ // statement is not the same write as `now()` evaluated per statement.
289
+ const slots = match[1].split(",").map((s) => s.trim());
290
+ if (slots.length === 0 || slots.some((s) => s !== "?")) return [];
291
+
292
+ const columns = slots.length;
293
+ if (rows.some((params) => params.length !== columns)) return [];
294
+
295
+ const chunkRows = Math.max(1, Math.floor(cap / columns));
296
+ if (chunkRows < 2) return [];
297
+
298
+ const valuesStart = match.indices?.[1]?.[0];
299
+ if (valuesStart === undefined) return [];
300
+ const head = sql.slice(0, valuesStart - 1).trimEnd();
301
+ const oneRow = `(${new Array(columns).fill("?").join(", ")})`;
302
+
303
+ const statements: Array<[string, unknown[]]> = [];
304
+ for (let start = 0; start < rows.length; start += chunkRows) {
305
+ const chunk = rows.slice(start, start + chunkRows);
306
+ const flat: unknown[] = [];
307
+ for (const params of chunk) flat.push(...params);
308
+ statements.push([`${head} ${new Array(chunk.length).fill(oneRow).join(", ")}`, flat]);
309
+ }
310
+ return statements;
311
+ }
312
+
313
+ /**
314
+ * Blank out string literals, quoted identifiers and comments, so a keyword
315
+ * search sees only real SQL. Blanks are spaces of the SAME LENGTH (newlines
316
+ * preserved), so offsets and line structure still line up with the original.
317
+ *
318
+ * This exists because "does the caller's SQL already have a LIMIT?" used to be
319
+ * `sql.toUpperCase().split("--")[0].includes("LIMIT")`, and MEASURED on a real
320
+ * 150-row table with the 100-row cap in force, every one of these returned
321
+ * ALL 150 ROWS instead of 100:
322
+ *
323
+ * SELECT * FROM t WHERE label != 'LIMIT' ORDER BY id -- literal
324
+ * SELECT * FROM t ORDER BY id -- LIMIT 5 -- line comment
325
+ * SELECT * FROM t ORDER BY id /* LIMIT 5 *\/ -- block comment
326
+ *
327
+ * A column named `rate_limit` does it too. That is a silently UNCAPPED read of
328
+ * a whole table, which is the exact production incident the row cap exists to
329
+ * prevent, reachable through an ordinary column name.
330
+ *
331
+ * @param sql Raw SQL, exactly as the caller wrote it.
332
+ * @returns The same string with literals and comments replaced by spaces.
333
+ */
334
+ static scrubSqlText(sql: string): string {
335
+ let out = "";
336
+ let i = 0;
337
+ const blank = (ch: string): string => (ch === "\n" ? "\n" : " ");
338
+
339
+ while (i < sql.length) {
340
+ const c = sql[i];
341
+ const next = sql[i + 1];
342
+
343
+ // '...' string literal, with '' as the embedded-quote escape
344
+ if (c === "'" || c === '"') {
345
+ const quote = c;
346
+ out += " ";
347
+ i++;
348
+ while (i < sql.length) {
349
+ if (sql[i] === quote) {
350
+ if (sql[i + 1] === quote) {
351
+ out += " ";
352
+ i += 2;
353
+ continue;
354
+ }
355
+ out += " ";
356
+ i++;
357
+ break;
358
+ }
359
+ out += blank(sql[i]);
360
+ i++;
361
+ }
362
+ continue;
363
+ }
364
+
365
+ // -- line comment, to end of line
366
+ if (c === "-" && next === "-") {
367
+ while (i < sql.length && sql[i] !== "\n") {
368
+ out += " ";
369
+ i++;
370
+ }
371
+ continue;
372
+ }
373
+
374
+ // /* block comment */
375
+ if (c === "/" && next === "*") {
376
+ out += " ";
377
+ i += 2;
378
+ while (i < sql.length && !(sql[i] === "*" && sql[i + 1] === "/")) {
379
+ out += blank(sql[i]);
380
+ i++;
381
+ }
382
+ if (i < sql.length) {
383
+ out += " ";
384
+ i += 2;
385
+ }
386
+ continue;
387
+ }
388
+
389
+ out += c;
390
+ i++;
391
+ }
392
+
393
+ return out;
394
+ }
395
+
396
+ /**
397
+ * True when the statement ENDS with its own LIMIT clause, so appending another
398
+ * would be wrong (and on SQLite, a syntax error).
399
+ *
400
+ * Anchored to the END on purpose. A bare "contains LIMIT" test also matches a
401
+ * LIMIT inside a subquery, where the OUTER statement still needs its cap. This
402
+ * is tina4-php's `SqlNormalizerTrait::hasTrailingLimit` regex, ported verbatim
403
+ * so all four frameworks answer identically: it accepts a numeric value, `?`,
404
+ * `$1` and `:name` placeholders, MySQL's `LIMIT a, b`, and a trailing OFFSET.
405
+ *
406
+ * @param sql Raw SQL; literals and comments are scrubbed before matching.
407
+ */
408
+ static hasTrailingLimit(sql: string): boolean {
409
+ const val = String.raw`(?:\d+|\?|\$\d+|:\w+|%s)`;
410
+ const re = new RegExp(
411
+ String.raw`\bLIMIT\s+${val}(?:\s*,\s*${val})?(?:\s+OFFSET\s+${val})?\s*;?\s*$`,
412
+ "i",
413
+ );
414
+ return re.test(SQLTranslator.scrubSqlText(sql));
415
+ }
416
+
417
+ /**
418
+ * Append `LIMIT`/`OFFSET` to a statement unless it already carries its own.
419
+ *
420
+ * The clause goes on a NEW LINE. Appending it inline is the second half of the
421
+ * same bug: `SELECT * FROM t -- note` + ` LIMIT 100` puts the clause INSIDE the
422
+ * trailing comment, where SQLite silently ignores it and the whole table comes
423
+ * back. A newline cannot be commented out by a `--` that started on the line
424
+ * above. Trailing semicolons are stripped first for the same reason
425
+ * (`SELECT * FROM t;` + `LIMIT 100` is a syntax error).
426
+ *
427
+ * @param sql The caller's statement.
428
+ * @param limit Row cap to apply; a non-positive value means "no cap".
429
+ * @param offset Rows to skip; omitted or 0 emits no OFFSET.
430
+ */
431
+ static appendLimit(sql: string, limit?: number, offset?: number): string {
432
+ if (limit === undefined || limit === null || limit <= 0) return sql;
433
+ if (SQLTranslator.hasTrailingLimit(sql)) return sql;
434
+
435
+ const trimmed = sql.replace(/[\s;]+$/, "");
436
+ const suffix = offset !== undefined && offset > 0
437
+ ? `LIMIT ${limit} OFFSET ${offset}`
438
+ : `LIMIT ${limit}`;
439
+ return `${trimmed}\n${suffix}`;
440
+ }
175
441
  }
176
442
 
177
443
  // ── Query Cache ──────────────────────────────────────────────
@@ -196,14 +462,54 @@ export class QueryCache {
196
462
  }
197
463
 
198
464
  /**
199
- * Generate a cache key from a SQL query and params.
465
+ * Stable identity of the DATABASE a cache entry came from.
466
+ *
467
+ * `engine://host:port/database` - and deliberately NOTHING else.
468
+ *
469
+ * WHY IT EXISTS: the key used to be `query:${sql}:${params}` with nothing
470
+ * naming the connection, so on any SHARED backend two databases cross-served
471
+ * each other's rows. Two apps pointed at one Redis, or one app with a primary
472
+ * and an analytics connection, silently read each other's data. Identical SQL
473
+ * text across tenants is the COMMON case, not an edge case, so the collision
474
+ * was the normal outcome.
475
+ *
476
+ * WHY NO CREDENTIALS: a password in the key means every rotation silently
477
+ * cold-starts the cache, and a shared backend's key namespace is visible to
478
+ * every tenant of that backend - a secret must never be folded into it. The
479
+ * username is out for the same reason plus a second: two connections
480
+ * differing only by role read the SAME rows and should share the entry.
481
+ *
482
+ * WHY NOTHING PER-PROCESS: no pid, no object id, no salt. Those would isolate
483
+ * the databases by ACCIDENT and destroy the point of a shared cache, because
484
+ * no instance would ever hit another instance's entry.
200
485
  */
201
- static queryKey(sql: string, params?: unknown[]): string {
486
+ static cacheIdentity(url: string): string {
487
+ try {
488
+ const parsed = new DatabaseUrl(url);
489
+ return `${parsed.engine}://${parsed.host ?? ""}:${parsed.port ?? ""}/${parsed.database}`;
490
+ } catch {
491
+ // An unparseable URL still needs a STABLE identity, and falling back to a
492
+ // constant would silently restore the cross-serving bug. The raw URL is
493
+ // stable and distinct; it is only reached for a URL the connection layer
494
+ // is about to reject anyway.
495
+ return url;
496
+ }
497
+ }
498
+
499
+ /**
500
+ * Generate a cache key from DATABASE IDENTITY + SQL + params.
501
+ *
502
+ * The NUL separators keep the three parts from running together, so a table
503
+ * named after the tail of a database name cannot forge another database's
504
+ * key. The key is not hashed here: the only backend with a key-length limit
505
+ * is memcached, and its backend already SHA-256-hashes whatever it is given.
506
+ */
507
+ static queryKey(sql: string, params?: unknown[], identity = ""): string {
202
508
  const paramStr = params ? JSON.stringify(params) : "";
203
- // Simple hash via string combination
204
- return `query:${sql}:${paramStr}`;
509
+ return `query:${identity}\u0000${sql}\u0000${paramStr}`;
205
510
  }
206
511
 
512
+
207
513
  /**
208
514
  * Get a cached value. Returns undefined if expired or missing.
209
515
  */