Skip to main content
The MySqlDatabase class is the core interface for interacting with a MySQL database. It provides methods for executing queries, managing transactions, and working with Common Table Expressions (CTEs).

Database Methods

select()

Creates a SELECT query. Call with no arguments to select all columns, or pass a selection object to specify columns.
object
Selection object specifying which columns to select. Each key is the result field name, each value is a column or expression.

Examples


selectDistinct()

Creates a SELECT DISTINCT query to return only unique rows.
object
Selection object specifying which columns to select for uniqueness.

Examples


insert()

Creates an INSERT query to add new rows to a table.
MySqlTable
required
The table to insert into.

Examples


update()

Creates an UPDATE query to modify existing rows.
MySqlTable
required
The table to update.
Always use a .where() clause with updates unless you intend to update all rows.

Examples


delete()

Creates a DELETE query to remove rows from a table.
MySqlTable
required
The table to delete from.
Calling delete without a .where() clause will delete all rows in the table.

Examples


$with()

Defines a Common Table Expression (CTE) for use in queries.
string
required
The alias name for the CTE.

Examples


with()

Incorporates previously defined CTEs into the main query.
WithSubquery[]
required
One or more CTEs to incorporate.

Examples


$count()

Creates a count query builder for efficient row counting.
MySqlTable | MySqlViewBase | SQL
required
The table, view, or SQL expression to count rows from.
SQL
Optional WHERE clause filters.

Examples


execute()

Executes raw SQL queries.
SQLWrapper | string
required
The SQL query to execute. Can be a string or a SQL template tagged expression.

Examples


transaction()

Executes a function within a database transaction.
function
required
Callback function that receives a transaction object. All database operations within this function will be part of the transaction.
MySqlTransactionConfig
Transaction configuration options like isolation level.

Examples


Relational Queries

query

Access relational query API when a schema is provided.
The query property is only available when you pass a schema to the database constructor.

Examples


Read Replicas

withReplicas()

Configures read replicas for read/write splitting.
MySqlDatabase
required
The primary (write) database connection.
MySqlDatabase[]
required
Array of replica (read) database connections. Must have at least one replica.
function
Custom function to select which replica to use. Defaults to random selection.

Examples


Type Helpers

MySqlDatabase

MySqlQueryResultHKT
The query result type for the specific MySQL driver being used.
PreparedQueryHKTBase
The prepared query type for the specific MySQL driver.
Record<string, unknown>
The full schema definition including all tables and relations.
TablesRelationalConfig
Extracted relational configuration from the full schema.