Skip to main content

MySQL adapter

The MySQL adapter introspects via information_schema and produces SQL with ? positional parameters and backtick-quoted identifiers.

Setup​

import mysql from 'mysql2/promise';
import { Biref, mysqlAdapter } from '@biref/scanner';

const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'secret',
database: 'mydb',
});

const biref = Biref.builder()
.withAdapter(mysqlAdapter.create(connection))
.build();

Client interface​

mysqlAdapter.create() accepts any object matching this structural type:

interface MySQLClient {
execute<TRow>(
sql: string,
params?: any[],
): Promise<[TRow[], unknown]>;
}

Both mysql2/promise Connection and Pool satisfy this shape. The SDK never imports mysql2.

Namespace model​

MySQL uses databases as namespaces. Default: the connected database (DATABASE()).

// Scan specific databases
const model = await biref.scan({ namespaces: ['mydb', 'other_db'] });

// Scan everything (excludes information_schema, mysql, performance_schema, sys)
const model = await biref.scan({ namespaces: 'all' });

// Default: scans the connected database
const model = await biref.scan();

URL schemes​

mysql, mariadb

Type mapping​

MySQL typeCategory
tinyint, smallint, mediumint, int, bigintinteger
tinyint(1)boolean
decimal, numeric, float, doubledecimal
varchar, char, text, tinytext, mediumtext, longtextstring
date, yeardate
datetime, timestamptimestamp
timetime
jsonjson
binary, varbinary, blob, tinyblob, mediumblob, longblob, bitbinary
enumenum (values parsed from COLUMN_TYPE)
setenum (values parsed from COLUMN_TYPE)

tinyint(1) as boolean​

MySQL conventionally uses TINYINT(1) for boolean columns. The adapter detects this via COLUMN_TYPE and maps it to the boolean category. Regular TINYINT (without the (1) display width) maps to integer.

Enum and set parsing​

Enum values are extracted from the COLUMN_TYPE string:

enum('active','inactive','banned') -> ['active', 'inactive', 'banned']
set('read','write','admin') -> ['read', 'write', 'admin']

Record parser​

MySQLRecordParser extends DefaultRecordParser with:

  • bigint strings converted to JS bigint
  • tinyint(1) numeric 0/1 converted to JS boolean
  • json strings parsed to objects
  • decimal preserved as strings (driver default)

Indexes​

Supports: btree, hash. Fulltext and spatial indexes map to unknown. MySQL does not support partial indexes.

Constraints​

Supports: unique, check (MySQL 8.0.16+). Check constraints on older versions are silently skipped.

LIMIT / OFFSET​

MySQL's prepared statement protocol does not reliably handle ? placeholders for LIMIT and OFFSET. The query engine inlines them as literal integers in the SQL text. They are safe since they always come from numeric values in the SDK, never from user input.