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: object / env vars"]
    Cfg --> RP[(Read Pool)]
    Cfg --> WP[(Write Pool)]
    App --> QB["Chainable Query Builder: static state"]
    App --> Raw["read() / write()"]
    QB --> Exec["query() Executor: slow query log"]
    Raw --> Exec
    Exec --> RP
    Exec --> WP
  1. graph TB
  2. App[Application] --> Init["init()"]
  3. Init --> Cfg["Config Resolution: object / env vars"]
  4. Cfg --> RP[(Read Pool)]
  5. Cfg --> WP[(Write Pool)]
  6. App --> QB["Chainable Query Builder: static state"]
  7. App --> Raw["read() / write()"]
  8. QB --> Exec["query() Executor: slow query log"]
  9. Raw --> Exec
  10. Exec --> RP
  11. Exec --> WP

Layers


LayerLocationResponsibility
Config resolutioninit(), correctConfig() (src/MySQLPool.ts:40-97)Picks read and write configs by priority, fills defaults, creates and verifies mysql2/promise pools
Query buildertable() through increase() (src/MySQLPool.ts:117-227)Accumulates columns, conditions, joins, sorting, and paging in static fields
SQL assemblyget(), insert(), update(), upsert() (src/MySQLPool.ts:229-360)Turns the accumulated state into SQL and bindings
Executorquery() (src/MySQLPool.ts:362-392)Picks the pool, checks out a connection, runs, times, and releases it
Raw entry pointsread(), write() (src/MySQLPool.ts:394-406)Run hand-written SQL on a fixed pool
Lifecycleclose() (src/MySQLPool.ts:99-115)Close the pools

Cross-Cutting Principles


PrincipleDetail
Single database exitEvery SQL statement passes through query(), so slow query logging and connection release live in one place
Process-wide singletonMySQLPool has only static members and a private constructor, so the whole process shares one pair of pools and one builder state

Where It Sits Among Node.js MySQL Libraries


LibraryWhat it isHow MySQLPool relates
mysql2MySQL driver for Node.jsMySQLPool is built on it: both pools come from mysql2/promise createPool(), and raw results and errors are mysql2's own
Knex.jsSQL query builder for PostgreSQL, MySQL, SQLite, SQL Server, and other databasesMySQLPool targets MySQL only and keeps two pools, picking read or write per call through table(name, target), read(), and write()
SequelizeORM with models, associations, and read replicationMySQLPool has no models or associations; it is a static class whose chain goes straight from table() to SQL
KyselyType-safe SQL query builder for TypeScriptMySQLPool takes column names as plain strings and types results only through the get<T>() generic, with no compile-time schema checking

Further Reading


中文