{"id":43592,"library":"pg-transaction","title":"pg-transaction","description":"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.","status":"maintenance","version":"1.0.4","language":"javascript","source_language":"en","source_url":"git://github.com/goodybag/node-pg-transaction","tags":["javascript","postgres","postgresql","postgre","node-postgres","transactions","transaction"],"install":[{"cmd":"npm install pg-transaction","lang":"bash","label":"npm"},{"cmd":"yarn add pg-transaction","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg-transaction","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency; requires node-postgres to be installed","package":"pg","optional":false}],"imports":[{"note":"CJS-only; ESM import not supported.","wrong":"import Transaction from 'pg-transaction';","symbol":"Transaction","correct":"const Transaction = require('pg-transaction');"},{"note":"Export is a single constructor, not a named export.","wrong":"const { Transaction } = require('pg-transaction');","symbol":"default import","correct":"const Transaction = require('pg-transaction');"},{"note":"Requires an instance of pg.Client, not the module itself.","wrong":"const tx = new Transaction(pg);","symbol":"Client usage","correct":"const client = new pg.Client(connectionString); const tx = new Transaction(client);"}],"quickstart":{"code":"const pg = require('pg');\nconst Transaction = require('pg-transaction');\n\nconst connectionString = process.env.PG_CON ?? '';\nconst client = new pg.Client(connectionString);\nclient.connect((err) => {\n  if (err) throw err;\n  client.query('CREATE TEMP TABLE test(id serial, name varchar(20))');\n\n  const tx = new Transaction(client);\n  tx.on('error', (err) => { console.error('TX error:', err); });\n  tx.begin();\n  tx.query(\"INSERT INTO test(name) VALUES($1)\", ['Alice']);\n  tx.savepoint('sp1');\n  tx.query(\"INSERT INTO test(name) VALUES($1)\", ['Bob']);\n  tx.rollback('sp1');\n  tx.release('sp1');\n  tx.commit((err) => {\n    if (err) console.error('Commit error:', err);\n    client.query('SELECT COUNT(*) AS count FROM test', (err, result) => {\n      if (err) throw err;\n      console.log('Count:', result.rows[0].count); // 1\n      client.end();\n    });\n  });\n});","lang":"javascript","description":"Creates a PostgreSQL client, wraps it in a Transaction, executes a transaction with savepoint and rollback."},"warnings":[{"fix":"Use either callbacks entirely or events entirely, not both.","message":"If a callback is provided to begin/commit/rollback, error events are not emitted. This is by design to avoid double-handling.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Consider alternatives like pg-promise or knex transactions for modern applications.","message":"The package is unmaintained and uses older patterns (no TypeScript, no Promises).","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Ensure client.connect() callback has succeeded before creating a Transaction.","message":"Transaction must be used with an already-connected pg.Client. Calling methods on a disconnected client will throw.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Always rollback before release if you intend to undo changes at a savepoint.","message":"savepoint and release must be called in correct order. Releasing a savepoint before rollback or vice versa may cause errors.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'2013':52 '2014':57 'abstract':69 'begin':33 'begin/commit/rollback':72 'callback':43 'commit':34 'consid':60 'differenti':65 'either':42 'emitt':46 'error':48 'event':45 'exclus':75 'handl':49 'javascript':80 'key':64 'last':54 'librari':12,40 'manag':17 'node':22,78,85 'node-postgr':21,77,84 'node.js':11 'object':31 'pg':2,5,24 'pg-transact':1,4 'pg.client':27 'postgr':23,79,81,83,86 'postgresql':15,82 'provid':66 'raw':71 'releas':38,50 'rollback':35 'savepoint':36 'simpl':68 'simplifi':14 'small':10 'sql':73 'stabl':61 'support':32 'top':19 'transact':3,6,16,30,87,88 'unmaintain':63 'updat':55 'use':41 'v1.0.4':7 'work':74 'wrap':26","created_at":"2026-06-05T17:00:32.282302+00:00","updated_at":"2026-06-05T17:00:32.282302+00:00","problems":[{"fix":"Change to const Transaction = require('pg-transaction');","cause":"Using ESM import style (import Transaction from 'pg-transaction') instead of require.","error":"TypeError: Transaction is not a constructor"},{"fix":"Ensure tx.savepoint('sp1') is called before release or rollback, and don't release twice.","cause":"Trying to release or rollback to a savepoint that was never created or already released.","error":"Error: savepoint \"sp1\" does not exist"},{"fix":"Always call tx.commit() or tx.rollback() before starting a new transaction on the same tx object.","cause":"Calling tx.begin() while a transaction is already active (e.g., not after commit/rollback).","error":"Error: Cannot call begin() inside a transaction"}],"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/goodybag/node-pg-transaction","github":"git://github.com/goodybag/node-pg-transaction","docs":null,"changelog":null,"pypi":null,"npm":"pg-transaction","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}}