node-query-template
v0.0.1
Published
> Node.js library for SQL queries templating and parameters wrapping
Downloads
2
Readme
node-query-template
Node.js library for SQL queries templating and parameters wrapping
Installation
npm install node-query-template
Features
- Simple templating, e.g.
{{templateName}}
- Named parameters wrapping, e.g.
:namedParameter
- Parameterizing and templating can be simply extended by adding new strategy
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;