Registry / database / pg-lazy

pg-lazy

JSON →
library3.0.2jsnpmunverified

Simple functional helper layer on top of node-postgres (pg) providing convenient methods like `one()`, `many()`, `none()`, `withTransaction()`, and `canConnect()` on both Pool and Client instances. Version 3.0.2 requires Node >= 10 and peer dependency pg ^8.0.0. The library extends pg.Pool and pg.Client with ES6 classes (no longer mutating originals, stores originals as pg._Pool and pg._Client). Supports tagged template literal `sql` for safe parameterized queries. Offers both manual pool initialization and singleton mode. Release cadence appears low; no recent updates.

npm install pg-lazy
INSTALL
IMPORT
SIG · PG-LAZY
P
pg-lazy
databasejavascriptv3.0.2
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.

pgLazy
✓ import pgLazy from 'pg-lazy'
✗ const pgLazy = require('pg-lazy').default
Default export via ESM; CJS uses require('pg-lazy') directly.
Pool
✓ const { Pool } = pgLazy(require('pg'), { connectionString })
Pool is not exported at top-level; it's destructured from the object returned by pgLazy().
sql
✓ import pgLazy from 'pg-lazy'; const { sql } = pgLazy(pg, config);
✗ import { sql } from 'pg-lazy'
sql tag is only available from the return object of the default export.

Initializes a pg-lazy Pool with a connection string, then uses `one()` to fetch a single row.

import pg from 'pg'; import pgLazy from 'pg-lazy'; const config = { connectionString: process.env.DATABASE_URL ?? 'postgres://localhost:5432/mydb' }; const { Pool, sql } = pgLazy(pg, config); const pool = new Pool(); async function getUser(id: number) { const user = await pool.one(sql`SELECT * FROM users WHERE id = ${id}`); return user; } getUser(1).then(console.log).catch(console.error);
Debug
Known issues
breakingPool and Client are no longer instances of pg.Pool and pg.Client in v3 – they are custom exports.
fix
Use the returned Pool/Client from pgLazy() instead of pg.Pool directly.
affects: >=3.0.0
breakingisConnected() renamed to canConnect() in v3.
fix
Replace pool.isConnected() or client.isConnected() with pool.canConnect() or client.canConnect().
affects: >=3.0.0
breakingv2+ no longer mutates pg.Pool and pg.Client; stores originals as pg._Pool and pg._Client.
fix
Update code to use the returned Pool/Client instead of pg.Pool/pg.Client.
affects: >=2.0.0 <3.0.0
gotchaSingleton pool initialization requires passing a third argument {singleton:true}.
fix
Either pass {singleton:true} or manually create a new Pool() instance.
affects: >=2.0.0
deprecatedNode 8 support dropped in v2; v3 requires Node >= 10.
fix
Ensure Node >= 10.
affects: >=3.0.0
Errors
Common errors & fixes
Cannot find module 'pg-lazy'
Missing install of pg-lazy or its peer dependency pg.
fix
npm install pg-lazy pg
TypeError: pool.one is not a function
Using a regular pg.Pool instead of the returned Pool from pgLazy().
fix
Use const { Pool } = pgLazy(pg, config); const pool = new Pool();
Error: parameter 'text' must be a string
Calling pool.query(sql`...`) without the sql tag, or using plain string in pool.one().
fix
Always use sql`...` for parameterized queries (e.g., pool.one(sql`SELECT * FROM t WHERE id = ${id}`)).
Upgrade
Version history
3.0.2latest on npm
Audit
Dependencies
pgrequiredpeer dependency – requires node-postgres ^8.0.0
Agent activity
8 hits · last 30 days
node
6
Resources
pg-lazy — npm install pg-lazy · libregistry