Registry / database / postgres-queryparser

postgres-queryparser

JSON →
library1.1.7jsnpmunverified

A lightweight Node.js utility for substituting PostgreSQL parameterized queries with actual values. It provides two methods: parseQuery (basic substitution) and generateQuery (with error validation). Version 1.1.7 is the latest stable release with low release cadence. Key differentiators: simple CJS-only package, handles arrays, nulls, and escape characters in values. Works with any PostgreSQL library (e.g., node-postgres) to generate final SQL strings for debugging or logging. Not an AST parser or SQL formatter; purely a string replacement tool for $N placeholders.

npm install postgres-queryparser
INSTALL
IMPORT
SIG · POSTGRES-QUERYPARS
P
postgres-queryparser
databasejavascriptv1.1.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.

default import
const pgParser = require('postgres-queryparser');
import pgParser from 'postgres-queryparser';
Package is CJS-only; does not support ESM imports. Must use require().
parseQuery
const { parseQuery } = require('postgres-queryparser');
import { parseQuery } from 'postgres-queryparser';
CommonJS destructured require works. ESM import will fail.
generateQuery
const result = require('postgres-queryparser').generateQuery(query, values);
require('postgres-queryparser').parseQuery(query, values);
generateQuery is a separate function added in v1.1.5 that includes error validation; parseQuery is older.

Shows how to import and use both parseQuery and generateQuery for PostgreSQL parameter substitution.

const pgParser = require('postgres-queryparser'); const query = 'SELECT * FROM users WHERE id = $1 AND status = $2'; const values = [42, 'active']; // Using parseQuery (basic) const parsed = pgParser.parseQuery(query, values); console.log(parsed); // Output: SELECT * FROM users WHERE id = '42' AND status = 'active' // Using generateQuery (with error validation, since v1.1.5) const generated = pgParser.generateQuery(query, values); console.log('Resolved Query:', generated); // Output: SELECT * FROM users WHERE id = '42' AND status = 'active'
Debug
Known issues
gotchaValues are not escaped for SQL injection; this is a debugging/logging tool, not for production SQL execution.
fix
Always use parameterized queries with your database driver (e.g., pg) for actual queries. Use postgres-queryparser only for logging or debugging.
affects: >=0.0.0
breakinggenerateQuery was introduced in v1.1.5; parseQuery from earlier versions does not return error details.
fix
Upgrade to >=1.1.5 if you need error validation.
affects: <1.1.5
gotchaArray values are not converted to PostgreSQL array literals; they are stringified as JavaScript arrays.
fix
If you pass an array as a value, it will be inserted as a JavaScript array representation (e.g., '[1,2,3]') not a proper SQL array format.
affects: >=0.0.0
deprecatedv1.1.1-1.1.4 may have issues with null values (fixed in v1.1.5).
fix
Upgrade to >=1.1.5.
affects: <1.1.5
Errors
Common errors & fixes
TypeError: pg_parser.parseQuery is not a function
Incorrect import: trying to use ESM import syntax on a CJS-only module.
fix
Use require('postgres-queryparser') instead of import.
Cannot find module 'postgres-queryparser'
Package not installed or installed in wrong directory.
fix
Run 'npm install postgres-queryparser' in your project root.
TypeError: Cannot read properties of undefined (reading 'parseQuery')
Require returns an object with methods; likely the module was not loaded correctly due to a missing default export.
fix
Ensure you are using const pgParser = require('postgres-queryparser'); and not const { default: pgParser } = require(...);
Upgrade
Version history
1.1.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources