sql-tag
A template tag for writing elegant parameterized SQL queries based on ES2015 tagged template literals.
Compatible with pg, pg-native, mysql and mysql2. Read more about sequelize support.
Status
Installation
Install the package via npm
:
$ npm install --save sql-tag
Usage
Arguments
query
(string): The sql query.[...*]
(...*): The query replacements.
Returns
(Object): A structured object with the sql query string and its replacements.
Examples
const sql = ;const out = sql`SELECT * FROM biz WHERE id = `;// => { sql: 'SELECT * FROM biz WHERE id = ?', query: 'SELECT * FROM biz WHERE id = $1', values: ['foo'] }
const sql = ;const foo = 'bar';const out = sql`SELECT * FROM biz WHERE id = `;// => { sql: 'SELECT * FROM biz\n WHERE id = ?\n', query: 'SELECT * FROM biz\n WHERE id = $1\n', values: ['bar'] }
The tag itself is framework agnostic. It should just require a small modification to the query generator function.
NOTE: the sql
tag does not provide any kind of escaping safety. It delegates that work to the underlying framework.
pg/pg-native
Integration withThe output format is sql-tag
is directly compatible with pg
and pg-native
parameterized queries.
const pg = ;const client = ;const sql = ; client;
mysql/mysql2
Integration withconst mysql = ;const connection = mysql;const sql = ; connection;
sequelize
Integration withSequelize requires a special format to be able to handle parameterized queries. Check out the sequelize-sql-tag plugin which builds on top of sql-tag
to provide this functionality.
Tests
npm test
Release
npm version [<newversion> | major | minor | patch] -m "Release %s"
License
MIT