Registry / database / pg-query-formatter

pg-query-formatter

JSON →
library0.0.5jsnpmunverified

Lightweight query string formatter for PostgreSQL in Node.js (v0.0.5, last updated 2014). Provides a printf-like syntax for safely building SQL queries with escaping for identifiers, literals, and subqueries. Supports parameterized queries via toParam() for use with node-postgres. Currently in maintenance mode; no updates since 2014. Alternative libraries like pg-promise, squel, or knex may be more actively maintained.

npm install pg-query-formatter
INSTALL
IMPORT
SIG · PG-QUERY-FORMATTER
P
pg-query-formatter
databasejavascriptv0.0.5
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.

Query
const Query = require('pg-query-formatter');
import Query from 'pg-query-formatter';
CommonJS only; no ESM support. Use require().
new Query(fmt, ...values)
const sql = new Query('SELECT %L', 'hello');
const sql = Query('SELECT %L', 'hello');
Query must be instantiated with new.
Query.List
const list = new Query.List(' AND ');
const list = Query.List(' AND ');
Query.List is a constructor; must use new.

Demonstrates basic construction, parameterized output, array expansion, and Query.List for dynamic SQL building.

const Query = require('pg-query-formatter'); // Basic select with identifier and literal const sql = new Query('SELECT * FROM %I WHERE name = %L', 'teachers', 'George'); console.log(sql.toString()); // Output: SELECT * FROM teachers WHERE name = 'George' // Parameterized query for safe database execution const param = sql.toParam(); console.log(param.text); // SELECT * FROM teachers WHERE name = $1 console.log(param.values); // [ 'George' ] // Array expansion for IN clause const sql2 = new Query('SELECT * FROM teachers WHERE name IN (%L)', ['George', 'Jorge', 'Georgio']); console.log(sql2.toString()); // Output: SELECT * FROM teachers WHERE name IN ('George', 'Jorge', 'Georgio') // Using Query.List for dynamic AND clauses const where = new Query.List(' AND '); where.append('age > %L', 20); where.append('age < %L', 30); const sql3 = new Query('SELECT %I FROM teachers WHERE %Q', ['id', 'name'], where); console.log(sql3.toString()); // Output: SELECT id, name FROM teachers WHERE age > 20 AND age < 30
Debug
Known issues
deprecatedPackage is no longer maintained; last update 2014. Use pg-promise, squel, or knex instead.
fix
Migrate to a maintained library like pg-promise (https://github.com/vitaly-t/pg-promise) or knex (https://knexjs.org).
affects: >=0.0.0
gotchaThe constructor requires the 'new' keyword; calling Query(...) without new will return undefined.
fix
Always use new Query(fmt, ...values).
affects: >=0.0.0
gotchaUntrusted input passed to %s (unescaped string) can lead to SQL injection. Only use %s for trusted values.
fix
Use %L for literals and %I for identifiers to safely escape input. Avoid %s with user-supplied data.
affects: >=0.0.0
gotchaQuery.List requires call to .toString() or toParam(); simply logging the object will not show the query.
fix
Always call .toString() or .toParam() to get the formatted output.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Query is not a constructor
Importing with import statement or forgetting 'new'
fix
Use const Query = require('pg-query-formatter'); and then new Query(...);
Cannot read property 'toString' of undefined
Calling .toString() on a Query instance that was not instantiated with new.
fix
Ensure you use new Query(fmt, ...).
TypeError: fmt.replace is not a function
First argument to new Query() is not a string.
fix
Pass a string format as first argument: new Query('SELECT %L', value);
Upgrade
Version history
0.0.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
pg-query-formatter — npm install pg-query-formatter · libregistry