Registry / database / pg-essential

pg-essential

JSON →
library1.0.2jsnpmunverified

Lightweight Node.js library on top of pg-spice that patches pg.Client with helper methods: fetchOne, fetchAll, execute, executeBulkInsertion, and executeTransaction. Version 1.0.2 is stable with no recent updates. It does not manage connection pooling, leaving that to the developer. Differentiators: bulk insertion and transaction handling without SQL injection via pg-promise internals.

npm install pg-essential
INSTALL
IMPORT
SIG · PG-ESSENTIAL
P
pg-essential
databasejavascriptv1.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.

patch
const { patch } = require('pg-essential'); patch(pg);
import { patch } from 'pg-essential';
pg-essential is CJS-only. ESM import requires dynamic import().
pg.Client.prototype.fetchOne
const client = new pg.Client(); // then patch first const row = await client.fetchOne('SELECT * FROM users WHERE id=$1', [1]);
const row = await pg.fetchOne('SELECT * FROM users WHERE id=$1', [1]);
fetchOne is a prototype method on client instance, not a static export.
executeBulkInsertion
await client.executeBulkInsertion([{id:1,name:'a'}], ['id','name'], 'users');
await client.executeBulkInsertion('users', [{id:1,name:'a'}], ['id','name']);
Order of arguments: bulkData, columns, tableName.

Demonstrates patching pg, connecting a client, and using fetchOne, fetchAll, execute, executeBulkInsertion, and executeTransaction.

const pg = require('pg'); require('pg-essential').patch(pg); async function main() { const client = new pg.Client({ connectionString: process.env.DATABASE_URL ?? 'postgres://localhost/mydb' }); await client.connect(); // fetchOne const user = await client.fetchOne('SELECT * FROM users WHERE id = $1', [1]); console.log('User:', user); // fetchAll const allUsers = await client.fetchAll('SELECT * FROM users'); console.log('All users:', allUsers); // execute const result = await client.execute('UPDATE users SET name = $1 WHERE id = $2', ['John', 1]); console.log('Updated rows:', result.rowCount); // executeBulkInsertion const bulkData = [ { id: 2, name: 'Alice' }, { id: 3, name: 'Bob' } ]; await client.executeBulkInsertion(bulkData, ['id', 'name'], 'users'); // executeTransaction await client.executeTransaction(async (txClient) => { await txClient.execute('INSERT INTO logs (msg) VALUES ($1)', ['start']); await txClient.execute('UPDATE users SET name = $1 WHERE id = $2', ['Updated', 2]); }); await client.end(); } main().catch(console.error);
Debug
Known issues
gotchaMust call patch(pg) before creating any clients; otherwise prototype methods are undefined.
fix
Call require('pg-essential').patch(pg) at app startup before any new pg.Client().
affects: >=1.0.0
gotchaexecuteBulkInsertion uses pg-promise internally, but its options are not configurable. Large bulk inserts may exceed PostgreSQL parameter limit.
fix
Split bulk data into chunks of ~500 rows to avoid parameter limit errors.
affects: >=1.0.0
gotchaexecuteTransaction does not expose the connection from pool; it expects you to call p.getClient() yourself as shown in examples.
fix
Wrap executeTransaction inside getPool().then(...) as shown in the README examples.
affects: >=1.0.0
deprecatedpg-spice on which this depends is no longer actively maintained. Future compatibility with newer pg versions is uncertain.
fix
Consider using pg-promise or knex directly for bulk operations and transactions.
affects: >=1.0.0
gotchafetchOne returns null if no rows found, not undefined or throws. Check for null to handle empty results.
fix
Always check const row = await client.fetchOne(...); if (row === null) { ... }
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: client.fetchOne is not a function
patch(pg) was not called before creating the client.
fix
Call require('pg-essential').patch(pg) before any new pg.Client() instantiation.
error: column "id" of relation "test" does not exist
executeBulkInsertion column names array contains a column that does not exist in the table.
fix
Ensure columns array matches actual table column names, including case sensitivity.
Error: Can't set headers after they are sent.
Calling done() multiple times or handling errors incorrectly in connection callback.
fix
Ensure connection.done() is called exactly once per getClient block, and catch errors to avoid double call.
Cannot find module 'pg-spice'
pg-essential depends on pg-spice which may not be installed automatically if peer dependencies are not resolved.
fix
Run npm install pg-spice or use --legacy-peer-deps if using npm v7+.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
pgrequiredpeer dependency – library patches pg.Client prototype
pg-spicerequiredinternal dependency for bulk insertion and SQL injection prevention
Agent activity
4 hits · last 30 days
node
4
Resources
pg-essential — npm install pg-essential · libregistry