# neat-mysql

> a neat way to interact with MySQL from Node.js

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

## Install

```sh
npm install neat-mysql
pnpm add neat-mysql
yarn add neat-mysql
bun add neat-mysql
```

## 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.2 |
| Published | 2022-01-06 |
| First published | 2021-01-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 31.2 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Rasmus von Syberg |
| Maintainers | bricksharetech |

## Links

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

## Dependencies (6)

- [ssh2](https://npm.io/package/ssh2.md) ^1.5.0
- [config](https://npm.io/package/config.md) ^3.3.3
- [mysql2](https://npm.io/package/mysql2.md) ^2.2.5
- [winston](https://npm.io/package/winston.md) ^3.3.3
- [sqlstring](https://npm.io/package/sqlstring.md) ^2.3.2
- [winston-gke-formatter](https://npm.io/package/winston-gke-formatter.md) 0.0.2

## Recent versions

- 2.0.2 (latest) — 2022-01-06
- 2.0.1 — 2021-09-08
- 2.0.0 — 2021-09-08
- 1.0.24 — 2021-08-03
- 1.0.23 — 2021-08-03
- 1.0.22 — 2021-03-25
- 1.0.21 — 2021-03-15
- 1.0.20 — 2021-03-09
- 1.0.19 — 2021-02-16
- 1.0.18 — 2021-02-16
- 1.0.17 — 2021-02-12
- 1.0.16 — 2021-02-11
- 1.0.15 — 2021-02-11
- 1.0.14 — 2021-02-11
- 1.0.13 — 2021-02-11
- … 13 more at https://npm.io/package/neat-mysql/versions

## README

# neat-mysql

A library to query and manipulate MySQL in a neat way.

## Installation

### Add package

```
npm i neat-mysql
```

`neat-mysql` has typescript typings out of the box.

### Environment

The following env variables can be used to modify global behaviour:

```
NEAT_MYSQL_LOG_LEVEL // (default=INFO) controls logging output
NEAT_MYSQL_NULL_TO_UNDEFINED // (default=false) returns 'undefined' instead of 'null' from queries
```

## Usage

```
const dbConfig = {
  host: "localhost",
  port: 3306,
  database: "database",
  user: "root",
  password: "root"
};
const connector = connectToDatabase(dbConfig);
```

### Query

```
import { query } from 'neat-mysql';

interface Person {
  id: number;
  name: string;
}

...

async function findPerson(id: number): Promise<Person | undefined> {
  const person = await queryOne<Person>([`
    SELECT id, name FROM people WHERE id = ?
  `, [id]], await connector)
  return person;
}

async function findPeople(): Promise<Person[]> {
  const people = await query<Person>(`SELECT id, name FROM people`, await connector)
  return people;
}

```

### Execute

All executions return `ResultSetHeader` from `mysql2`, which is implemented by `neat-mysql`.

```
import { execute } from 'neat-mysql';

...

async function deletePerson(id: number) {
  const result = await execute([`
    DELETE FROM people WHERE id = ?
  `, [id]], await connector)
  return result;
}

```

### Transaction

```
import { transaction, Connection } from 'neat-mysql';

...

async function doLotsOfStuff(): Promise<Person[]> {
  return transaction((connection) => {
    await connection.execute([`
      DELETE FROM people WHERE id = ?
    `, [id]])

    const people = await connection.query<Person>([`SELECT id, name FROM people`])
    return people;
  }, await connector)
}

```

You can concatenate the connection of `transaction` with regular queries and executions by passing it as the second parameter:

```
// this version is equivalent to the above one
async function doLotsOfStuff(): Promise<Person[]> {
  return transaction((connection) => {
    await execute([`
      DELETE FROM people WHERE id = ?
    `, [id]], connection)

    const people = await query<Person>([`SELECT id, name FROM people`], connection)
    return people;
  }, await connector)
}

...

// this execution can be done on its own or inside a transaction if a connection is passed
async function deletePerson(id: number, connection?: Connection) {
  const result = await execute([`
    DELETE FROM people WHERE id = ?
  `, [id]], connection)
  return result;
}

```

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