@supalive/core 1.20.0 → 1.20.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.
@@ -0,0 +1,622 @@
1
+ import { o as utf8Encode, r as supaliveStringify } from "./helper-zdJT5FUc.js";
2
+ import { i as mapReadSetToRaw, s as CORE_INIT_LOCK_NAME, t as checkLogsAffectReadSet } from "./overlap-checker-CCgq_Tpa.js";
3
+ import { C as normalizeIdToBytes, l as OccConflictError, w as normalizeToBytes } from "./types_db-OUou3o2Z.js";
4
+ import mysql from "mysql2/promise";
5
+ //#region src/db/mysql.ts
6
+ function toBufferView(bytes) {
7
+ if (Buffer.isBuffer(bytes)) return bytes;
8
+ return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
9
+ }
10
+ function encodeMysqlWriteParam(value) {
11
+ if (value == null) return value;
12
+ if (value instanceof Map) return JSON.stringify(Object.fromEntries(value));
13
+ if (Array.isArray(value) || Object.getPrototypeOf(value) === Object.prototype) return JSON.stringify(value);
14
+ return value;
15
+ }
16
+ /**
17
+ * mysql2 returns `ResultSetHeader` for non-SELECT queries (carrying
18
+ * `affectedRows`) and `RowDataPacket[]` for SELECTs. The previous shim
19
+ * blindly cast to T[] which made `rowCount` zero for UPDATE/DELETE, breaking
20
+ * CAS detection. This helper picks the right shape.
21
+ */
22
+ function normalizeMysqlResult(raw) {
23
+ if (Array.isArray(raw)) return {
24
+ rows: raw,
25
+ rowCount: raw.length
26
+ };
27
+ if (raw && typeof raw === "object" && "affectedRows" in raw) {
28
+ const header = raw;
29
+ return {
30
+ rows: [],
31
+ rowCount: Number(header.affectedRows) || 0,
32
+ insertId: header.insertId
33
+ };
34
+ }
35
+ return {
36
+ rows: [],
37
+ rowCount: 0
38
+ };
39
+ }
40
+ var MySqlDatabase = class {
41
+ constructor(logger, config, pool = void 0) {
42
+ this.logger = logger;
43
+ const { queryTimeoutMs, ...poolConfig } = config;
44
+ this.queryTimeoutMs = queryTimeoutMs ?? 0;
45
+ this.config = poolConfig;
46
+ if (pool) this.pool = pool;
47
+ else this.pool = mysql.createPool(poolConfig);
48
+ }
49
+ logger;
50
+ config;
51
+ queryTimeoutMs;
52
+ pool;
53
+ dbType = "mysql";
54
+ sqlBuilder = mysqlSql;
55
+ columnTypes = /* @__PURE__ */ new Map();
56
+ sqlCache = /* @__PURE__ */ new Map();
57
+ /**
58
+ * Fetches actual column types from MySQL for `table` and returns a
59
+ * map of DB column name → MySQL type string (as produced by `COLUMN_TYPE`,
60
+ * e.g. `bigint`, `integer`, `varchar(32)`, `text`, `json`).
61
+ * No caching here — see {@link getColumnTypes} for the cached path.
62
+ */
63
+ async loadColumnTypesFromMySql(table) {
64
+ const result = await this.query(`SELECT COLUMN_NAME AS column_name, COLUMN_TYPE AS mysql_type
65
+ FROM INFORMATION_SCHEMA.COLUMNS
66
+ WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?
67
+ ORDER BY ORDINAL_POSITION`, [table]);
68
+ const out = /* @__PURE__ */ new Map();
69
+ for (const row of result.rows) out.set(row.column_name.toLowerCase(), row.mysql_type);
70
+ return out;
71
+ }
72
+ /**
73
+ * Synchronous column-type lookup. The apply path calls this on every
74
+ * batched write, so it can't await — populate the cache via
75
+ * {@link bootstrapColumnTypes} at server start. Throws if `table` is
76
+ * missing (programmer error / bootstrap skipped).
77
+ */
78
+ getColumnTypes(table) {
79
+ const hit = this.columnTypes.get(table);
80
+ if (!hit) {
81
+ this.logger.warn({ table }, "MySqlDatabase: no cached column types fallback to non-batched writes");
82
+ return null;
83
+ }
84
+ return hit;
85
+ }
86
+ /**
87
+ * Eagerly load column types into the in-memory cache. With a Redis
88
+ * client supplied, attempts the cache key first and falls through to
89
+ * MySQL on miss (writing the result back to Redis). This is the
90
+ * "one server queries MySQL, the rest read from Redis" pattern for cold
91
+ * starts in a multi-instance deploy. Without Redis, always queries MySQL.
92
+ *
93
+ * Pass `tables` to limit the load to a specific list; otherwise every
94
+ * user table in the connected database is loaded.
95
+ *
96
+ * The Redis cache key should encode whatever invalidates types in your
97
+ * environment (typically a schema version / deploy version). The
98
+ * subscriber in supalive-server.ts is expected to publish a reload
99
+ * signal after migrations and invoke {@link invalidateColumnTypes}.
100
+ */
101
+ async bootstrapColumnTypes(opts) {
102
+ this.invalidateColumnTypes();
103
+ const { redis, cacheKey, tables } = opts;
104
+ if (redis) {
105
+ const cached = await redis.getCachedTableColumnTypes(cacheKey);
106
+ if (cached) {
107
+ for (const [table, cols] of Object.entries(cached)) this.columnTypes.set(table, new Map(Object.entries(cols)));
108
+ return;
109
+ }
110
+ }
111
+ const names = tables ?? await this.listUserTables();
112
+ for (const t of names) {
113
+ if (this.columnTypes.has(t)) continue;
114
+ const fresh = await this.loadColumnTypesFromMySql(t);
115
+ this.columnTypes.set(t, fresh);
116
+ }
117
+ if (redis) {
118
+ const dump = {};
119
+ for (const [table, cols] of this.columnTypes) dump[table] = Object.fromEntries(Object.entries(cols).sort());
120
+ await redis.setCachedTableColumnsTypes(cacheKey, dump);
121
+ }
122
+ }
123
+ /**
124
+ * Invalidate cached types (and dependent SQL) for `table`, or the entire
125
+ * cache if `table` is omitted. The Redis-pubsub reload signal in
126
+ * supalive-server.ts is expected to call this on schema changes.
127
+ */
128
+ invalidateColumnTypes(table) {
129
+ if (table) {
130
+ this.columnTypes.delete(table);
131
+ for (const k of [...this.sqlCache.keys()]) if (k.includes(`:${table}:`) || k === `d:${table}`) this.sqlCache.delete(k);
132
+ } else {
133
+ this.columnTypes.clear();
134
+ this.sqlCache.clear();
135
+ }
136
+ }
137
+ /** Replace this instance's caches with shared references from a parent.
138
+ * Used by getTransactionClient so types loaded once at startup are
139
+ * visible inside transactions. */
140
+ adoptCaches(columnTypes, sqlCache) {
141
+ this.columnTypes = columnTypes;
142
+ this.sqlCache = sqlCache;
143
+ }
144
+ /** Enumerate the user-defined tables in the connected database for mysql — used
145
+ * by bootstrapColumnTypes() when no explicit list is provided. */
146
+ async listUserTables() {
147
+ return (await this.query(`SELECT table_name FROM information_schema.tables
148
+ WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE'
149
+ AND table_name NOT IN ('commit_logs', 'metadata', 'global_commit_ts')`)).rows.map((r) => r.table_name);
150
+ }
151
+ async getCoreVersion() {
152
+ const result = await this.query("SELECT meta_value FROM metadata WHERE meta_key = 'core_version'");
153
+ return BigInt(result.rows[0]?.meta_value ?? 0);
154
+ }
155
+ async getAppMigration() {
156
+ const result = await this.query("SELECT meta_value FROM metadata WHERE meta_key = 'app_migration'");
157
+ return BigInt(result.rows[0]?.meta_value ?? 0);
158
+ }
159
+ coreMigrationStatements(toVersion) {
160
+ if (toVersion === 1n) return [
161
+ `INSERT IGNORE INTO global_commit_ts SELECT 0 WHERE NOT EXISTS (SELECT 1 FROM global_commit_ts)`,
162
+ `INSERT IGNORE INTO metadata (meta_key, meta_value) VALUES ('latest_committed_ts', 0)`,
163
+ `INSERT IGNORE INTO metadata (meta_key, meta_value) VALUES ('min_retained_ts', 0)`
164
+ ];
165
+ return [];
166
+ }
167
+ async withCoreInitLock(fn) {
168
+ const conn = await this.pool.getConnection();
169
+ try {
170
+ const [rows] = await conn.query("SELECT GET_LOCK(?, 30) AS locked", [CORE_INIT_LOCK_NAME]);
171
+ if ((Array.isArray(rows) ? rows[0]?.locked : void 0) !== 1) throw new Error(`Failed to acquire core-init lock '${CORE_INIT_LOCK_NAME}'`);
172
+ await fn({
173
+ exec: async (sql, params = []) => {
174
+ const [r] = await conn.query(sql, params);
175
+ return normalizeMysqlResult(r);
176
+ },
177
+ getVersion: async () => {
178
+ const [r] = await conn.query("SELECT meta_value FROM metadata WHERE meta_key = 'core_version'");
179
+ const row = Array.isArray(r) ? r[0] : void 0;
180
+ return BigInt(row?.meta_value ?? 0);
181
+ },
182
+ setVersion: async (version) => {
183
+ await conn.query(`INSERT INTO metadata (meta_key, meta_value) VALUES ('core_version', ?)
184
+ ON DUPLICATE KEY UPDATE meta_value = VALUES(meta_value)`, [version.toString()]);
185
+ }
186
+ });
187
+ } finally {
188
+ await conn.query("SELECT RELEASE_LOCK(?)", [CORE_INIT_LOCK_NAME]).catch(() => {});
189
+ conn.release();
190
+ }
191
+ }
192
+ async query(sql, params = []) {
193
+ const client = await this.pool.getConnection();
194
+ try {
195
+ const [rows] = this.queryTimeoutMs > 0 ? await client.query({
196
+ sql,
197
+ values: params,
198
+ timeout: this.queryTimeoutMs
199
+ }) : await client.query(sql, params);
200
+ return normalizeMysqlResult(rows);
201
+ } catch (err) {
202
+ throw translateMysqlError(err);
203
+ } finally {
204
+ client.release();
205
+ }
206
+ }
207
+ async getClient() {
208
+ return await this.pool.getConnection();
209
+ }
210
+ async getTransactionClient() {
211
+ const client = await this.pool.getConnection();
212
+ const tx = new MySqlTxDatabase(this.logger, {
213
+ ...this.config,
214
+ queryTimeoutMs: this.queryTimeoutMs
215
+ }, this.pool, client);
216
+ tx.adoptCaches(this.columnTypes, this.sqlCache);
217
+ return tx;
218
+ }
219
+ async getLatestSnapshotTimestamp() {
220
+ const result = await this.query("SELECT meta_value FROM metadata WHERE meta_key = 'latest_committed_ts'");
221
+ return BigInt(result.rows[0]?.meta_value ?? 0);
222
+ }
223
+ async updateLatestSnapshotTimestamp(commitTs) {
224
+ await this.query(`UPDATE metadata SET meta_value = ? WHERE meta_key = 'latest_committed_ts' AND meta_value < ?`, [commitTs, commitTs]);
225
+ }
226
+ async getMinSnapshotTimestamp() {
227
+ const result = await this.query("SELECT meta_value FROM metadata WHERE meta_key = 'min_retained_ts'");
228
+ return BigInt(result.rows[0]?.meta_value ?? 0);
229
+ }
230
+ async updateMinSnapshotTimestamp(ts) {
231
+ await this.query(`UPDATE metadata SET meta_value = ? WHERE meta_key = 'min_retained_ts' AND meta_value < ?`, [ts, ts]);
232
+ }
233
+ async getNextTimestamp() {
234
+ const result = await this.query("UPDATE global_commit_ts SET id = LAST_INSERT_ID(id + 1)");
235
+ return BigInt(result.insertId ?? 0);
236
+ }
237
+ async attachPrevDataToWriteSet(writeSet, readSet) {
238
+ const readTsByKey = /* @__PURE__ */ new Map();
239
+ for (let i = readSet.length - 1; i >= 0; i--) {
240
+ const r = readSet[i];
241
+ if (r.kind !== "point" || r.expectedCommitTs === void 0) continue;
242
+ const key = `${r.table} ${r.id}`;
243
+ if (!readTsByKey.has(key)) readTsByKey.set(key, r.expectedCommitTs);
244
+ }
245
+ const byTable = /* @__PURE__ */ new Map();
246
+ for (const entry of writeSet) {
247
+ if (entry.op !== "update" && entry.op !== "delete") continue;
248
+ const bucket = byTable.get(entry.table);
249
+ if (bucket) bucket.push(entry);
250
+ else byTable.set(entry.table, [entry]);
251
+ }
252
+ if (byTable.size === 0) return;
253
+ for (const [table, entries] of byTable) {
254
+ const ids = entries.map((e) => e.id);
255
+ const sql = `SELECT * FROM ${table} WHERE id IN (${ids.map(() => "?").join(", ")})`;
256
+ const result = await this.query(sql, ids);
257
+ const byId = /* @__PURE__ */ new Map();
258
+ for (const row of result.rows) byId.set(String(row.id), row);
259
+ for (const entry of entries) {
260
+ const row = byId.get(String(entry.id));
261
+ const key = `${entry.table} ${entry.id}`;
262
+ const priorReadTs = readTsByKey.get(key);
263
+ if (row) {
264
+ entry.prevData = row;
265
+ entry.expectedCommitTs = priorReadTs !== void 0 ? priorReadTs : BigInt(row.commit_ts);
266
+ } else if (priorReadTs !== void 0) entry.expectedCommitTs = priorReadTs;
267
+ }
268
+ }
269
+ }
270
+ async prepareCommitWrites(readSet, writeSet, commitTs) {
271
+ const result = [];
272
+ await this.mysqlPrepareBatchWrites(result, writeSet, commitTs);
273
+ await this.mysqlPrepareCommitLogs(result, writeSet, commitTs);
274
+ if (!readSet.some((r) => r.kind === "range")) return {
275
+ commits: result,
276
+ validateReadPoints: this.mysqlPrepareValidatePointReadsByCommitTs(readSet, writeSet),
277
+ hasRangeRead: false
278
+ };
279
+ const rawReadSet = mapReadSetToRaw(readSet);
280
+ return {
281
+ commits: result,
282
+ hasRangeRead: true,
283
+ tableIds: [...new Set(rawReadSet.map((r) => r.tableId))],
284
+ rawReadSet
285
+ };
286
+ }
287
+ async mysqlPrepareBatchWrites(result, writeSet, commitTs) {
288
+ let i = 0;
289
+ let counter = 0;
290
+ while (i < writeSet.length) {
291
+ counter++;
292
+ const { table, op } = writeSet[i];
293
+ let j = i + 1;
294
+ while (j < writeSet.length && writeSet[j].table === table && writeSet[j].op === op) j++;
295
+ const batch = writeSet.slice(i, j);
296
+ if (op === "insert") await this.mysqlPrepareInsertBatch(result, table, batch, commitTs);
297
+ else if (op === "update") await this.mysqlPrepareUpdateSingle(result, table, batch, commitTs);
298
+ else await this.mysqlPrepareDeleteBatch(result, table, batch);
299
+ i = j;
300
+ if (counter % 50 == 0) await new Promise((resolve) => setImmediate(resolve));
301
+ }
302
+ }
303
+ async mysqlPrepareInsertBatch(result, table, entries, commitTs) {
304
+ if (entries.length === 0) return;
305
+ const groups = /* @__PURE__ */ new Map();
306
+ for (const entry of entries) {
307
+ const cols = [...Object.keys(entry.data ?? {}), "commit_ts"].sort();
308
+ const key = cols.join("|");
309
+ let g = groups.get(key);
310
+ if (!g) {
311
+ g = {
312
+ cols,
313
+ rows: []
314
+ };
315
+ groups.set(key, g);
316
+ }
317
+ g.rows.push(entry);
318
+ }
319
+ let counter = 0;
320
+ for (const { cols, rows } of groups.values()) {
321
+ counter++;
322
+ const values = [];
323
+ const tuples = [];
324
+ for (const entry of rows) {
325
+ const data = {
326
+ ...entry.data ?? {},
327
+ commit_ts: commitTs
328
+ };
329
+ tuples.push(`(${cols.map(() => "?").join(", ")})`);
330
+ for (const col of cols) values.push(encodeMysqlWriteParam(data[col]));
331
+ }
332
+ if (counter % 50 == 0) await new Promise((resolve) => setImmediate(resolve));
333
+ result.push({
334
+ sql: `INSERT INTO ${table} (${cols.join(", ")}) VALUES ${tuples.join(", ")}`,
335
+ params: values,
336
+ operation: "insertBatch",
337
+ table
338
+ });
339
+ }
340
+ }
341
+ async mysqlPrepareUpdateSingle(result, table, entries, commitTs) {
342
+ if (entries.length === 0) return;
343
+ let counter = 0;
344
+ for (const entry of entries) {
345
+ counter++;
346
+ const data = {
347
+ ...entry.data ?? {},
348
+ commit_ts: commitTs
349
+ };
350
+ const sets = Object.keys(data).map((c) => `${c} = ?`);
351
+ const values = Object.values(data).map(encodeMysqlWriteParam);
352
+ const expected = entry.expectedCommitTs ?? -1n;
353
+ if (counter % 50 == 0) await new Promise((resolve) => setImmediate(resolve));
354
+ result.push({
355
+ sql: `UPDATE ${table} SET ${sets.join(", ")} WHERE id = ? AND commit_ts = ?`,
356
+ params: [
357
+ ...values,
358
+ entry.id,
359
+ expected
360
+ ],
361
+ operation: "updateSingle",
362
+ table,
363
+ entryId: entry.id,
364
+ expectedTs: expected
365
+ });
366
+ }
367
+ }
368
+ async mysqlPrepareDeleteBatch(result, table, entries) {
369
+ if (entries.length === 0) return;
370
+ let counter = 0;
371
+ for (const entry of entries) {
372
+ counter++;
373
+ const expected = entry.expectedCommitTs ?? -1n;
374
+ if (counter % 50 == 0) await new Promise((resolve) => setImmediate(resolve));
375
+ result.push({
376
+ sql: `DELETE FROM ${table} WHERE id = ? AND commit_ts = ?`,
377
+ params: [entry.id, expected],
378
+ table,
379
+ operation: "deleteSingle",
380
+ entryId: entry.id,
381
+ expectedTs: expected
382
+ });
383
+ }
384
+ }
385
+ async mysqlPrepareCommitLogs(result, writeEntries, commitTs) {
386
+ if (writeEntries.length === 0) return;
387
+ const values = [];
388
+ const tuples = [];
389
+ let counter = 0;
390
+ for (const entry of writeEntries) {
391
+ counter++;
392
+ tuples.push(`(?, ?, ?, ?)`);
393
+ values.push(toBufferView(normalizeIdToBytes(entry.id)), commitTs, toBufferView(entry.tableId), toBufferView(utf8Encode(supaliveStringify(entry))));
394
+ if (counter % 50 == 0) await new Promise((resolve) => setImmediate(resolve));
395
+ }
396
+ result.push({
397
+ sql: `INSERT INTO commit_logs (id, ts, table_id, data) VALUES ${tuples.join(", ")}`,
398
+ params: values,
399
+ table: "commit_logs",
400
+ operation: "commit_logs"
401
+ });
402
+ }
403
+ /**
404
+ * Point-only OCC check. For each genuine point read, compare the row's
405
+ * current commit_ts against the value captured at read time. A `null`
406
+ * expectedCommitTs means "row didn't exist when we read"; any row found
407
+ * now is a conflict. Reads whose row is being written in this same txn
408
+ * are skipped — the CAS UPDATE/DELETE already validated those.
409
+ */
410
+ mysqlPrepareValidatePointReadsByCommitTs(readSet, writeSet) {
411
+ const result = [];
412
+ const writeKeys = /* @__PURE__ */ new Set();
413
+ for (const w of writeSet) writeKeys.add(`${w.table} ${w.id}`);
414
+ const byTable = /* @__PURE__ */ new Map();
415
+ const seen = /* @__PURE__ */ new Set();
416
+ for (const entry of readSet) {
417
+ if (entry.kind !== "point") continue;
418
+ const key = `${entry.table} ${entry.id}`;
419
+ if (writeKeys.has(key) || seen.has(key)) continue;
420
+ seen.add(key);
421
+ if (entry.expectedCommitTs === void 0) continue;
422
+ const bucket = byTable.get(entry.table);
423
+ if (bucket) bucket.push({
424
+ entry,
425
+ key
426
+ });
427
+ else byTable.set(entry.table, [{
428
+ entry,
429
+ key
430
+ }]);
431
+ }
432
+ for (const [table, pendings] of byTable) {
433
+ const ids = pendings.map((p) => p.entry.id);
434
+ const placeholders = ids.map(() => "?").join(", ");
435
+ result.push({
436
+ sql: `SELECT id, commit_ts FROM ${table} WHERE id IN (${placeholders})`,
437
+ params: ids,
438
+ table,
439
+ operation: "pointReadValidation",
440
+ pendingsEntry: pendings
441
+ });
442
+ }
443
+ return result;
444
+ }
445
+ async commitWrites(beginTs, queries, commitTs) {
446
+ await this.mysqlApplyWrites(queries.commits);
447
+ if (!queries.hasRangeRead) {
448
+ if ((queries.validateReadPoints?.length ?? 0) > 0) await this.validatePointReadsByCommitTs(queries.validateReadPoints ?? []);
449
+ return { success: true };
450
+ }
451
+ const logs = await this.getCommitLogsSinceTs(beginTs, commitTs, queries.tableIds ?? []);
452
+ if (checkLogsAffectReadSet(logs, queries.rawReadSet ?? [])) throw new OccConflictError(`Conflict detected: overlapping commit logs since ${beginTs}`, { logs });
453
+ return { success: true };
454
+ }
455
+ async mysqlApplyWrites(result) {
456
+ for (const q of result) switch (q.operation) {
457
+ case "insertBatch":
458
+ try {
459
+ await this.query(q.sql, q.params);
460
+ } catch (err) {
461
+ if (err && typeof err === "object" && (err.errno === 1062 || err.code === "ER_DUP_ENTRY")) {
462
+ const failedId = (err.message ?? "").match(/key '[^']+'.*?\(.*?\)=\(?([^,\)]+)/)?.[1];
463
+ throw new OccConflictError(failedId !== void 0 ? `Conflict: insert into ${q.table}/${failedId} hit duplicate key` : `Conflict: batch insert into ${q.table} hit duplicate key`, err);
464
+ }
465
+ throw err;
466
+ }
467
+ break;
468
+ case "updateSingle":
469
+ if (((await this.query(q.sql, q.params)).rowCount ?? 0) === 0) throw new OccConflictError(`Conflict: CAS update of ${q.table}/${q.entryId} (expected commit_ts=${q.expectedTs}) matched no rows`, {
470
+ table: q.table,
471
+ id: q.entryId,
472
+ expected: q.expectedTs
473
+ });
474
+ break;
475
+ case "deleteSingle":
476
+ if (((await this.query(q.sql, q.params)).rowCount ?? 0) === 0) throw new OccConflictError(`Conflict: CAS delete of ${q.table}/${q.entryId} (expected commit_ts=${q.expectedTs}) matched no rows`, {
477
+ table: q.table,
478
+ id: q.entryId,
479
+ expected: q.expectedTs
480
+ });
481
+ break;
482
+ case "commit_logs":
483
+ await this.query(q.sql, q.params);
484
+ break;
485
+ default: throw new Error("Unhandled MySQL Operation: " + q.operation);
486
+ }
487
+ }
488
+ async validatePointReadsByCommitTs(queries) {
489
+ for (const q of queries) {
490
+ const result = await this.query(q.sql, q.params);
491
+ const currentById = /* @__PURE__ */ new Map();
492
+ for (const row of result.rows) currentById.set(String(row.id), BigInt(row.commit_ts));
493
+ for (const { entry } of q.pendingsEntry ?? []) {
494
+ const current = currentById.get(String(entry.id));
495
+ const expected = entry.expectedCommitTs;
496
+ if (expected === null) {
497
+ if (current !== void 0) throw new OccConflictError(`Conflict: row ${entry.table}/${entry.id} was inserted concurrently`, {
498
+ table: entry.table,
499
+ id: entry.id
500
+ });
501
+ } else {
502
+ if (current === void 0) throw new OccConflictError(`Conflict: row ${entry.table}/${entry.id} was deleted concurrently`, {
503
+ table: entry.table,
504
+ id: entry.id,
505
+ expected
506
+ });
507
+ if (current !== expected) throw new OccConflictError(`Conflict: row ${entry.table}/${entry.id} changed from ts=${expected} to ts=${current}`, {
508
+ table: entry.table,
509
+ id: entry.id,
510
+ expected,
511
+ current
512
+ });
513
+ }
514
+ }
515
+ }
516
+ }
517
+ async pruneCommitLogsBefore(ts) {
518
+ const result = await this.query(`DELETE FROM commit_logs WHERE ts < ?`, [ts]);
519
+ return BigInt(result.rowCount ?? 0);
520
+ }
521
+ async getMinCommitLogTs() {
522
+ const min = (await this.query(`SELECT min(ts) AS min FROM commit_logs`)).rows[0]?.min;
523
+ return min === null || min === void 0 ? null : BigInt(min);
524
+ }
525
+ async getCommitLogsBetweenTs(beginTs, endTs, tableIds) {
526
+ if (tableIds.length === 0) return [];
527
+ const sql = `SELECT id, ts, table_id, data FROM commit_logs WHERE ts >= ? AND ts < ? AND table_id IN (${tableIds.map(() => "?").join(", ")})`;
528
+ return (await this.query(sql, [
529
+ beginTs,
530
+ endTs,
531
+ ...tableIds.map(toBufferView)
532
+ ])).rows.map((row) => ({
533
+ id: normalizeToBytes(row.id),
534
+ ts: BigInt(row.ts),
535
+ tableId: normalizeToBytes(row.table_id),
536
+ data: normalizeToBytes(row.data)
537
+ }));
538
+ }
539
+ async hasCommitForPoints(beginTs, endTs, points) {
540
+ if (points.length === 0) return false;
541
+ const sql = `SELECT 1 FROM commit_logs WHERE ts >= ? AND ts < ? AND (table_id, id) IN (${points.map(() => "(?, ?)").join(", ")}) LIMIT 1`;
542
+ const params = [beginTs, endTs];
543
+ for (const p of points) params.push(toBufferView(p.tableId), toBufferView(p.id));
544
+ return (await this.query(sql, params)).rows.length > 0;
545
+ }
546
+ async getCommitLogsSinceTs(sinceTs, excludeTs, tableIds) {
547
+ if (tableIds.length === 0) return [];
548
+ const sql = `SELECT id, ts, table_id, data FROM commit_logs WHERE ts >= ? AND ts <> ? AND table_id IN (${tableIds.map(() => "?").join(", ")})`;
549
+ return (await this.query(sql, [
550
+ sinceTs,
551
+ excludeTs,
552
+ ...tableIds.map(toBufferView)
553
+ ])).rows.map((row) => ({
554
+ id: normalizeToBytes(row.id),
555
+ ts: BigInt(row.ts),
556
+ tableId: normalizeToBytes(row.table_id),
557
+ data: normalizeToBytes(row.data)
558
+ }));
559
+ }
560
+ async close() {
561
+ await this.pool.end();
562
+ }
563
+ };
564
+ var MySqlTxDatabase = class extends MySqlDatabase {
565
+ constructor(logger, config, pool, client) {
566
+ super(logger, config, pool);
567
+ this.client = client;
568
+ }
569
+ client;
570
+ async query(sql, params = []) {
571
+ try {
572
+ const [rows] = this.queryTimeoutMs > 0 ? await this.client.query({
573
+ sql,
574
+ values: params,
575
+ timeout: this.queryTimeoutMs
576
+ }) : await this.client.query(sql, params);
577
+ return normalizeMysqlResult(rows);
578
+ } catch (err) {
579
+ throw translateMysqlError(err);
580
+ }
581
+ }
582
+ async begin() {
583
+ await this.client.beginTransaction();
584
+ }
585
+ async commit() {
586
+ await this.client.commit();
587
+ }
588
+ async rollback() {
589
+ await this.client.rollback();
590
+ }
591
+ release() {
592
+ this.client.release();
593
+ }
594
+ };
595
+ /**
596
+ * MySQL InnoDB deadlock (errno 1213 / SQLSTATE 40001) is transient and
597
+ * semantically equivalent to an OCC conflict from the mutation runner's
598
+ * perspective: re-execute the handler with a fresh snapshot. Wrapping
599
+ * here lets realtime_db.ts stay driver-agnostic — its retry loop already
600
+ * handles OccConflictError.
601
+ */
602
+ function translateMysqlError(err) {
603
+ if (err && typeof err === "object" && (err.errno === 1213 || err.code === "ER_LOCK_DEADLOCK")) return new OccConflictError("deadlock detected", err);
604
+ return err;
605
+ }
606
+ const mysqlSql = {
607
+ placeholder: (i) => `?`,
608
+ like: (col, i) => `${col} LIKE ?`,
609
+ ilike: (col, i) => `${col} LIKE ?`,
610
+ icontains: (col, i) => `${col} LIKE ?`,
611
+ jsonContains: (col, _i, opts) => opts?.pathIndex !== void 0 ? `JSON_CONTAINS(${col}, CAST(? AS JSON), ?)` : `JSON_CONTAINS(${col}, CAST(? AS JSON))`,
612
+ jsonContainedBy: (col, _i, opts) => opts?.pathIndex !== void 0 ? `JSON_CONTAINS(CAST(? AS JSON), JSON_EXTRACT(${col}, ?))` : `JSON_CONTAINS(CAST(? AS JSON), ${col})`,
613
+ jsonHasKey: (col, _i, opts) => {
614
+ if (opts?.pathCount && opts.pathCount > 0) return `JSON_CONTAINS_PATH(${col}, ${opts.mode === "all" ? "'all'" : "'one'"}, ${new Array(opts.pathCount).fill("?").join(", ")})`;
615
+ return opts?.raw ? `JSON_CONTAINS_PATH(${col}, 'one', ?)` : `JSON_CONTAINS_PATH(${col}, 'one', CONCAT('$."', REPLACE(?, '"', '\\\\"'), '"'))`;
616
+ },
617
+ jsonArrayContains: (col, _i, opts) => opts?.pathIndex !== void 0 ? `JSON_CONTAINS(${col}, JSON_ARRAY(CAST(? AS JSON)), ?)` : `JSON_CONTAINS(${col}, JSON_ARRAY(CAST(? AS JSON)))`
618
+ };
619
+ //#endregion
620
+ export { MySqlDatabase as t };
621
+
622
+ //# sourceMappingURL=mysql-BX3cm94v.js.map