# @opdime/mysql-promises

> A promise wrapper around the mysql module.

Latest version **0.1.0** (published 2018-07-01) · Unlicense license · 0 weekly downloads

## Install

```sh
npm install @opdime/mysql-promises
pnpm add @opdime/mysql-promises
yarn add @opdime/mysql-promises
bun add @opdime/mysql-promises
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2018-07-01 |
| First published | 2018-07-01 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jörn Neumeyer |
| Maintainers | joernneumeyer |
| Keywords | Setup |

## Links

- npm: https://www.npmjs.com/package/@opdime/mysql-promises
- Repository: https://github.com/OPDIME/ts-setup
- Homepage: https://github.com/OPDIME/mysql-promises-js#readme
- Issues: https://github.com/OPDIME/mysql-promises-js/issues
- npm.io page: https://npm.io/package/@opdime/mysql-promises

## Dependencies (1)

- [mysql](https://npm.io/package/mysql.md) ^2.15.0

## Recent versions

- 0.1.0 (latest) — 2018-07-01

## README

# MySQL-Promises-JS
---
MySQL-Promises-JS is a simple wrapper arount the __mysql__ package, which earases the callbacks from the API and replaces them with `Promise`s.

```javascript
const { MysqlPromises } = require('@opdime/mysql-promises');
// or
import { MysqlPromises } from '.';

const connection = MysqlPromises.createConnection({
  database: 'example',
  host: 'localhost',
  password: 'root',
  user: 'root'
});

connection
  .connect()
  .then(x => x.query('SELECT * FROM tbl_user;'))
  .then(({ result }) => console.log(result))
  .then(() => connection.end())
  .catch(e => console.error(e));
```

#### `static MysqlPromises.createConnection(config = undefined)`
Creates a new `MysqlPomises` instance. The configuration is the same as usually used with the __mysql__ package.

#### `async MysqlPromises.beginTransaction()`
Starts a new transaction.

#### `async MysqlPromises.commit()`
Commits the current transaction.

#### `MysqlPromises.config()`
Exposes the underlying configuration of the `Connection` instance.

#### `async MysqlPromises.connect()`
Initializes the connection to the configured endpoint. This method returns a `Promise` which contains the connected `MysqlPromises` instance.

#### `MysqlPromises.destroy()`
Closes the current `Connection` instance immediatly, terminating all pending tasks.

#### `async MysqlPromises.end()`
Closes the current `Connection` instance soft. So, all pending tasks will be executed.

#### `MysqlPromises.getRawConnection()`
Exposes the Exposes the underlying `Connection` instance.

#### `async MysqlPromises.query(query, params = [])`
Executes the provided query. The result is going to be an object of the type `QueryResult` which has the following TypeScript definition:
```typescript
export type QueryResult<T> = Promise<{ fields?: Array<FieldInfo>; result: T }>;
```
As shown in the code above, the result may be of any type. In case you use TypeScript, you can also specify the type of the result. So, a query call in TypeScript could look like this:
```typescript

interface IUser {
  username: string;
  dataOfBirth: Date;
}

connection
  .connect()
  .then(x => x.query<Array<IUser>>('SELECT * FROM tbl_user;'))
  .then(({ result }) => console.log(result))
  .then(() => connection.end())
  .catch(e => console.error(e));
```

#### `async MysqlPromises.rollback()`
Performs a rollback on the current transaction.

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