{"id":43961,"library":"sql-template-strings","title":"SQL Template Strings","description":"sql-template-strings (v2.2.2) is a mature, stable library that enables ES6 tagged template literals for building prepared SQL statements. It supports mysql, mysql2, pg (PostgreSQL), and Sequelize, automatically generating the correct placeholder syntax ($1, ?) and extracting values into arrays. Unlike raw template strings or manual concatenation, it prevents SQL injection by separating query text and values. Ships TypeScript definitions. Minimal API surface (SQL tag and append method). Last updated 2020; no active development but widely used and bug-free for its scope.","status":"maintenance","version":"2.2.2","language":"javascript","source_language":"en","source_url":"https://github.com/felixfbecker/node-sql-template-strings","tags":["javascript","mysql","mysql2","postgres","pg","prepared","statements","placeholder","es6","typescript"],"install":[{"cmd":"npm install sql-template-strings","lang":"bash","label":"npm"},{"cmd":"yarn add sql-template-strings","lang":"bash","label":"yarn"},{"cmd":"pnpm add sql-template-strings","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Module is ESM with default export. Named import will fail.","wrong":"import { SQL } from 'sql-template-strings'","symbol":"default (SQL)","correct":"import SQL from 'sql-template-strings'"},{"note":"CommonJS uses default export, not named destructuring.","wrong":"const { SQL } = require('sql-template-strings')","symbol":"SQL (CommonJS)","correct":"const SQL = require('sql-template-strings')"},{"note":"Statement is a named export, not default. For TypeScript type annotations.","wrong":"import Statement from 'sql-template-strings'","symbol":"Statement class","correct":"import { Statement } from 'sql-template-strings'"}],"quickstart":{"code":"import SQL from 'sql-template-strings';\nimport { createConnection } from 'mysql2/promise';\n\nasync function main() {\n  const conn = await createConnection({ host: 'localhost', user: 'root', database: 'test' });\n  const name = 'harry potter';\n  const author = 'J. K. Rowling';\n  const query = SQL`SELECT author FROM books WHERE name = ${name} AND author = ${author}`;\n  const [rows] = await conn.execute(query.sql, query.values);\n  console.log(rows);\n  await conn.end();\n}\nmain().catch(console.error);","lang":"typescript","description":"Shows how to use the SQL tagged template to create a prepared statement and execute it with mysql2 using async/await."},"warnings":[{"fix":"Keep all dynamic values as direct template interpolations, not inside quoted substrings.","message":"Do not use `${value}` inside string literals inside the template – the tag only escapes top-level interpolations.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use query.sql or query.text for the query string and query.values for the parameters, or use compatible drivers (mysql, mysql2, pg) that accept the object directly.","message":"The SQL tag returns an object, not a string. Passing it directly to db.query(..., callback) may not work in all drivers.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use the append() method to build queries dynamically instead of concatenating strings.","message":"Do not mix SQL tagged templates with string concatenation inside the same template – it breaks escaping.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use query.sql for MySQL-style (?) placeholders and query.text for PostgreSQL-style ($1) placeholders.","message":"The .query property is deprecated in favor of .sql and .text for clarity.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"search_vec":"'1':39 '2020':75 'activ':77 'api':66 'append':71 'array':44 'automat':33 'bug':84 'bug-fre':83 'build':21 'concaten':51 'correct':36 'definit':64 'develop':78 'enabl':15 'es6':16,97 'extract':41 'free':85 'generat':34 'inject':55 'javascript':89 'last':73 'librari':13 'liter':19 'manual':50 'matur':11 'method':72 'minim':65 'mysql':27,90 'mysql2':28,91 'pg':29,93 'placehold':37,96 'postgr':92 'postgresql':30 'prepar':22,94 'prevent':53 'queri':58 'raw':46 'scope':88 'separ':57 'sequel':32 'ship':62 'sql':1,5,23,54,68 'sql-template-str':4 'stabl':12 'statement':24,95 'string':3,7,48 'support':26 'surfac':67 'syntax':38 'tag':17,69 'templat':2,6,18,47 'text':59 'typescript':63,98 'unlik':45 'updat':74 'use':81 'v2.2.2':8 'valu':42,61 'wide':80","created_at":"2026-06-05T17:02:19.071276+00:00","updated_at":"2026-06-05T17:02:19.071276+00:00","problems":[{"fix":"Use the backtick syntax: SQL`...`","cause":"Using SQL tag as a regular function instead of a tagged template: SQL(...)","error":"Cannot read properties of undefined (reading 'sql')"},{"fix":"Chain .append() correctly: query.append(SQL`...`)","cause":"Forgetting to call .append() on a Statement object before using it as a query.","error":"TypeError: (intermediate value) is not iterable"},{"fix":"Use default import: import SQL from 'sql-template-strings' or require: const SQL = require('sql-template-strings')","cause":"Importing incorrectly (named instead of default) or using in an environment without ES module support.","error":"ReferenceError: SQL is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/felixfbecker/node-sql-template-strings#readme","github":"https://github.com/felixfbecker/node-sql-template-strings","docs":null,"changelog":null,"pypi":null,"npm":"sql-template-strings","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}