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.
require('node-pg-helper') (default export as object)
✓ const db = require('node-pg-helper');
✗ import db from 'node-pg-helper';
This package is CommonJS-only (no ESM exports). Using ES import syntax will fail.
setClient
✓ db.setClient(pool);
✗ setClient(pool);
setClient is a method on the default export object, not a named export.
insert
✓ await db.insert('users', { id: 100, firstname: 'john' });
✗ await db.insert('users', { id: 100, firstname: 'john' }, {});
insert only accepts table name and data object; no third argument. Extra arguments are ignored.
upsert
✓ await db.upsert('users', { id: 100, firstname: 'john' }, 'id');
✗ await db.upsert('users', { id: 100, firstname: 'john' }, ['id']);
The third parameter must be a string (single conflict column), not an array.
update
✓ await db.update('users', { lastname: 'snow' }, { id: 100 });
✗ await db.update('users', { lastname: 'snow' }, { id: 100 }, { returning: true });
update does not support options like 'returning' – it only returns undefined.
Initializes a pg Pool, sets client with setClient(), performs insert, selectAllRows, upsert, and update operations.
const { Pool } = require('pg');
const db = require('node-pg-helper');
const pool = new Pool({
connectionString: process.env.DATABASE_URL ?? '',
ssl: { rejectUnauthorized: false }
});
db.setClient(pool);
async function run() {
await db.insert('users', { id: 1, firstname: 'Alice', lastname: 'Smith' });
const rows = await db.selectAllRows('users');
console.log(rows);
await db.upsert('users', { id: 1, firstname: 'Alice', lastname: 'Jones' }, 'id');
await db.update('users', { lastname: 'Johnson' }, { id: 1 });
await db.selectAllRows('users').then(r => console.log(r));
}
run().catch(console.error);
Errors
Common errors & fixes
TypeError: db.setClient is not a function
Importing incorrectly (e.g., using ES import syntax or destructuring).
fixUse const db = require('node-pg-helper'); Cannot read property 'query' of undefined
setClient() not called before performing a database operation.
fixCall db.setClient(pool) after creating the pool.
Error: insert requires an object
Passing a non-object as data (e.g., array or string) to insert/upsert/update.
fixEnsure the data argument is a plain object with key-value pairs.
Audit
Dependencies
pgrequiredRequired peer dependency; node-pg-helper wraps pg's Pool for SQL queries.