@probitas/client-sql-mysql

MySQL client for Probitas scenario testing framework.

This package provides a MySQL client designed for integration testing, with transaction support and prepared statement capabilities.

Features

  • Query Execution: Parameterized queries with type-safe results
  • Transactions: Full transaction support with isolation levels
  • Prepared Statements: Automatic parameter escaping and type conversion
  • Connection Pooling: Configurable pool with idle timeout
  • Resource Management: Implements AsyncDisposable for proper cleanup

Installation

deno add jsr:@probitas/client-sql-mysql

Quick Start

import { createMySqlClient } from "@probitas/client-sql-mysql";

const client = await createMySqlClient({
  url: {
    host: "localhost",
    port: 3306,
    username: "root",
    password: "secret",
    database: "mydb",
  },
});

// Query with parameters (uses ? placeholders)
const result = await client.query<{ id: number; name: string }>(
  "SELECT id, name FROM users WHERE active = ?",
  [true]
);
console.log(result.rows);

// Get first row
const user = await client.queryOne<{ id: number; name: string }>(
  "SELECT * FROM users WHERE id = ?",
  [1]
);

await client.close();

Transactions

await client.transaction(async (tx) => {
  await tx.query("INSERT INTO accounts (id, balance) VALUES (?, ?)", [1, 100]);
  await tx.query("INSERT INTO accounts (id, balance) VALUES (?, ?)", [2, 200]);
  // Automatically committed if no error, rolled back on exception
}, { isolationLevel: "serializable" });

Using with using Statement

await using client = await createMySqlClient({
  url: { host: "localhost", username: "root", database: "testdb" },
});

const result = await client.query("SELECT 1 as n");
// Client automatically closed when block exits
Package Description
@probitas/client-sql Common SQL types and utilities
@probitas/client-sql-postgres PostgreSQL client
@probitas/client-sql-sqlite SQLite client
@probitas/client-sql-duckdb DuckDB client

Installation

deno add jsr:@probitas/client-sql-mysql

Classes

class

#AccessDeniedError

class AccessDeniedError extends MySqlError
ExtendsMySqlError

Error thrown when access is denied (wrong credentials).

NameDescription
name
mysqlKind
Constructor
new AccessDeniedError(message: string, options?: MySqlErrorOptions)
Properties
  • readonlynamestring
  • readonlymysqlKind"access_denied"
class

#ConnectionRefusedError

class ConnectionRefusedError extends MySqlError
ExtendsMySqlError

Error thrown when connection is refused.

NameDescription
name
mysqlKind
Constructor
new ConnectionRefusedError(message: string, options?: MySqlErrorOptions)
Properties
  • readonlynamestring
  • readonlymysqlKind"connection_refused"
class

#ConstraintError

class ConstraintError extends SqlError
ExtendsSqlError

Error thrown when a constraint violation occurs.

NameDescription
name
kind
constraint
Constructor
new ConstraintError(message: string, constraint: string, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"constraint"
  • readonlyconstraintstring
class

#DeadlockError

class DeadlockError extends SqlError
ExtendsSqlError

Error thrown when a deadlock is detected.

NameDescription
name
kind
Constructor
new DeadlockError(message: string, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"deadlock"
class

#MySqlError

class MySqlError extends SqlError
ExtendsSqlError

Base error class for MySQL-specific errors. Extends SqlError with MySQL-specific properties like errno.

NameDescription
name
errno
Constructor
new MySqlError(message: string, options?: MySqlErrorOptions)
Properties
  • readonlynamestring
  • readonlyerrno?number
class

#MySqlTransactionImpl

class MySqlTransactionImpl implements MySqlTransaction
Constructor
new MySqlTransactionImpl(connection: AnyPoolConnection)
Methods
static begin(): unknown

Begin a new transaction.

query(): unknown
queryOne(): unknown
commit(): unknown
rollback(): unknown
savepoint(): unknown
rollbackToSavepoint(): unknown
releaseSavepoint(): unknown
class

#QuerySyntaxError

class QuerySyntaxError extends SqlError
ExtendsSqlError

Error thrown when a SQL query has syntax errors.

NameDescription
name
kind
Constructor
new QuerySyntaxError(message: string, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"query"
class

#SqlError

class SqlError extends ClientError

Base error class for SQL-specific errors. Extends ClientError with SQL-specific properties.

NameDescription
name
kind
sqlState
Constructor
new SqlError(message: string, kind: SqlErrorKind, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykindSqlErrorKind
  • readonlysqlState?string
class

#SqlQueryResult

class SqlQueryResult<T = Record<string, any>>

SQL query result with rows, metadata, and transformation methods.

NameDescription
ok
rows
rowCount
duration
metadata
map()Map rows to a new type.
as()Create class instances from rows.
Constructor
new SqlQueryResult(init: SqlQueryResultInit<T>)
Properties
Methods
map(): unknown

Map rows to a new type.

as(): unknown

Create class instances from rows.

class

#SqlRows

class SqlRows<T> extends Array<T>
ExtendsArray<T>

Row array with first/last helper methods. Implements ReadonlyArray by extending Array.

NameDescription
first()Get the first row, or undefined if empty.
firstOrThrow()Get the first row, or throw if empty.
last()Get the last row, or undefined if empty.
lastOrThrow()Get the last row, or throw if empty.
Constructor
new SqlRows(items: readonly T[])
Methods
first(): unknown

Get the first row, or undefined if empty.

firstOrThrow(): unknown

Get the first row, or throw if empty.

last(): unknown

Get the last row, or undefined if empty.

lastOrThrow(): unknown

Get the last row, or throw if empty.

Interfaces

interface

#MySqlClient

interface MySqlClient extends AsyncDisposable

MySQL client interface.

NameDescription
configThe client configuration.
dialectThe SQL dialect identifier.
query()Execute a SQL query.
queryOne()Execute a query and return the first row or undefined.
transaction()Execute a function within a transaction.
close()Close the client and release all connections.
Properties
  • readonlyconfigMySqlClientConfig

    The client configuration.

  • readonlydialect"mysql"

    The SQL dialect identifier.

Methods
query<T = Record<string, any>>(sql: string, params?: unknown[]): Promise<SqlQueryResult<T>>

Execute a SQL query.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
queryOne<T = Record<string, any>>(sql: string, params?: unknown[]): Promise<T | undefined>

Execute a query and return the first row or undefined.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
transaction<T>(fn: (tx: SqlTransaction) => unknown, options?: SqlTransactionOptions): Promise<T>

Execute a function within a transaction. Automatically commits on success or rolls back on error.

Parameters
close(): Promise<void>

Close the client and release all connections.

interface

#MySqlClientConfig

interface MySqlClientConfig extends CommonOptions

Configuration for creating a MySQL client.

NameDescription
urlConnection URL or configuration.
poolConnection pool configuration.
timezoneTimezone for the connection.
Properties
  • readonlyurlstring | MySqlConnectionConfig

    Connection URL or configuration.

    Can be a connection URL string (e.g., "mysql://user:pass@host:port/database") or a detailed MySqlConnectionConfig object.

  • readonlypool?MySqlPoolConfig

    Connection pool configuration.

  • readonlytimezone?string

    Timezone for the connection.

interface

#MySqlConnectionConfig

interface MySqlConnectionConfig extends CommonConnectionConfig

MySQL connection configuration.

Extends CommonConnectionConfig with MySQL-specific options.

NameDescription
databaseDatabase name to connect to.
tlsTLS configuration.
charsetCharacter set.
Properties
  • readonlydatabase?string

    Database name to connect to.

  • readonlytls?MySqlTlsConfig

    TLS configuration.

  • readonlycharset?string

    Character set.

interface

#MySqlErrorOptions

interface MySqlErrorOptions extends SqlErrorOptions

Options for MySqlError constructor.

NameDescription
errnoMySQL error code (e.g., 1045, 1062).
Properties
  • readonlyerrno?number

    MySQL error code (e.g., 1045, 1062).

interface

#MySqlPoolConfig

interface MySqlPoolConfig

Connection pool configuration.

NameDescription
connectionLimitMaximum number of connections in the pool.
minConnectionsMinimum number of idle connections.
queueLimitMaximum number of connection requests the pool will queue.
waitForConnectionsWhether to wait for connections to become available.
idleTimeoutTime in milliseconds before connection is considered idle.
Properties
  • readonlyconnectionLimit?number

    Maximum number of connections in the pool.

  • readonlyminConnections?number

    Minimum number of idle connections.

  • readonlyqueueLimit?number

    Maximum number of connection requests the pool will queue.

  • readonlywaitForConnections?boolean

    Whether to wait for connections to become available.

  • readonlyidleTimeout?number

    Time in milliseconds before connection is considered idle.

interface

#MySqlTlsConfig

interface MySqlTlsConfig

TLS/SSL configuration for MySQL connections.

NameDescription
caRoot CA certificate (PEM format).
certClient certificate (PEM format).
keyClient private key (PEM format).
rejectUnauthorizedSkip server certificate verification (use only for testing).
Properties
  • readonlyca?string

    Root CA certificate (PEM format).

  • readonlycert?string

    Client certificate (PEM format).

  • readonlykey?string

    Client private key (PEM format).

  • readonlyrejectUnauthorized?boolean

    Skip server certificate verification (use only for testing).

interface

#MySqlTransaction

interface MySqlTransaction extends SqlTransaction

MySQL-specific transaction interface. Extends SqlTransaction with MySQL-specific features.

NameDescription
savepoint()Create a savepoint.
rollbackToSavepoint()Rollback to a savepoint.
releaseSavepoint()Release a savepoint.
Methods
savepoint(name: string): Promise<void>

Create a savepoint.

Parameters
  • namestring
    • Savepoint name
rollbackToSavepoint(name: string): Promise<void>

Rollback to a savepoint.

Parameters
  • namestring
    • Savepoint name
releaseSavepoint(name: string): Promise<void>

Release a savepoint.

Parameters
  • namestring
    • Savepoint name
interface

#SqlErrorOptions

interface SqlErrorOptions extends ErrorOptions

Options for SqlError constructor.

NameDescription
sqlStateSQL State code (e.g., "23505" for unique violation)
Properties
  • readonlysqlState?string

    SQL State code (e.g., "23505" for unique violation)

interface

#SqlQueryResultExpectation

interface SqlQueryResultExpectation<T>

Expectation interface for SQL query results. All methods return this for chaining.

NameDescription
ok()Verify query succeeded
notOk()Verify query failed
noContent()Verify result has no rows
hasContent()Verify result has rows
rows()Verify exact row count
rowsAtLeast()Verify minimum row count
rowsAtMost()Verify maximum row count
rowCount()Verify exact affected row count
rowCountAtLeast()Verify minimum affected row count
rowCountAtMost()Verify maximum affected row count
rowContains()Verify a row contains the given subset
rowMatch()Custom row validation
mapContains()Verify mapped data contains the given subset
mapMatch()Custom mapped data validation
asContains()Verify instance contains the given subset
asMatch()Custom instance validation
lastInsertId()Verify exact lastInsertId
hasLastInsertId()Verify lastInsertId is present
durationLessThan()Verify query duration is below threshold
Methods
ok(): this

Verify query succeeded

notOk(): this

Verify query failed

noContent(): this

Verify result has no rows

hasContent(): this

Verify result has rows

rows(count: number): this

Verify exact row count

Parameters
  • countnumber
rowsAtLeast(count: number): this

Verify minimum row count

Parameters
  • countnumber
rowsAtMost(count: number): this

Verify maximum row count

Parameters
  • countnumber
rowCount(count: number): this

Verify exact affected row count

Parameters
  • countnumber
rowCountAtLeast(count: number): this

Verify minimum affected row count

Parameters
  • countnumber
rowCountAtMost(count: number): this

Verify maximum affected row count

Parameters
  • countnumber
rowContains(subset: Partial<T>): this

Verify a row contains the given subset

Parameters
  • subsetPartial<T>
rowMatch(matcher: (rows: SqlRows<T>) => unknown): this

Custom row validation

Parameters
  • matcher(rows: SqlRows<T>) => unknown
mapContains<U>(mapper: (row: T) => unknown, subset: Partial<U>): this

Verify mapped data contains the given subset

Parameters
  • mapper(row: T) => unknown
  • subsetPartial<U>
mapMatch<U>(mapper: (row: T) => unknown, matcher: (mapped: U[]) => unknown): this

Custom mapped data validation

Parameters
  • mapper(row: T) => unknown
  • matcher(mapped: U[]) => unknown
asContains<U>(ctor: (row: T) => unknown, subset: Partial<U>): this

Verify instance contains the given subset

Parameters
  • ctor(row: T) => unknown
  • subsetPartial<U>
asMatch<U>(ctor: (row: T) => unknown, matcher: (instances: U[]) => unknown): this

Custom instance validation

Parameters
  • ctor(row: T) => unknown
  • matcher(instances: U[]) => unknown
lastInsertId(expected: bigint | string): this

Verify exact lastInsertId

Parameters
  • expectedbigint | string
hasLastInsertId(): this

Verify lastInsertId is present

durationLessThan(ms: number): this

Verify query duration is below threshold

Parameters
  • msnumber
interface

#SqlQueryResultInit

interface SqlQueryResultInit<T>

Options for creating a SqlQueryResult.

NameDescription
okWhether the query succeeded
rowsThe result rows
rowCountNumber of affected rows (for INSERT/UPDATE/DELETE)
durationQuery execution duration in milliseconds
metadataAdditional metadata
Properties
  • readonlyokboolean

    Whether the query succeeded

  • readonlyrowsreadonly T[]

    The result rows

  • readonlyrowCountnumber

    Number of affected rows (for INSERT/UPDATE/DELETE)

  • readonlydurationnumber

    Query execution duration in milliseconds

  • readonlymetadataSqlQueryResultMetadata

    Additional metadata

interface

#SqlQueryResultMetadata

interface SqlQueryResultMetadata

Metadata for SQL query results.

NameDescription
lastInsertIdLast inserted ID (for INSERT statements)
warningsWarning messages from the database
Properties
  • readonlylastInsertId?bigint | string

    Last inserted ID (for INSERT statements)

  • readonlywarnings?readonly string[]

    Warning messages from the database

interface

#SqlTransaction

interface SqlTransaction

SQL transaction interface. Implementations should provide actual database-specific transaction handling.

NameDescription
query()Execute a query within the transaction.
queryOne()Execute a query and return the first row or undefined.
commit()Commit the transaction.
rollback()Rollback the transaction.
Methods
query<T = Record<string, any>>(sql: string, params?: unknown[]): Promise<SqlQueryResult<T>>

Execute a query within the transaction.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
queryOne<T = Record<string, any>>(sql: string, params?: unknown[]): Promise<T | undefined>

Execute a query and return the first row or undefined.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
commit(): Promise<void>

Commit the transaction.

rollback(): Promise<void>

Rollback the transaction.

interface

#SqlTransactionOptions

interface SqlTransactionOptions

Options for starting a transaction.

NameDescription
isolationLevelIsolation level for the transaction
Properties

Functions

function

#convertMySqlError

function convertMySqlError(error: unknown): SqlError

Convert a mysql2 error to the appropriate error class.

Parameters
  • errorunknown
function

#createMySqlClient

async function createMySqlClient(config: MySqlClientConfig): Promise<MySqlClient>

Create a new MySQL client instance with connection pooling.

The client provides connection pooling, parameterized queries, transaction support, and automatic result type mapping.

Parameters
Returns

Promise<MySqlClient> — A promise resolving to a new MySQL client instance

Examples

Using URL string

const client = await createMySqlClient({
  url: "mysql://user:password@localhost:3306/testdb",
});

const result = await client.query<{ id: number; name: string }>(
  "SELECT * FROM users WHERE id = ?",
  [1],
);
console.log(result.rows.first());  // { id: 1, name: "Alice" }

await client.close();

Using connection config object

const client = await createMySqlClient({
  url: {
    host: "localhost",
    port: 3306,
    username: "root",
    password: "password",
    database: "testdb",
  },
  pool: { connectionLimit: 20 },
});

Transaction with auto-commit/rollback

const user = await client.transaction(async (tx) => {
  await tx.query("INSERT INTO users (name) VALUES (?)", ["Alice"]);
  return await tx.queryOne("SELECT LAST_INSERT_ID() as id");
});

Using await using for automatic cleanup

await using client = await createMySqlClient({
  url: "mysql://localhost:3306/testdb",
});

const result = await client.query("SELECT 1");
// Client automatically closed when scope exits
function

#expectSqlQueryResult

function expectSqlQueryResult<T = Record<string, any>>(result: SqlQueryResult<T>): SqlQueryResultExpectation<T>

Create an expectation for a SQL query result.

Parameters

Type Aliases

type

#MySqlErrorKind

type MySqlErrorKind = "access_denied" | "connection_refused"

MySQL-specific error kinds.

type

#SqlErrorKind

type SqlErrorKind = "query" | "constraint" | "deadlock" | "unknown"

SQL-specific error kinds.

type

#SqlIsolationLevel

type SqlIsolationLevel = "read_uncommitted" | "read_committed" | "repeatable_read" | "serializable"

Transaction isolation level.

Search Documentation