Configuration

Last updated

This page lists every config source, field, and default that init() accepts.

Source Priority

init(config?) picks one source for each side using the table below (src/MySQLPool.ts:52-72). The chosen source is used as a whole; fields are never merged across sources:

Pool Source (highest first)
Read config.config → config.read → DB_READ_* env vars
Write config.config → config.write → config.read → DB_WRITE_* env vars
import MySQLPool from "@pardnchiu/mysql-pool";

// Read-write split
await MySQLPool.init({
  read: { host: "replica.local", user: "reader", password: "secret", database: "app", connectionLimit: 16 },
  write: { host: "primary.local", user: "writer", password: "secret", database: "app", connectionLimit: 4 },
});

MySQLConfig

interface MySQLConfig {
  host?: string;
  port?: number;
  user?: string;
  password?: string;
  database?: string;
  charset?: string;
  connectionLimit?: number;
}

Every field gets a default and is trimmed by correctConfig() (src/MySQLPool.ts:40-50):

Field Default Detail
host localhost
port 3306 Also falls back to 3306 when it does not parse as an integer
user root
password "" Leading and trailing whitespace is removed
database "" An empty string skips that side's pool
charset utf8mb4
connectionLimit 8 Maximum open connections in the pool

MySQLConfig is not exported from the package.

Environment Variables

Read when no matching config object is passed:

Read side Write side Default
DB_READ_HOST DB_WRITE_HOST localhost
DB_READ_PORT DB_WRITE_PORT 3306
DB_READ_USER DB_WRITE_USER root
DB_READ_PASSWORD DB_WRITE_PASSWORD ""
DB_READ_DATABASE DB_WRITE_DATABASE "" (no pool)
DB_READ_CHARSET DB_WRITE_CHARSET utf8mb4
DB_READ_CONNECTION DB_WRITE_CONNECTION 4

Note the different connection defaults: 4 through environment variables, 8 through a config object without connectionLimit.

Fixed Pool Options

Beyond the fields above, both pools are created with waitForConnections: true; other mysql2 pool options (such as queueLimit, timezone, or ssl) cannot be passed through init().

中文