sql-template is a JavaScript library that uses ES6 tagged template literals to build SQL queries with parameterized placeholders, compatible with the pg (node-postgres) library. Version 1.3.2 is the latest stable release. It provides a simple, extensible tag-based system for constructing SQL strings and prepared statements automatically, escaping values to prevent SQL injection. Key features include custom transformers ($where$, $set$, $in$, etc.) for common SQL clauses, and static methods like SQL.insert, SQL.update, SQL.select for programmatic query building. Compared to other query builders, it emphasizes simplicity and direct mapping to SQL syntax while leveraging ES6 template strings for readability. The package is well-tested with 100% code coverage and is designed to be a drop-in replacement for raw SQL string formatting.
npm install sql-templateNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to use sql-template with pg to build a parameterized query from template strings.
Always use SQL`... ${value} ...` and never interpolate strings via `${rawString}`.Use $where${ { id: [1,2,3] } } for IN clause? Actually $where$ with array values generates IN automatically. See documentation.Consider alternatives like @databases/pg or sql-template-strings if active maintenance is needed.
Use $$ for literal question mark? Not needed if using tagged template properly.
Use default import: `import SQL from 'sql-template'` or `const SQL = require('sql-template')`.Run `npm install sql-template` or `yarn add sql-template`.
Use backticks: `SQL\`SELECT * FROM foo WHERE id = ${id}\``.