query-template
v1.1.0
Published
> Node.js library for SQL queries templating and parameters wrapping
Downloads
11
Readme
node-query-template
Node.js library for SQL queries templating and parameters wrapping
Installation
npm install query-template
Features
- Simple templating, e.g.
{{templateName}}
- Named parameters wrapping, e.g.
:namedParameter
- Parameterizing and templating can be simply extended by adding new parameterizer
- Params can be wrapped for Postgres (
$1
) and MySQL (?
) format
Dependencies
- No dependencies
Usage example
Simple query definition example
const getUsersByScore = {
sql: `
SELECT id
FROM users
WHERE score > :score
{{balance}};
`,
addons: {
balance: {
options: {propertyName: 'needBalance', propertyValue: true},
sql: 'AND balance >= :minBalance',
},
},
};
Simple query building example
const QueryTemplater = require('query-template');
const qt = new QueryTemplater();
const sqlWithBalance = qt.processTemplates(getUsersByScore, {needBalance: true});
// => SELECT id FROM users WHERE score > :score AND balance >= :minBalance;
const sqlWithoutBalance = qt.processTemplates(getUsersByScore, {});
// => SELECT id FROM users WHERE score > :score;
Parametrizing example
const builtSQL = qt.parametrizeQuery(sqlWithBalance, {score: 10, minBalance: 150})
Result:
{
query: 'SELECT id FROM users WHERE score > $1 AND balance >= $2',
params: [10,150]
}
Author
Pavel Romanov -- [email protected] -- GitHub
License
Distributed under MIT License. See LICENSE
for more information;