88 validateUpdateTodo ,
99} from "../db/validation"
1010import type { Express } from "express"
11+ import type { Txid } from "@tanstack/db-collections"
1112
1213// Create Express app
1314const app : Express = express ( )
@@ -23,15 +24,19 @@ app.get(`/api/health`, (req, res) => {
2324} )
2425
2526// Generate a transaction ID
26- async function generateTxId ( tx : any ) : Promise < string > {
27- const result = await tx `SELECT txid_current() as txid`
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`
2833 const txid = result [ 0 ] ?. txid
2934
3035 if ( txid === undefined ) {
3136 throw new Error ( `Failed to get transaction ID` )
3237 }
3338
34- return String ( txid )
39+ return parseInt ( txid , 10 )
3540}
3641
3742// ===== TODOS API =====
@@ -75,7 +80,7 @@ app.post(`/api/todos`, async (req, res) => {
7580 try {
7681 const todoData = validateInsertTodo ( req . body )
7782
78- let txid ! : string
83+ let txid ! : Txid
7984 const newTodo = await sql . begin ( async ( tx ) => {
8085 txid = await generateTxId ( tx )
8186
@@ -102,7 +107,7 @@ app.put(`/api/todos/:id`, async (req, res) => {
102107 const { id } = req . params
103108 const todoData = validateUpdateTodo ( req . body )
104109
105- let txid ! : string
110+ let txid ! : Txid
106111 const updatedTodo = await sql . begin ( async ( tx ) => {
107112 txid = await generateTxId ( tx )
108113
@@ -139,7 +144,7 @@ app.delete(`/api/todos/:id`, async (req, res) => {
139144 try {
140145 const { id } = req . params
141146
142- let txid ! : string
147+ let txid ! : Txid
143148 await sql . begin ( async ( tx ) => {
144149 txid = await generateTxId ( tx )
145150
@@ -210,7 +215,7 @@ app.post(`/api/config`, async (req, res) => {
210215 console . log ( `POST /api/config` , req . body )
211216 const configData = validateInsertConfig ( req . body )
212217
213- let txid ! : string
218+ let txid ! : Txid
214219 const newConfig = await sql . begin ( async ( tx ) => {
215220 txid = await generateTxId ( tx )
216221
@@ -237,7 +242,7 @@ app.put(`/api/config/:id`, async (req, res) => {
237242 const { id } = req . params
238243 const configData = validateUpdateConfig ( req . body )
239244
240- let txid ! : string
245+ let txid ! : Txid
241246 const updatedConfig = await sql . begin ( async ( tx ) => {
242247 txid = await generateTxId ( tx )
243248
@@ -274,7 +279,7 @@ app.delete(`/api/config/:id`, async (req, res) => {
274279 try {
275280 const { id } = req . params
276281
277- let txid ! : string
282+ let txid ! : Txid
278283 await sql . begin ( async ( tx ) => {
279284 txid = await generateTxId ( tx )
280285
0 commit comments