For the complete documentation index, see llms.txt. This page is also available as Markdown.

TypeScript Usage

MariaDB Connector/Node.js ships with built-in TypeScript definitions enabling typed query results and connection options without requiring a separate @types package.

MariaDB Connector/Node.js ships with built-in TypeScript definitions. No additional @types/ package is required.

Quick Start

Install the connector (TypeScript definitions are included):

$ npm install mariadb

Import using ES module syntax:

import mariadb from 'mariadb';

Or with named imports:

import { createConnection, createPool, SqlError } from 'mariadb';

A minimal typed example:

import mariadb from 'mariadb';

async function main(): Promise<void> {
  const conn = await mariadb.createConnection({
    host: 'mydb.com',
    user: 'myUser',
    password: 'myPwd',
  });

  try {
    const rows = await conn.query<{ now: Date }[]>('SELECT NOW() as now');
    console.log(rows[0].now);
  } finally {
    await conn.end();
  }
}

main();

Typing Query Results

connection.query() and connection.execute() accept a generic type parameter for the result type, and an optional second generic type parameter for the values array (added in 3.5.1).

For INSERT, UPDATE, and DELETE queries, the result is a UpsertResult object:

Accessing Result Metadata (RowsWithMeta / WithMeta)

Every result-set carries column metadata, an array of FieldInfo objects describing each returned column. Since 3.5.3, the connector exports two helper types so you can type that metadata without writing the intersection or tuple shapes by hand.

RowsWithMeta<T> default result shape

By default, the result is a row array with a non-enumerable meta property attached. Because meta is non-enumerable it does not appear in JSON.stringify(), for...in, or spreads, so a plain T[] type doesn't describe it. RowsWithMeta<T> adds it:

RowsWithMeta<Animal> expands to Animal[] & { meta: FieldInfo[] }, so it is still iterable and indexable exactly like a normal row array.

WithMeta<T> tuple shape with metaAsArray

Setting metaAsArray: true (per-query or on the connection) returns [rows, metadata] as a tuple instead of attaching a meta property, a compatibility shape that matches mysql2. WithMeta<T> describes that tuple:

WithMeta<T> resolves to [T, FieldInfo[]]. If T is already a [any, FieldInfo[]] tuple it is returned unchanged, so pre-wrapped types keep working.

FieldInfo column metadata

Each entry of meta is a FieldInfo describing one column, exposing both properties (columnLength, type, scale, collation, …) and accessor methods (name(), db(), table(), signed(), …):

Since 3.5.3, FieldInfo additionally exposes the MariaDB extended type name via the dataTypeName property (e.g. 'uuid', 'inet6', 'json'; MariaDB only, undefined on MySQL) and the isDataTypeFormatJson() helper.

Since 3.5.4, the Types (name) and TypeNumbers (protocol code) enums are real runtime values, so they can be compared against instead of hard-coding strings. Earlier versions declared them in the type definitions only, and importing them threw at runtime:

The name for the 4-byte integer type is INT (protocol code 3), not LONG. Before 3.5.4, the type definitions declared it as Types.LONG, which never matched the 'INT' value returned at runtime.

Connection with Type-Safe Options

Connection options are fully typed via the ConnectionConfig interface:

Pool with TypeScript

Pool options are typed via the PoolConfig interface:

Getting a Typed Connection from a Pool

Automatic Connection Release with await using

Since 3.5.1, ConnectionPromise implements Symbol.asyncDispose, enabling the await using syntax from TypeScript 5.2+ (ES2024). This ensures conn.end() or conn.release() is called automatically when the block exits, even if an error is thrown.

Requirements: TypeScript ≥ 5.2 and "lib": ["ES2022", "ESNext"] (or "ESNext") in your tsconfig.json.

Standalone connection

Pool connection

Transaction with await using

For transactions, use try/catch inside the block to handle rollback on error. The connection is still released automatically regardless of outcome:

This replaces the previous try/finally pattern:

Error Handling

Errors thrown by the connector are instances of SqlError, which extends the standard Error with additional properties:

tsconfig.json Recommendations

The "ESNext" entry in lib is required for Symbol.asyncDispose and the await using syntax.

This page is: Copyright © 2026 MariaDB. All rights reserved.

spinner

Last updated

Was this helpful?