> [!NOTE]
> This README was generated by [SKILL](https://github.com/agenvoy/skill-readme-generate), get the ZH version from [here](https://github.com/pardnio/node-mysql-pool/blob/main/doc/README.zh.md).

***

<p align="center">
<strong>CHAIN YOUR MYSQL QUERIES ACROSS READ AND WRITE POOLS!</strong>
</p>

<p align="center">
<a href="https://www.npmjs.com/package/@pardnchiu/mysql-pool"><img src="https://img.shields.io/npm/v/@pardnchiu/mysql-pool?include_prereleases&style=for-the-badge" alt="npm"></a>
<a href="https://www.jsdelivr.com/package/npm/@pardnchiu/mysql-pool"><img src="https://img.shields.io/jsdelivr/npm/hm/@pardnchiu/mysql-pool?include_prereleases&style=for-the-badge" alt="Downloads"></a>
<a href="https://www.npmjs.com/package/@pardnchiu/mysql-pool"><img src="https://img.shields.io/npm/l/@pardnchiu/mysql-pool?include_prereleases&style=for-the-badge" alt="License"></a>
</p>

***

> A Node.js MySQL connection pool with read-write splitting, a chainable query builder, and slow query logging

## Table of Contents

- [Features](#features)
- [Architecture](#architecture)
- [License](#license)
- [Author](#author)

## Features

> `npm install @pardnchiu/mysql-pool` · [Documentation](https://github.com/pardnio/node-mysql-pool/blob/main/doc/doc.md)

- **Read-Write Split Pools** — Maintains separate read and write connection pools, configured from an object or `DB_READ_*`/`DB_WRITE_*` environment variables, with the write side falling back to the read config when omitted.
- **Chainable Query Builder** — Chain select, where, join, orderBy, and limit from `table()` through `get()`, binding every value through placeholders instead of string concatenation.
- **Pagination Total in One Query** — `total()` attaches the full row count via the `COUNT(*) OVER()` window function, so paging never needs a second COUNT query.
- **UPSERT with MySQL Function Allowlist** — Values matching allowlisted functions such as `NOW()` and `UUID()` are inlined into SQL while everything else is bound as a parameter.
- **Slow Query Logging and Graceful Shutdown** — Logs the duration and SQL of any query over 20ms and releases both pools automatically on `SIGINT`/`SIGTERM`.

## Architecture

> [Full Architecture](https://github.com/pardnio/node-mysql-pool/blob/main/doc/architecture.md)

```mermaid
graph TB
    App[Application] --> Init["init()"]
    Init --> Cfg[Config Resolution<br/>object / env vars]
    Cfg --> RP[(Read Pool)]
    Cfg --> WP[(Write Pool)]
    App --> QB[Chainable Query Builder]
    App --> Raw["read() / write()"]
    QB --> Exec["query() Executor<br/>slow query log"]
    Raw --> Exec
    Exec --> RP
    Exec --> WP
```

## License

This project is licensed under the [MIT LICENSE](https://github.com/pardnio/node-mysql-pool/blob/main/LICENSE).

## Author

Just [open an issue](https://github.com/pardnio/node-mysql-pool/issues/new) to share an idea.

<a href="https://github.com/pardnio/node-mysql-pool/graphs/contributors">
  <img src="https://contrib.rocks/image?repo=pardnio/node-mysql-pool&cache_bust=2026-10-07" alt="node-mysql-pool contributors" />
</a>

***

©️ 2025 [邱敬幃 Pardn Chiu](https://www.linkedin.com/in/pardnchiu)
