Registry / database / pg-format-fix

pg-format-fix

JSON →
library1.0.5jsnpmunverified

Node.js implementation of PostgreSQL's format() to safely create dynamic SQL queries. This version 1.0.5 is a minor fix to the archived original pg-format (last version 1.0.4) which contained a bug when the global absolute path is rewritten. It supports escaped SQL identifiers (%I), literals (%L), and simple strings (%s), as well as argument positioning, buffers, arrays, and objects. Useful for building SQL queries safely against SQL injection. Note: the original pg-format is archived and this fork fixes a specific path rewriting bug. Release cadence is low (last update 2022). Differentiator: lightweight, no dependencies, mimics PostgreSQL's format function exactly.

npm install pg-format-fix
INSTALL
IMPORT
SIG · PG-FORMAT-FIX
P
pg-format-fix
databasejavascriptv1.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.

default import
import format from 'pg-format'
ESM default import (requires node >= 12 or bundler). CommonJS users should use require as shown in README.
CommonJS require
const format = require('pg-format')
const { format } = require('pg-format')
Library exports a single function as default; named destructuring will return undefined.
named functions
import format from 'pg-format'; format.ident('col'); format.literal('val'); format.string('str'); format.withArray('fmt', []); format.config({});
import { ident, literal } from 'pg-format'
Functions like ident, literal, string, withArray, config are attached to the default exported function, not named exports.

Demonstrates basic formatting, array handling, argument positioning, Node.js Buffers, and bulk inserts with nested arrays.

import format from 'pg-format'; // Basic usage const sql1 = format('SELECT * FROM %I WHERE id = %L', 'users', 1); console.log(sql1); // SELECT * FROM users WHERE id = '1' // With array const ids = [1, 2, 3]; const sql2 = format('SELECT * FROM users WHERE id IN (%L)', ids); console.log(sql2); // SELECT * FROM users WHERE id IN ('1','2','3') // With argument position const sql3 = format('UPDATE %1$I SET name = %2$L WHERE id = %3$L', 'users', 'Alice', 42); console.log(sql3); // UPDATE users SET name = 'Alice' WHERE id = '42' // WithBuffer (Node.js) const buf = Buffer.from([0x62, 0x65, 0x65, 0x66]); const sql4 = format('INSERT INTO files (data) VALUES (%L)', buf); console.log(sql4); // INSERT INTO files (data) VALUES E'\\x62656566' // WithArray for bulk insert const rows = [['Alice', 30], ['Bob', 25]]; const sql5 = format('INSERT INTO users (name, age) VALUES %L', rows); console.log(sql5); // INSERT INTO users (name, age) VALUES ('Alice', '30'), ('Bob', '25')
Debug
Known issues
deprecatedOriginal pg-format package (v1.0.4) is archived and no longer maintained.
fix
Use pg-format-fix (v1.0.5) or another actively maintained alternative.
affects: <1.0.5
gotchaThe package is a fork with a bug fix; future compatibility with pg-format is not guaranteed. Check for updates or breakages.
fix
Monitor the fork's repository for any changes; consider pinning the version.
affects: >=1.0.5
breakingUndefined or null passed to %I will throw an error.
fix
Ensure identifiers are always defined strings; use %L or %s for null handling.
affects: >=1.0.0
gotchaObject literals are JSON.stringify'd and escaped; unexpected side effects for complex objects or circular references.
fix
Pass only plain objects or use %s for custom serialization.
affects: >=1.0.0
gotchaNested arrays in %L produce grouped lists like ('a','b'),('c','d'); not suitable for all query contexts.
fix
For single-row inserts, pass a flat array; for multiple rows, use nested arrays as shown in quickstart.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: format is not a function
ESM import or destructured require syntax incorrectly used.
fix
Use default import: import format from 'pg-format' in ESM; or const format = require('pg-format') in CommonJS.
Cannot read properties of undefined (reading 'ident')
Named import used: import { ident } from 'pg-format'.
fix
Access ident as a property of the default import: format.ident.
Error: pg-format: unexpected %
A literal % character outside %% sequence used in format string.
fix
Escape literal % as %% in format string, e.g. '100%% complete'.
Upgrade
Version history
1.0.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
pg-format-fix — npm install pg-format-fix · libregistry