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);
Errors
Common errors & fixes
TypeError: client.fetchOne is not a function
patch(pg) was not called before creating the client.
fixCall 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.
fixEnsure 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.
fixEnsure 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.
fixRun npm install pg-spice or use --legacy-peer-deps if using npm v7+.
Audit
Dependencies
pgrequiredpeer dependency – library patches pg.Client prototype
pg-spicerequiredinternal dependency for bulk insertion and SQL injection prevention