Architecture
Last updated
This page shows in one overview diagram which layers make up MySQLPool and how they connect.
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<br/>static state]
App --> Raw["read() / write()"]
QB --> Exec["query() Executor<br/>slow query log"]
Raw --> Exec
Exec --> RP
Exec --> WP
Sig[SIGINT / SIGTERM] --> Close["close()"]
Close --> RP
Close --> WP
Layers
| Layer | Location | Responsibility |
|---|---|---|
| Config resolution | init(), correctConfig() (src/MySQLPool.ts:40-97) |
Picks read and write configs by priority, fills defaults, creates and verifies mysql2/promise pools |
| Query builder | table() through increase() (src/MySQLPool.ts:117-227) |
Accumulates columns, conditions, joins, sorting, and paging in static fields |
| SQL assembly | get(), insert(), update(), upsert() (src/MySQLPool.ts:229-360) |
Turns the accumulated state into SQL and bindings |
| Executor | query() (src/MySQLPool.ts:362-392) |
Picks the pool, checks out a connection, runs, times, and releases it |
| Raw entry points | read(), write() (src/MySQLPool.ts:394-406) |
Run hand-written SQL on a fixed pool |
| Lifecycle | close() and signal handlers (src/MySQLPool.ts:99-115, 409-417) |
Close the pools; on a signal, close and exit the process |
Cross-Cutting Principles
| Principle | Detail |
|---|---|
| Single database exit | Every SQL statement passes through query(), so slow query logging and connection release live in one place |
| Values bound, identifiers inlined | Data values use ? placeholders; column names, operators, and LIMIT/OFFSET numbers are written into SQL |
| Process-wide singleton | MySQLPool has only static members and a private constructor, so the whole process shares one pair of pools and one builder state |
Further Reading
- Per-module diagrams, data flow, and state machine: doc/architecture.md
- Read-Write Pools, Builder State