# neat-sql-template

> a neat way to write SQL templates in Node.js

Latest version **2.0.3** (published 2022-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install neat-sql-template
pnpm add neat-sql-template
yarn add neat-sql-template
bun add neat-sql-template
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2022-01-06 |
| First published | 2021-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 22.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Rasmus von Syberg |
| Maintainers | bricksharetech |

## Links

- npm: https://www.npmjs.com/package/neat-sql-template
- Repository: https://github.com/Brickshare/neat-sql-template
- Homepage: https://github.com/Brickshare/neat-sql-template#readme
- Issues: https://github.com/Brickshare/neat-sql-template/issues
- npm.io page: https://npm.io/package/neat-sql-template

## Dependencies (4)

- [winston](https://npm.io/package/winston.md) 3.3.3
- [sqlstring](https://npm.io/package/sqlstring.md) 2.3.2
- [lodash.zip](https://npm.io/package/lodash.zip.md) 4.2.0
- [winston-gke-formatter](https://npm.io/package/winston-gke-formatter.md) 0.0.2

## Recent versions

- 2.0.3 (latest) — 2022-01-06
- 2.0.2 — 2021-10-13
- 2.0.1 — 2021-08-18
- 2.0.0 — 2021-08-08
- 1.0.10 — 2021-08-03
- 1.0.9 — 2021-07-08
- 1.0.8 — 2021-04-29
- 1.0.7 — 2021-03-16
- 1.0.6 — 2021-03-11
- 1.0.5 — 2021-03-11
- 1.0.3 — 2021-01-29
- 1.0.2 — 2021-01-28
- 1.0.1 — 2021-01-28
- 1.0.0 — 2021-01-28

## README

# neat-mysql

A library to generate SQL templates in a neat way. Works well with either of:
g

- [neat-mysql](https://github.com/Brickshare/neat-mysql#readme)
- [mysql2](https://github.com/sidorares/node-mysql2#readme)

## Installation

```
npm i neat-sql-template
```

`neat-sql-template` has typescript typings out of the box.

## Usage

### Prepared statement using tag

```
import { SQL } from 'neat-sql-template';

const statement = SQL`
  UPDATE people SET name = ${'John'} WHERE id = ${1}
`;
// { sql: 'UPDATE people SET name = ? WHERE id = ?', values: ['John', 1] }

const concatenatedStatement = SQL`
  UPDATE people SET name = (${SQL`SELECT name from people WHERE id = ${2}`}) WHERE id = ${1}
`;
/*
  {
    sql: 'UPDATE people SET name = (SELECT name from people WHERE id = ?) WHERE id = ?;',
    values: [2, 1]
  }
*/

{
      statement: '\n' +
        'UPDATE people SET name = SELECT name from people WHERE id = ? WHERE id = ?\n',
      arguments: [ '2', '1' ]
    }

```

### Prepared statement using javascript objects

```
import { sqlTemplate } from 'neat-sql-template';

const people = [
  { name: 'John', email: 'john@mail.com' },
  { name: 'Tim', email: 'tim@mail.com' },
  { name: 'Benjamin', email: 'ben@mail.com' },
]

const insertStatement = sqlTemplate.insert(people[0], 'people');
/*
  {
    sql: 'INSERT INTO people (name, email) VALUES (?, ?);',
    values: ['John', 'john@mail.com']
  }
*/

const insertMultipleStatement = sqlTemplate.insertMultiple(people, 'people');
/*
  {
    sql: 'INSERT INTO people (name, email) VALUES (?, ?), (?, ?), (?, ?);',
    values: ['John', 'john@mail.com', 'Tim', 'tim@mail.com', 'Benjamin', 'ben@mail.com']
  }
*/

const updateStatement = sqlTemplate.update({ ...people[0], id: 1 }, 'people');
/*
  {
    sql: 'UPDATE people SET name = ?, email = ? WHERE id = ?',
    values: ['John', 'john@mail.com', 1]
  }
*/

const findStatement = sqlTemplate.find({ id: 1 }, 'people');
/*
  {
    sql: 'SELECT * FROM people WHERE id = ?',
    values: [1]
  }
*/

const removeStatement = sqlTemplate.remove({ id: 1 }, 'people');
/*
  {
    sql: 'DELETE FROM people WHERE id = ?',
    values: [1]
  }
*/

```

### Prepared statement using mongodb-like API

```
const findStatement = find<Type>({ id: { $ne: 123 } }, 'table');
/*
  {
    sql: 'SELECT * FROM table WHERE id != ?',
    values: [123]
  }
*/

const findStatement = find<Type>({ id: { $ne: null } }, 'table');
/*
  {
    sql: 'SELECT * FROM table WHERE id IS NOT NULL',
    values: []
  }
*/

const deleteStatement = remove<Type>({ created_at: { $lt: new Date() } }, 'table');
/*
  {
    sql: 'DELETE FROM table WHERE created_at < ?',
    values: [Date(...)]
  }
*/

const findStatement = find<Type>({ name: { $unlike: 'name' } }, 'table');
/*
  {
    sql: 'SELECT * FROM table WHERE name NOT LIKE ?',
    values: ['name']
  }
*/

// explicit AND
const findStatement = find<Type>({
  $and: [{ id: 123 }, { name: 'name' }],
}, 'table');

/*
  {
    sql: 'SELECT * FROM table WHERE id = ? AND name = ?',
    values: [123, 'name']
  }
*/

// implicit AND
const findStatement = find<Type>({
  name: { $unlike: 'name' },
  id: { $ne: null }
}, 'table');

/*
  {
    sql: 'SELECT * FROM table WHERE name NOT LIKE ? AND id IS NOT NULL',
    values: ['name']
  }
*/

const findStatement = find<Type>({
  $or: [{ id: 123 }, { name: 'name' }],
}, 'table');

/*
  {
    sql: 'SELECT * FROM table WHERE id = ? OR name = ?',
    values: [123, 'name']
  }
*/

```

---
_Source: https://npm.io/package/neat-sql-template · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
