{"id":43598,"library":"pg-tx","title":"pg-tx","description":"Transaction wrapper for node-postgres that prevents use-after-release bugs by wrapping the client in a Proxy. Current version 1.0.1, released in 2021. Unlike naive implementations based on Stack Overflow answers, pg-tx throws an error if you attempt to use the client after the transaction ends. Supports automatic savepoints for nested transactions and works with both Pool and PoolClient. Ships TypeScript types. Lightweight with zero dependencies.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/golergka/pg-tx","tags":["javascript","pg","postgres","connection","transaction","savepoint","database","db","tx","typescript"],"install":[{"cmd":"npm install pg-tx","lang":"bash","label":"npm"},{"cmd":"yarn add pg-tx","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg-tx","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export only. Named import will be undefined.","wrong":"const { tx } = require('pg-tx')","symbol":"default export (tx)","correct":"import tx from 'pg-tx'"},{"note":"Default export is the tx function. Named import is wrong.","wrong":"import { tx } from 'pg-tx'","symbol":"tx function","correct":"import tx from 'pg-tx'"},{"note":"Works with Pool or PoolClient. The callback receives a proxied client.","wrong":"import * as tx from 'pg-tx'","symbol":"TypeScript usage with Pool","correct":"import tx from 'pg-tx';\nimport { Pool } from 'pg';\n\nconst pool = new Pool();\nawait tx(pool, async (db) => { ... })"}],"quickstart":{"code":"import tx from 'pg-tx';\nimport { Pool } from 'pg';\n\nconst pool = new Pool({\n  connectionString: process.env.POSTGRES_URL ?? ''\n});\n\nasync function transferFunds(from: string, to: string, amount: number) {\n  await tx(pool, async (db) => {\n    await db.query(\n      'UPDATE accounts SET balance = balance - $1 WHERE name = $2',\n      [amount, from]\n    );\n    await db.query(\n      'UPDATE accounts SET balance = balance + $1 WHERE name = $2',\n      [amount, to]\n    );\n  });\n}\n\ntransferFunds('Alice', 'Bob', 100).catch(console.error);","lang":"typescript","description":"Shows basic usage of pg-tx with a Pool transaction transferring funds between accounts."},"warnings":[{"fix":"Always use db inside the callback and never keep a reference after the transaction completes.","message":"The callback client (db) is a Proxy that throws on use after the transaction ends. Do NOT store the db reference for later use.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For nested transactions, just call tx(db, ...) inside the callback — it will create a savepoint.","message":"Nested transactions automatically use savepoints. You cannot use BEGIN/COMMIT/ROLLBACK inside a transaction callback; use nested tx() calls instead.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Treat db as a PoolClient; it implements the same query, release, etc. methods.","message":"The db parameter is NOT a PoolClient but a ProxyClient. It shares the same interface but has additional guards. Do not rely on instanceof checks.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.1':26 '2021':29 'answer':37 'attempt':46 'automat':56 'base':33 'bug':16 'client':20,50 'connect':78 'current':24 'databas':81 'db':82 'depend':74 'end':54 'error':43 'implement':32 'javascript':75 'lightweight':71 'naiv':31 'nest':59 'node':8 'node-postgr':7 'overflow':36 'pg':2,39,76 'pg-tx':1,38 'pool':65 'poolclient':67 'postgr':9,77 'prevent':11 'proxi':23 'releas':15,27 'savepoint':57,80 'ship':68 'stack':35 'support':55 'throw':41 'transact':4,53,60,79 'tx':3,40,83 'type':70 'typescript':69,84 'unlik':30 'use':13,48 'use-after-releas':12 'version':25 'work':62 'wrap':18 'wrapper':5 'zero':73","created_at":"2026-06-05T17:00:33.900963+00:00","updated_at":"2026-06-05T17:00:33.900963+00:00","problems":[{"fix":"Replace 'import { tx } from 'pg-tx'' with 'import tx from 'pg-tx''","cause":"Importing named export instead of default","error":"TypeError: Cannot read properties of undefined (reading 'query')"},{"fix":"Use db.query(...) or db.connect() etc. db is an object, not a function.","cause":"Attempting to call db as a function instead of using db.query","error":"TypeError: db is not a function"},{"fix":"Move all queries inside the callback. Do not pass db to Promises that outlive the callback.","cause":"Attempting to use the db object after the transaction callback has returned","error":"Error: Client has already been released"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/golergka/pg-tx#readme","github":"https://github.com/golergka/pg-tx","docs":null,"changelog":null,"pypi":null,"npm":"pg-tx","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}