https://github.com/a1k0r/node-query-template
Node.js library for SQL queries templating
https://github.com/a1k0r/node-query-template
mysql nodejs postgresql query sql template
Last synced: about 2 months ago
JSON representation
Node.js library for SQL queries templating
- Host: GitHub
- URL: https://github.com/a1k0r/node-query-template
- Owner: a1k0r
- License: mit
- Created: 2018-02-19T12:58:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-14T11:47:10.000Z (over 7 years ago)
- Last Synced: 2025-10-22T04:22:04.988Z (8 months ago)
- Topics: mysql, nodejs, postgresql, query, sql, template
- Language: JavaScript
- Size: 132 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# node-query-template
> Node.js library for SQL queries templating and parameters wrapping
[![NPM Version][npm-image]][npm-url]
## Installation
```sh
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
```js
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
```js
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
```js
const builtSQL = qt.parametrizeQuery(sqlWithBalance, {score: 10, minBalance: 150})
```
Result:
```js
{
query: 'SELECT id FROM users WHERE score > $1 AND balance >= $2',
params: [10,150]
}
```
## Author
Pavel Romanov -- alkor@alkor.pw -- [GitHub](https://github.com/Shikyaro)
## License
Distributed under MIT License. See [`LICENSE`](./LICENSE) for more information;
[npm-image]: https://img.shields.io/npm/v/query-template.svg?style=flat-square
[npm-url]: https://npmjs.org/package/query-template