API Reference
Last updated
This page lists the signature, return value, and detail page of every public MySQLPool static method.
Exports
import MySQLPool from "@pardnchiu/mysql-pool"; // default export
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
| Method | Signature | Description |
|---|---|---|
init |
init(config?: Record<string, MySQLConfig>): Promise<void> |
Creates and verifies the read and write pools; see Configuration |
close |
close(): Promise<void> |
Closes both pools; see Lifecycle and Shutdown |
Query Building
| Method | Signature | Description |
|---|---|---|
table |
table(tableName: string, target?: "read" | "write"): typeof MySQLPool |
Resets state and sets the table and the pool for get(), default "read" |
select |
select(...fields: string[]): typeof MySQLPool |
Sets columns; see Select and Where |
where |
where(column: string, operator: any, value?: any): typeof MySQLPool |
Adds an AND condition |
innerJoin |
innerJoin(table: string, first: string, operator: string, second?: string): typeof MySQLPool |
See Joins |
leftJoin |
Same as innerJoin |
LEFT JOIN |
rightJoin |
Same as innerJoin |
RIGHT JOIN |
orderBy |
orderBy(column: string, direction?: "ASC" | "DESC" | "asc" | "desc"): typeof MySQLPool |
See Sorting and Pagination |
limit |
limit(num: number): typeof MySQLPool |
LIMIT |
offset |
offset(num: number): typeof MySQLPool |
OFFSET |
total |
total(): typeof MySQLPool |
Adds a total row count to every row |
increase |
increase(target: string, number?: number): typeof MySQLPool |
Adds an increment to update(); see Insert and Update |
Execution
| Method | Signature | Pool | Returns |
|---|---|---|---|
get |
get<T = any>(): Promise<T[]> |
Set by table() |
Array of rows |
insert |
insert(data: Record<string, any>): Promise<number | null> |
Write | insertId, or null when 0 |
update |
update(data?: Record<string, any>): Promise<ResultSetHeader> |
Write | mysql2 ResultSetHeader |
upsert |
upsert(data: Record<string, any>, updateData?: Record<string, any> | string): Promise<number | null> |
Write | insertId, or null when 0; see Upsert |
query |
query<T = any>(query: string, params?: any[], target?: "read" | "write"): Promise<T> |
target or the most recent table() |
First element of the mysql2 result |
read |
read<T = any>(query: string, params?: any[]): Promise<T> |
Read | Same as query |
write |
write<T = any>(query: string, params?: any[]): Promise<T> |
Write | Same as query; see Raw SQL |
Error Messages
| Message | Cause |
|---|---|
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 |
tableName is null only initially and never returns to null after the first table() call, so Table not set only fires before first use. Other errors (connection failures, SQL syntax errors) are mysql2's original errors, rethrown unchanged.