Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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 1 month ago
JSON representation

Node.js library for SQL queries templating

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 -- [email protected] -- [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