Registry / database / node-sql-template

node-sql-template

JSON →
library0.2.3jsnpmunverified

A simple SQL template engine and query runner for Node.js, version 0.2.3, low release cadence. It reads SQL files from a directory, renders them with escaped values, and executes queries using a connection pool via the mysql package. Key differentiators: simple API with forge/run, streaming results, and debug output. Alternatives like Squel or Knex are more feature-rich but heavier.

npm install node-sql-template
INSTALL
IMPORT
SIG · NODE-SQL-TEMPLATE
N
node-sql-template
databasejavascriptv0.2.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
const SQLTemplate = require('node-sql-template');
import SQLTemplate from 'node-sql-template';
Package is CommonJS only; no ESM exports. Use require.
forge
const template = SQLTemplate.forge(options);
forge is the factory method; returns a template instance.
render
template.render('example-1', [1]);
template.run('example-1', [1], callback);
render() returns the rendered SQL string; run() executes the query.

Shows template rendering and query execution with a callback.

const path = require('path'); const SQLTemplate = require('node-sql-template'); // Create a SQL file 'example.sql' with content: SELECT ? const options = { template: { dir: path.resolve(__dirname, './sql'), ext: 'sql' }, connection: { host: 'localhost', user: 'root', password: 'root' }, isDebug: true }; const template = SQLTemplate.forge(options); // Render only template.render('example', [1], (err, sql) => { console.log('SQL:', sql); }); // Execute query with callback template.run('example', [1], (err, rows) => { console.log('Rows:', rows); template.end(); });
Debug
Known issues
deprecatedThe mysql package is deprecated. Consider using mysql2.
fix
Install and use mysql2 instead of mysql; API is similar but more modern.
affects: >=0.2.0
breakingIn version 0.2.0, mysql became a peer dependency. You must install it separately.
fix
Run 'npm install mysql' alongside node-sql-template.
affects: >=0.2.0
gotchaThe password in options.connection is passed in plain text; ensure secure transport.
fix
Use environment variables and enforce TLS with ssl option.
affects: all
gotchaThe template dir must exist and contain .sql files; no default.
fix
Specify an existing directory with SQL files.
affects: all
Errors
Common errors & fixes
Cannot find module 'mysql'
Missing peer dependency mysql.
fix
Install mysql: npm install mysql
TypeError: template.render is not a function
Using import statement on CommonJS module.
fix
Use require instead of import.
Error: ER_NO_DB_ERROR: No database selected
Missing database in connection options.
fix
Add 'database' to options.connection, e.g., { database: 'mydb' }.
Upgrade
Version history
0.2.3latest on npm
Audit
Dependencies
mysqloptionalRequired peer dependency for query execution; must be installed separately.
Agent activity
5 hits · last 30 days
node
4
Resources
node-sql-template — npm install node-sql-template · libregistry