# @ikoala/node-mysql-promise

> Node.js MySQL library promise wrapper

Latest version **2.0.2** (published 2025-01-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install @ikoala/node-mysql-promise
pnpm add @ikoala/node-mysql-promise
yarn add @ikoala/node-mysql-promise
bun add @ikoala/node-mysql-promise
```

## Health

**Score 45/100 (D)** — status: stable.

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

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2025-01-07 |
| First published | 2018-06-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 3 |
| Unpacked size | 3.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | iKoala |
| Maintainers | ikoala |

## Links

- npm: https://www.npmjs.com/package/@ikoala/node-mysql-promise
- Repository: https://github.com/iKoala/node-mysql-promise
- Homepage: https://github.com/iKoala/node-mysql-promise#readme
- Issues: https://github.com/iKoala/node-mysql-promise/issues
- npm.io page: https://npm.io/package/@ikoala/node-mysql-promise

## Dependencies (3)

- [dotenv](https://npm.io/package/dotenv.md) ^16.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [mysql2](https://npm.io/package/mysql2.md) ^3.6.2

## Recent versions

- 2.0.2 (latest) — 2025-01-07
- 2.0.1 — 2025-01-07
- 1.4.12 — 2023-05-10
- 1.4.11 — 2023-05-10
- 1.4.10 — 2022-06-28
- 1.4.8 — 2022-06-27
- 1.4.6 — 2022-06-27
- 1.3.8 — 2021-06-15
- 1.3.7 — 2021-06-10
- 1.3.6 — 2021-06-10
- 1.3.5 — 2021-05-10
- 1.3.4 — 2020-02-26
- 1.3.3 — 2020-02-26
- 1.3.2 — 2020-02-26
- 1.3.1 — 2020-02-25
- … 12 more at https://npm.io/package/@ikoala/node-mysql-promise/versions

## README

### node-mysql-promise
Node.js MySQL library promise wrapper

### Initialization
> npm install @ikoala/node-mysql-promise

### Usage
```javascript
const db = require('@ikoala/node-mysql-promise')
const mysqlConfig = {
  "host": "127.0.0.1",
  "user": "root",
  "password": "abc@123",
  "database": "test"
}
db.create('master', mysqlConfig)

const query = async (val) => {
  const sql = 'SELECT NOW(), ?, 1'
  const params = [val]
  const rs = await db.query(sql, params)
  console.log(rs) // 2020-02-01 15:56:00,foo,1
}

query('foo')
```

### Helper Functions
```javascript
const select = helper.createSelect('table_name', 'primary_key_field');
(async () => {
  await select({ all: true }) // Select all records
  await select(1) // Select primary key id 1
  // Select field `foo` with value `bar`
  await select({ where: {
    foo: 'bar'
  }})
})()

const insert = helper.createInsert('table_name', 'primary_key_field', {
  defaultFields: {
    ctime: () => new Date()
  }
});
(async () => {
  await insert({
    field1: 'foo',
    field2: 'bar'
  }) // foo,bar,[ctime date]
})()
```

# Testing

```bash
# Setup DB Connection and Login Credential
export DB_HOST=127.0.0.1
export DB_USER=root
export DB_PASSWORD=abc@123
export DB_VERBOSE=1 # Optional, enable query log
```

> npm run test

# Dependencies:
[MySQL2 - Node.js MySQL driver](https://www.npmjs.com/package/mysql2)

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