Registry / database / sqltt
library1.0.0-32jsnpmunverified

A JavaScript library (v1.0.0-32, prerelease) for managing SQL queries using ES6 tagged template literals. It provides a template system to keep queries readable, maintainable, reusable, and organized, while allowing syntax adaptation across different database engines (PostgreSQL, MySQL, etc.). Unlike ORMs, it focuses solely on template management. It can be used as a Node.js module or as a CLI tool for generating SQL from templates. The library supports advanced features like parameterized arguments, conditional blocks, includes, comments, and multiple engine dialects. Its key differentiator is leveraging tagged templates for inline SQL composition without external template files, and the ability to publish templates for CLI execution.

npm install sqltt
INSTALL
IMPORT
SIG · SQLTT
S
sqltt
databasejavascriptv1.0.0-32
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.

sqltt
import sqltt from 'sqltt';
const sqltt = require('sqltt');
The package is ESM only by default. CommonJS require works but is not recommended in v1.0.0-32.
sqltt
const sqltt = require('sqltt');
For CommonJS projects, require is supported. The module exports the sqltt constructor directly.
sqltt (as constructor)
const tpl = new sqltt(`select ...`);
const tpl = sqltt(`select ...`);
sqltt is a class; must be used with 'new' for template instantiation.
template function (lambda)
const tpl = new sqltt($=>$`...`);
Using a lambda function enables advanced features like $.include, $.arg, $.REM, $.keys, $.values, $.entries.

Show how to define a sqltt template with an argument, generate SQL for PostgreSQL and MySQL, and publish for CLI use.

// Create a template file (e.g., queries.js) const sqltt = require('sqltt'); const tpl = {}; tpl.selectUser = new sqltt(` select id, name, email from users where id = ${'userId'} `); // Generate SQL for PostgreSQL (default) const sql1 = tpl.selectUser.sql({ userId: 42 }); console.log(sql1); // Output: select id, name, email from users where id = $1 // For MySQL const sql2 = tpl.selectUser.sql({ userId: 42 }, { engine: 'mysql' }); console.log(sql2); // Output: select id, name, email from users where id = ? // Execute from CLI (after publishing) sqltt.publish(module, tpl); // Then: sqltt queries.js selectUser userId=42
Debug
Known issues
gotchaTemplate strings with backticks must not contain unescaped dollar signs outside the ${} syntax; otherwise they will be interpreted as JavaScript template literals.
fix
Use double dollar sign $$ for literal dollar signs in SQL.
affects: *
gotchaThe lambda function parameter ('$') is not the same as the module default export; it is a special object provided by sqltt.
fix
Inside the function, use $ to call the template literal and to access helper methods (e.g., $.include).
affects: *
deprecatedThe static publish method signature changed in v1.0.0-32; previously accepted (module, templates) object with template names as keys; now expects (module, { name: template, ... }).
fix
Update to v1.0.0-32 and use: sqltt.publish(module, tpl) where tpl is an object with template names as keys.
affects: <=1.0.0-31
gotcha.sql() method returns the generated SQL string; it does NOT execute queries. Ensure you pass to a database driver manually.
fix
Use the SQL string with your database client (e.g., pool.query(sqlttRes.sql(params))).
affects: *
gotchaEngine-specific output may not handle all differences; the 'generic' engine is currently identical to PostgreSQL.
fix
Explicitly specify engine: 'postgresql', 'mysql', etc. when calling .sql() to get correct placeholders.
affects: *
Errors
Common errors & fixes
TypeError: sqltt is not a constructor
Using default import with 'import sqltt from 'sqltt'' in a CommonJS environment or forgetting 'new'.
fix
Use 'const sqltt = require('sqltt')' for CommonJS, or ensure ESM and use 'new sqltt(...)'.
sqltt.publish is not a function
Calling sqltt.publish before the sqltt instance is created, or importing the wrong thing.
fix
Ensure you import sqltt correctly: const sqltt = require('sqltt');
Unexpected token ')' - template literal not closed
Mismatched backticks or missing closing parenthesis in lambda syntax.
fix
Check your template string: new sqltt($=>$`...`) with closing backtick and parentheses.
Upgrade
Version history
1.0.0-32latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
Meta
1
Amazon
1
OpenAI (training)
1
Resources
packagesqltt
sqltt — npm install sqltt · libregistry