Registry / database / pg-transaction

pg-transaction

JSON →
library1.0.4jsnpmunverified

pg-transaction v1.0.4 is a small Node.js library that simplifies PostgreSQL transaction management on top of node-postgres (pg). It wraps pg.Client with a Transaction object supporting begin, commit, rollback, savepoints, and release. The library uses either callbacks or event emitters for error handling. Released in 2013 and last updated in 2014, it is considered stable but unmaintained. Key differentiator: provides a simple abstraction over raw BEGIN/COMMIT/ROLLBACK SQL, working exclusively with node-postgres.

npm install pg-transaction
INSTALL
IMPORT
SIG · PG-TRANSACTION
P
pg-transaction
databasejavascriptv1.0.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Transaction
const Transaction = require('pg-transaction');
import Transaction from 'pg-transaction';
CJS-only; ESM import not supported.
default import
const Transaction = require('pg-transaction');
const { Transaction } = require('pg-transaction');
Export is a single constructor, not a named export.
Client usage
const client = new pg.Client(connectionString); const tx = new Transaction(client);
const tx = new Transaction(pg);
Requires an instance of pg.Client, not the module itself.

Creates a PostgreSQL client, wraps it in a Transaction, executes a transaction with savepoint and rollback.

const pg = require('pg'); const Transaction = require('pg-transaction'); const connectionString = process.env.PG_CON ?? ''; const client = new pg.Client(connectionString); client.connect((err) => { if (err) throw err; client.query('CREATE TEMP TABLE test(id serial, name varchar(20))'); const tx = new Transaction(client); tx.on('error', (err) => { console.error('TX error:', err); }); tx.begin(); tx.query("INSERT INTO test(name) VALUES($1)", ['Alice']); tx.savepoint('sp1'); tx.query("INSERT INTO test(name) VALUES($1)", ['Bob']); tx.rollback('sp1'); tx.release('sp1'); tx.commit((err) => { if (err) console.error('Commit error:', err); client.query('SELECT COUNT(*) AS count FROM test', (err, result) => { if (err) throw err; console.log('Count:', result.rows[0].count); // 1 client.end(); }); }); });
Debug
Known issues
gotchaIf a callback is provided to begin/commit/rollback, error events are not emitted. This is by design to avoid double-handling.
fix
Use either callbacks entirely or events entirely, not both.
affects: >=0.0.0
deprecatedThe package is unmaintained and uses older patterns (no TypeScript, no Promises).
fix
Consider alternatives like pg-promise or knex transactions for modern applications.
affects: >=1.0.0
gotchaTransaction must be used with an already-connected pg.Client. Calling methods on a disconnected client will throw.
fix
Ensure client.connect() callback has succeeded before creating a Transaction.
affects: >=0.0.0
gotchasavepoint and release must be called in correct order. Releasing a savepoint before rollback or vice versa may cause errors.
fix
Always rollback before release if you intend to undo changes at a savepoint.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Transaction is not a constructor
Using ESM import style (import Transaction from 'pg-transaction') instead of require.
fix
Change to const Transaction = require('pg-transaction');
Error: savepoint "sp1" does not exist
Trying to release or rollback to a savepoint that was never created or already released.
fix
Ensure tx.savepoint('sp1') is called before release or rollback, and don't release twice.
Error: Cannot call begin() inside a transaction
Calling tx.begin() while a transaction is already active (e.g., not after commit/rollback).
fix
Always call tx.commit() or tx.rollback() before starting a new transaction on the same tx object.
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies
pgrequiredpeer dependency; requires node-postgres to be installed
Agent activity
2 hits · last 30 days
node
2
Resources
pg-transaction — npm install pg-transaction · libregistry