Overview
@effect/sql-mysql2 provides a MySQL driver for Effect’s SQL toolkit. It uses the mysql2 library and supports connection pooling, transactions, and prepared statements.
Installation
Basic Usage
Creating a Client
Use theMysqlClient.layer function to create a MySQL client layer:
Connection with URL
You can also connect using a connection URL:Executing Queries
Configuration Options
Connection Options
| Option | Type | Description |
|---|---|---|
url | Redacted | MySQL connection URL (overrides other connection options) |
host | string | Database host |
port | number | Database port (default: 3306) |
database | string | Database name |
username | string | Username for authentication |
password | Redacted | Password for authentication |
Connection Pool Options
| Option | Type | Description |
|---|---|---|
maxConnections | number | Maximum number of connections in the pool |
connectionTTL | Duration.Input | Time-to-live for idle connections |
poolConfig | Mysql.PoolOptions | Additional mysql2 pool configuration |
Transform Options
| Option | Type | Description |
|---|---|---|
transformResultNames | (str: string) => string | Transform column names in results |
transformQueryNames | (str: string) => string | Transform identifiers in queries |
Advanced Options
| Option | Type | Description |
|---|---|---|
spanAttributes | Record<string, unknown> | Custom telemetry attributes |
Transactions
Automatic transaction management:Name Transformations
Automatically convert between naming conventions:Error Handling
All SQL errors are wrapped inSqlError:
