You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I read in the Neon docs that if I'm using transactions, I should use Pool or Client. And that Pool or Client objects must be connected, used and closed within a single request handler.
I'm using neon-serverless with drizzle-orm without using Pool or Client. Here's my current implementation:
// ...try{awaitdb.transaction(async(tx)=>{// Find an available dining tableconstavailableTable=awaittx.select().from(diningTables).where(and(// Ensure the table has sufficient capacitygte(diningTables.capacity,Number(guests)),// Ensure no overlapping reservations exist for this tablenot(exists(tx.select().from(reservations).where(and(eq(reservations.diningTableId,diningTables.id),eq(reservations.reservationDate,date),lt(reservations.startTime,endTime),gt(reservations.endTime,time))))))).limit(1).execute().then((res)=>res[0]);if(!availableTable){thrownewError('No available dining table.');}// Insert the reservationawaittx.insert(reservations).values({diningTableId: availableTable.id,name: name,email: email,notes: notes,reservationDate: date,startTime: time,endTime: endTime}).execute();});}catch(error: unknown){if(errorinstanceofError){console.error('Error creating reservation:',error.message);}else{console.error('An unexpected error occurred.');}return;}//...
The code works as expected - it successfully handles table reservations with proper transaction isolation (finding an available table and creating the reservation atomically). However, I want to ensure this is the correct way to implement transactions with drizzle-orm and neon-serverless.
Specific questions:
Should I be using Pool/Client instead of the current setup with drizzle-orm/neon-serverless?
Is my current approach safe for concurrent reservations?
Are there any potential connection handling issues I should be aware of?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I read in the Neon docs that if I'm using transactions, I should use Pool or Client. And that Pool or Client objects must be connected, used and closed within a single request handler.
I'm using neon-serverless with drizzle-orm without using Pool or Client. Here's my current implementation:
Database configuration (db.ts)
Transaction code (reserve/page.tsx)
See the full code here: https://github.com/ruzaikr/table-booking/blob/main/app/reserve/page.tsx#L39
The code works as expected - it successfully handles table reservations with proper transaction isolation (finding an available table and creating the reservation atomically). However, I want to ensure this is the correct way to implement transactions with drizzle-orm and neon-serverless.
Specific questions:
drizzle-orm/neon-serverless
?Any guidance would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions