Registry / database / sql-template

sql-template

JSON →
library1.3.2jsnpmunverified

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-template
INSTALL
IMPORT
SIG · SQL-TEMPLATE
S
sql-template
databasejavascriptv1.3.2
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.

SQL
import SQL from 'sql-template'
const { SQL } = require('sql-template')
Default export; named import does not exist. Use default import or require('sql-template') as shown in README.
SQL
const SQL = require('sql-template')
const sqlTemplate = require('sql-template').default
CommonJS require returns the default export directly.
SQL type (TypeScript)
import type SQL from 'sql-template'
import { SQL } from 'sql-template'
TypeScript users should use default type import or rely on type inference; the package does not ship types, but community types may exist.

Shows how to use sql-template with pg to build a parameterized query from template strings.

const SQL = require('sql-template'); const { Pool } = require('pg'); const pool = new Pool(); async function run() { const name = 'John'; const age = 30; const query = SQL`SELECT * FROM users WHERE name = ${name} AND age = ${age}`; // query = { text: 'SELECT * FROM users WHERE name = $1 AND age = $2', values: ['John', 30] } const result = await pool.query(query); console.log(result.rows); } run().catch(console.error);
Debug
Known issues
gotchaTemplate literal expressions are automatically converted to parameterized placeholders. Ensure you do NOT manually concatenate strings to avoid SQL injection.
fix
Always use SQL`... ${value} ...` and never interpolate strings via `${rawString}`.
affects: >=1.0.0
gotchaThe $where$ and other transformers treat objects as key-value pairs with AND logic. Using arrays for IN clauses works only with $in$ transformer.
fix
Use $where${ { id: [1,2,3] } } for IN clause? Actually $where$ with array values generates IN automatically. See documentation.
affects: >=1.0.0
deprecatedThe package has not seen updates since 2021; check GitHub for issues/PRs. No breaking changes known but community may have forked.
fix
Consider alternatives like @databases/pg or sql-template-strings if active maintenance is needed.
affects: >=1.3.2
gotchaTransformers use `?:` internally as parameter placeholder for JSONB compliance. Avoid using `?:` in raw SQL strings.
fix
Use $$ for literal question mark? Not needed if using tagged template properly.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: SQL is not a function
Import using named import incorrectly.
fix
Use default import: `import SQL from 'sql-template'` or `const SQL = require('sql-template')`.
Cannot find module 'sql-template'
Package not installed.
fix
Run `npm install sql-template` or `yarn add sql-template`.
SyntaxError: Unexpected template string
Using SQL tag without backticks or in non-template context.
fix
Use backticks: `SQL\`SELECT * FROM foo WHERE id = ${id}\``.
Upgrade
Version history
1.3.2latest on npm
Audit
Dependencies
pgoptionalCommonly used together for PostgreSQL queries; sql-template returns objects compatible with pg's query method.
Agent activity
12 hits · last 30 days
node
10
Meta
1
OpenAI (training)
1
Resources
sql-template — npm install sql-template · libregistry