pg-binder
v1.0.3
Published
Bind JavaScript objects to safe PostgreSQL queries – turn your plain objects into fully parameterized SQL with zero hassle.
Downloads
31
Maintainers
Readme
PostgreSQL Query Binder
✨ Bind objects to safe PostgreSQL queries – turn your plain objects into fully parameterized SQL with zero hassle.
Features
- 🔒 Safely converts JavaScript/TypeScript objects into parameterized PostgreSQL queries.
- 🧩 Use simple placeholders like
$(key)inside your SQL strings. - 📝 100% TypeScript-first with generics and type inference.
- ⚡ Instant error feedback if a placeholder is missing in the
valuesobject. - 🎯 Returns
[sqlWithPlaceholders, valuesArray]ready forpg.query(). - ✅ Strongly typed – TypeScript validates your object against SQL placeholders.
Installation
npm install pg-binderor with Yarn:
yarn add pg-binderUsage Examples
import { Pool } from 'pg';
import pgSqlBind from 'pg-binder';
const pool = new Pool({ /* your Postgres config */ });
const query = `
INSERT INTO products (name, price, stock)
VALUES ($(name), $(price), $(stock))
RETURNING *
`;
const result = await pool.query(...pgSqlBind(query, {
name: 'Laptop',
price: 999.99,
stock: 50
}));
console.log(result.rows[0]);License
MIT © Avijit
