API Reference

Last updated


This page lists the signature, return value, and detail page of every public MySQLPool static method.

Exports


  1. import MySQLPool from "@pardnchiu/mysql-pool"; // default export
  2. import { MySQLPool } from "@pardnchiu/mysql-pool"; // named export

MySQLPool has a private constructor and only static methods. Builder methods return typeof MySQLPool for chaining.

Lifecycle


MethodSignatureDescription
initinit(config?: Record<string, MySQLConfig>): Promise<void>Creates and verifies the read and write pools; see Configuration
closeclose(): Promise<void>Closes both pools; see Lifecycle and Shutdown

Query Building


MethodSignatureDescription
tabletable(tableName: string, target?: "read" | "write"): typeof MySQLPoolResets state and sets the table and the pool for get(), default "read"
selectselect(...fields: string[]): typeof MySQLPoolSets columns; see Select and Where
wherewhere(column: string, operator: any, value?: any): typeof MySQLPoolAdds an AND condition
innerJoininnerJoin(table: string, first: string, operator: string, second?: string): typeof MySQLPoolSee Joins
leftJoinSame as innerJoinLEFT JOIN
rightJoinSame as innerJoinRIGHT JOIN
orderByorderBy(column: string, direction?: "ASC" | "DESC" | "asc" | "desc"): typeof MySQLPoolSee Sorting and Pagination
limitlimit(num: number): typeof MySQLPoolLIMIT
offsetoffset(num: number): typeof MySQLPoolOFFSET
totaltotal(): typeof MySQLPoolAdds a total row count to every row
increaseincrease(target: string, number?: number): typeof MySQLPoolAdds an increment to update(); see Insert and Update

Execution


MethodSignaturePoolReturns
getget<T = any>(): Promise<T[]>Set by table()Array of rows
insertinsert(data: Record<string, any>): Promise<number | null>WriteinsertId, or null when 0
updateupdate(data?: Record<string, any>): Promise<ResultSetHeader>Writemysql2 ResultSetHeader
upsertupsert(data: Record<string, any>, updateData?: Record<string, any> | string): Promise<number | null>WriteinsertId, or null when 0; see Upsert
queryquery<T = any>(query: string, params?: any[], target?: "read" | "write"): Promise<T>target or the most recent table()First element of the mysql2 result
readread<T = any>(query: string, params?: any[]): Promise<T>ReadSame as query
writewrite<T = any>(query: string, params?: any[]): Promise<T>WriteSame as query; see Raw SQL

Error Messages


MessageCause
Table not set, query aborted.get() before any table() call
Table not set, insert aborted. / update aborted. / upsert aborted.The matching method before any table() call
read connection is not available. / write connection is not available.query() found no pool for the target
Read pool connection is not available. / Write pool connection is not available.read()/write() found no pool

Other errors (connection failures, SQL syntax errors) are mysql2's original errors, rethrown unchanged.
中文