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.
npm install pg-txNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic usage of pg-tx with a Pool transaction transferring funds between accounts.
Always use db inside the callback and never keep a reference after the transaction completes.
For nested transactions, just call tx(db, ...) inside the callback — it will create a savepoint.
Treat db as a PoolClient; it implements the same query, release, etc. methods.
Replace 'import { tx } from 'pg-tx'' with 'import tx from 'pg-tx''Use db.query(...) or db.connect() etc. db is an object, not a function.
Move all queries inside the callback. Do not pass db to Promises that outlive the callback.
No dependency data recorded yet.