Functions

Database transaction client

Use one checked-out client for the whole transaction, release on all paths; reconcile ambiguous commit outcomes. Source: https://node-postgres.com/features/transactions

Formal Syntax & Structure

nextjs syntaxnextjs
pool.connect(); client.query(...); client.release()

Working Example

Example Snippetnextjs
// partial server-only driver helper; reviewed pg requiredimport type {Pool} from 'pg';export async function setTitle(pool:Pool,id:string,owner:string,title:string) { const client=await pool.connect(); let broken=false; try {  await client.query('BEGIN');  const r=await client.query('UPDATE books SET title=$1 WHERE id=$2 AND owner_id=$3',[title,id,owner]);  if(r.rowCount!==1) throw new Error('Unavailable');  await client.query('COMMIT'); } catch(e) {try {await client.query('ROLLBACK');} catch {broken=true;} throw e;} finally {client.release(broken);}}
Next.js Reference