Registry / database / pg-sql-helpers

pg-sql-helpers

JSON →
library0.3.7jsnpmunverified

A dynamic SQL query helper library (v0.3.7) built on top of pg-sql, providing a lodash-like toolkit for constructing INSERT, UPDATE, WHERE, ORDER BY, and other SQL clauses programmatically without string concatenation. Released by Ian Storm Taylor, with low release cadence (last update 2016). Key differentiator: allows SQL-native template syntax via pg-sql while adding helpers for dynamic clauses, unlike ORMs or full query builders.

npm install pg-sql-helpers
INSTALL
IMPORT
SIG · PG-SQL-HELPERS
P
pg-sql-helpers
databasejavascriptv0.3.7
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.

INSERT
import { INSERT } from 'pg-sql-helpers'
import INSERT from 'pg-sql-helpers'
Named export; cannot be default imported.
WHERE
import { WHERE } from 'pg-sql-helpers'
const WHERE = require('pg-sql-helpers').WHERE
Correct for CJS, but preferred ESM syntax shown.
ORDER_BY
import { ORDER_BY } from 'pg-sql-helpers'
import { order_by } from 'pg-sql-helpers'
Exported as uppercase but also lowercase alias exists; recommended uppercase.
sql
import { sql } from 'pg-sql'
import { sql } from 'pg-sql-helpers'
sql template tag is from pg-sql, not pg-sql-helpers.
AND
import { AND } from 'pg-sql-helpers'
Named export; used after a hardcoded WHERE clause.

Shows dynamic WHERE and ORDER BY using pg-sql-helpers with pg-sql template tag and pg pool.

import { sql } from 'pg-sql'; import { INSERT, WHERE, ORDER_BY } from 'pg-sql-helpers'; import { Pool } from 'pg'; const pool = new Pool(); async function run() { const name = 'john'; const ageFilter = { gt: 42 }; const sort = ['-age', 'name']; const query = sql` SELECT id, name, age FROM users ${WHERE({ name, age: ageFilter })} ${ORDER_BY(sort)} `; const result = await pool.query(query); console.log(result.rows); } run().catch(err => console.error(err));
Debug
Known issues
gotchapg-sql must be installed alongside pg-sql-helpers; it is a peer dependency and not included automatically.
fix
Run: npm install pg-sql
affects: >=0.0.0
deprecatedLibrary is low maintenance; last release 2016, no updates for security or PostgreSQL version compatibility.
fix
Consider alternatives like Slonik or Kysely for active development.
affects: >=0.0.0
gotchaINSERT with array of objects uses keys from first object; subsequent objects missing keys may cause SQL errors.
fix
Ensure all objects in array have identical keys.
affects: >=0.0.0
gotchaORDER_BY accepts strings like '-column'; prefix '-' means descending. Ensure prefix is hyphen, not other minus signs.
fix
Use hyphen directly; check whitespace (e.g., '- column' will not work).
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'text')
pg-sql not installed or not imported correctly; query object missing sql template tag result.
fix
Install pg-sql: npm install pg-sql, and import { sql } from 'pg-sql'
Error: password authentication failed for user
Missing or incorrect database credentials in pg Pool configuration.
fix
Provide valid connection string or environment variables: DATABASE_URL
SyntaxError: Unexpected identifier at column 1
Using helpers outside of sql template tag; e.g., const clause = INSERT('users', {...}) used in string concatenation.
fix
Wrap helper calls inside sql`...` template literals only.
ReferenceError: INSERT is not defined
Forgot to import helper from pg-sql-helpers.
fix
Add import: import { INSERT } from 'pg-sql-helpers'
Upgrade
Version history
0.3.7latest on npm
Audit
Dependencies
pg-sqlrequiredpeer dependency; required for sql template tag and value escaping
Agent activity
5 hits · last 30 days
node
4
Resources
pg-sql-helpers — npm install pg-sql-helpers · libregistry