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:
PoolSource (highest first)
Readconfig.config → config.read → DB_READ_* env vars
Writeconfig.config → config.write → config.read → DB_WRITE_* env vars

  1. import MySQLPool from "@pardnchiu/mysql-pool";
  2. // Read-write split
  3. await MySQLPool.init({
  4. read: { host: "replica.local", user: "reader", password: "secret", database: "app", connectionLimit: 16 },
  5. write: { host: "primary.local", user: "writer", password: "secret", database: "app", connectionLimit: 4 },
  6. });

MySQLConfig


  1. interface MySQLConfig {
  2. host?: string;
  3. port?: number;
  4. user?: string;
  5. password?: string;
  6. database?: string;
  7. charset?: string;
  8. connectionLimit?: number;
  9. }

Every field gets a default and is trimmed by correctConfig() (src/MySQLPool.ts:40-50):
FieldDefaultDetail
hostlocalhost
port3306Also falls back to 3306 when it does not parse as an integer
userroot
password""Leading and trailing whitespace is removed
database""An empty string skips that side's pool
charsetutf8mb4
connectionLimit8Maximum open connections in the pool

MySQLConfig is not exported from the package.

Environment Variables


Read when no matching config object is passed:
Read sideWrite sideDefault
DB_READ_HOSTDB_WRITE_HOSTlocalhost
DB_READ_PORTDB_WRITE_PORT3306
DB_READ_USERDB_WRITE_USERroot
DB_READ_PASSWORDDB_WRITE_PASSWORD""
DB_READ_DATABASEDB_WRITE_DATABASE"" (no pool)
DB_READ_CHARSETDB_WRITE_CHARSETutf8mb4
DB_READ_CONNECTIONDB_WRITE_CONNECTION4

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

中文