| 25 | |
| 26 | // Generate a transaction ID |
| 27 | async function generateTxId(tx: any): Promise<Txid> { |
| 28 | // The ::xid cast strips off the epoch, giving you the raw 32-bit value |
| 29 | // that matches what PostgreSQL sends in logical replication streams |
| 30 | // (and then exposed through Electric which we'll match against |
| 31 | // in the client). |
| 32 | const result = await tx`SELECT pg_current_xact_id()::xid::text as txid` |
| 33 | const txid = result[0]?.txid |
| 34 | |
| 35 | if (txid === undefined) { |
| 36 | throw new Error(`Failed to get transaction ID`) |
| 37 | } |
| 38 | |
| 39 | return parseInt(txid, 10) |
| 40 | } |
| 41 | |
| 42 | // ===== TODOS API ===== |
| 43 | |