Registry / database / sql-string-qb

sql-string-qb

JSON →
library1.0.3jsnpmunverified

A minimal, dependency-free JavaScript/TypeScript library for assembling complex SQL queries using template literals. Version 1.0.3, MIT licensed. It returns an object compatible with popular database clients (pg, mysql, mysql2, sequelize, oracledb) by providing multiple query string representations (?, $1, :1 placeholders). Inspired by classNames, it uses tagged template functions (qb.t) for safe value interpolation. Ships TypeScript types, supports Deno, and has zero dependencies. Experimental helper functions (qb.set, qb.values, qb.in) for SET, VALUES, and IN clauses.

npm install sql-string-qb
INSTALL
IMPORT
SIG · SQL-STRING-QB
S
sql-string-qb
databasejavascriptv1.0.3
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.

default
import qb from 'sql-string-qb'
const qb = require('sql-string-qb')
Library is ESM-only; CommonJS require() will not work.
QbResult (implicit)
import qb from 'sql-string-qb'; const result: { sql: string; text: string; values: any[] } = qb(...)
TypeScript types are included; the result object has `.sql`, `.text`, `.query`, `.statement`, `.values`, `.toString()`.
qb.t
qb.t`column = ${value}`
qb.t('column = ' + value)
qb.t is a tagged template function; do NOT call it as a regular function with strings.
qb.unescaped
qb.unescaped('value')
For bypassing parameterization; use with caution.

Builds a parameterized SQL query using tagged template literal for safe value interpolation, returns an object with multiple placeholder formats.

import qb from 'sql-string-qb'; const category = 'sporting-goods'; const sortColumn = undefined; const sortOrder = undefined; const query = qb( 'SELECT id, name', 'FROM products WHERE', qb.t`category = ${category}`, 'ORDER BY', sortColumn || 'createdAt', sortOrder || 'DESC' ); console.log(query.sql); // SELECT id, name FROM products WHERE category = ? ORDER BY createdAt DESC console.log(query.values); // ['sporting-goods']
Debug
Known issues
gotchaVariables outside a qb.t function will go directly into the query and should be escaped first.
fix
Always wrap interpolated values in qb.t`...` to use parameterized queries.
affects: >=1.0.0
gotchaqb.unescaped bypasses parameterization; can lead to SQL injection if used with untrusted input.
fix
Avoid qb.unescaped with user-supplied values; manually escape or use only for trusted data.
affects: >=1.0.0
gotchaqb.set, qb.values, qb.in are experimental and may change in future versions.
fix
Pin version if using experimental helpers; check for updates on breaking changes.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: qb is not a function
Importing with CommonJS require() or incorrect default import.
fix
Use ESM: import qb from 'sql-string-qb'
Cannot find module 'sql-string-qb'
Package not installed or wrong import path.
fix
Run: npm install sql-string-qb
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
Meta
1
OpenAI (training)
1
Resources
sql-string-qb — npm install sql-string-qb · libregistry