Registry / database / sql-params-format

sql-params-format

JSON →
library2.0.0jsnpmunverified

sql-params-format v2.0.0 extends pg-format with named parameter support for SQL query formatting. It allows using %I, %s, and %L placeholders with both positional and named arguments, and automatically converts Moment.js objects to Date objects. The library is stable with low release cadence. Differs from pg-format by supporting named parameters, while being a thin wrapper; ideal for dynamic query building in Node.js.

npm install sql-params-format
INSTALL
IMPORT
SIG · SQL-PARAMS-FORMAT
S
sql-params-format
databasejavascriptv2.0.0
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.

format
import format from 'sql-params-format';
const format = require('sql-params-format');
Default import is the named parameter formatter. The package is ESM-only as of v2? Actually package.json has no "type": "module", but the README shows CommonJS style. Assuming legacy CJS.
format
const { format } = require('sql-params-format');
const format = require('sql-params-format').default;
CommonJS usage; no named export for 'format'. Actually the module exports a single function, so require returns it directly.
TypeScript types
import format from 'sql-params-format';
Packages does not ship TypeScript types; may need @types/ manual.

Demonstrates named parameter formatting for SQL queries using %I, %s, %L placeholders with an object of values. Also shows fallback positional usage.

const format = require('sql-params-format'); // Named parameters const sql = 'SELECT * FROM %I:table WHERE name = %L:name AND age > %L:age'; const formatted = format(sql, { table: 'users', name: 'John', age: 30 }); console.log(formatted); // => SELECT * FROM "users" WHERE name = 'John' AND age > 30 // Positional usage (like pg-format) const sql2 = 'INSERT INTO %I (%s) VALUES (%L)'; const formatted2 = format(sql2, 'table', 'col1, col2', 'value'); console.log(formatted2); // => INSERT INTO "table" (col1, col2) VALUES 'value'
Debug
Known issues
gotchaNamed parameters are case-sensitive. Ensure case matches between placeholder and object keys.
fix
Use exact casing for parameter names in SQL string and object keys.
affects: >=1.0.0
gotchaUnused named parameters are silently ignored. This can hide typos in parameter names.
fix
Review SQL string for all placeholder names; consider linting or validation.
affects: >=1.0.0
gotchaMoment.js objects are automatically converted to Date. May cause unexpected behavior if you rely on moment being serialized differently.
fix
Pass plain Date objects or strings to avoid automatic conversion.
affects: >=1.0.0
deprecatedMoment.js conversion may be removed in future versions. Rely on Date objects.
fix
Use Date objects instead of Moment instances.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: format is not a function
Importing the default export incorrectly in ESM environment.
fix
Use `import format from 'sql-params-format'` (default) or `const format = require('sql-params-format');` in CJS.
SyntaxError: Named parameter not found: foo
Attempted to use a named parameter that isn't provided in the params object.
fix
Add the missing key to the params object: `format(sql, { foo: 'value' })`.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
pg-formatrequiredCore formatting engine; sql-params-format delegates to pg-format after resolving named parameters.
Agent activity
12 hits · last 30 days
node
10
Meta
1
OpenAI (training)
1
Resources
sql-params-format — npm install sql-params-format · libregistry