Registry / database / dbh-pg

dbh-pg

JSON →
library3.0.0jsnpmunverified

Lightweight Database Handler for PostgreSQL built on top of node-postgres (pg) and bluebird promises. Provides a thin wrapper around pg's connection pool with promise-based methods for common database operations, transactions, and query shorthands. Current version 3.0.0. The library simplifies common tasks like transactions and connection lifecycle management using bluebird's Promise.using pattern. Compared to raw pg, it reduces boilerplate for queries and transactions but adds a dependency on bluebird. Suitable for projects already using bluebird or wanting a minimal promise-based interface over pg.

npm install dbh-pg
INSTALL
IMPORT
SIG · DBH-PG
D
dbh-pg
databasejavascriptv3.0.0
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.

DBH
import DBH from 'dbh-pg'
const { DBH } = require('dbh-pg')
Default export, not named. ESM/CJS both work due to dual package.
DBH
const DBH = require('dbh-pg')
const dbh = require('dbh-pg').default
CJS: use require directly; no .default needed.
Promise (bluebird)
const Promise = require('bluebird')
const Promise = global.Promise
DBH expects bluebird Promise; native Promise may work but not guaranteed.

Shows basic connection usage with fetchOne and a transaction using begin/commit.

const DBH = require('dbh-pg'); const Promise = require('bluebird'); const using = Promise.using; const db = new DBH('postgres://user:password@localhost/mydb'); using(db.conn(), function(conn) { return conn.fetchOne('SELECT * FROM users WHERE id = $1', [1]) .then(user => { console.log(user); }); }); // Transaction example: using(db.conn(), function(conn) { return conn .begin() .then(function() { return this.exec('UPDATE accounts SET balance = balance - 100 WHERE id = $1', [1]); }) .then(function() { return this.exec('UPDATE accounts SET balance = balance + 100 WHERE id = $1', [2]); }) .then(function() { return this.commit(); }); });
Debug
Known issues
gotchaRequires bluebird for Promise.using; native Promise.using does not exist.
fix
Install bluebird: npm install bluebird and require it.
affects: >=1.0.0
gotchaconn.commit() must be called explicitly; otherwise auto-rollback on connection release.
fix
Always call .commit() after successful transaction, or handle rollback explicitly.
affects: >=1.0.0
breakingVersion 2.x dropped support for callbacks; promises only.
fix
Use bluebird promises or async/await with bluebird's Promise.promisifyAll.
affects: >=2.0.0
deprecatedStatic methods DBH.exec, DBH.commit etc. are less idiomatic; prefer instance methods.
fix
Use conn.exec and conn.commit directly.
affects: >=3.0.0
Errors
Common errors & fixes
Cannot find module 'dbh-pg'
Package not installed or incorrect import path.
fix
Run `npm install dbh-pg` and ensure node_modules contains it.
Promise.using is not a function
Using native Promise instead of bluebird.
fix
Install bluebird and use `const Promise = require('bluebird');` before using `Promise.using`.
Error: Connection terminated unexpectedly
Database server not running or connection string incorrect.
fix
Verify PostgreSQL is running and connection string is correct (postgres://user:pass@host/db).
TypeError: db.conn(...).then is not a function
Trying to call .then on a disposer object; must use Promise.using.
fix
Wrap db.conn() inside `Promise.using(db.conn(), function(conn) { ... });`.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
bluebirdoptionalTransaction handling and Promise.using require bluebird; strongly recommended
pgrequiredCore PostgreSQL driver; required for database access
Agent activity
7 hits · last 30 days
node
6
Resources
packagedbh-pg
dbh-pg — npm install dbh-pg · libregistry