MySQL adapter for remix/data-table. Use this package when you want data-table APIs backed by mysql2.
- Native
mysql2Integration: Works withmysql2/promisePoolandPoolConnectioninstances - Full
data-tableAPI Support: Queries, relations, writes, and transactions - Adapter-Owned Compiler: SQL compilation lives in this adapter, with optional shared pure helpers from
data-table - Multi-Statement Migrations:
executeScript()runsup.sql/down.sqlfiles viamysql2(requiresmultipleStatements: true) - MySQL Capabilities Enabled By Default:
returning: falsesavepoints: trueupsert: truetransactionalDdl: falsemigrationLock: true
npm i remix mysql2import { createPool } from 'mysql2/promise'
import { createDatabase } from 'remix/data-table'
import { createMysqlDatabaseAdapter } from 'remix/data-table/mysql'
let pool = createPool(process.env.DATABASE_URL as string)
let db = createDatabase(createMysqlDatabaseAdapter(pool))Use db.query(...), relation loading, and transactions from remix/data-table. Import any driver-specific types you need directly from mysql2/promise.
data-table-mysql reports this capability set by default:
returning: falsesavepoints: trueupsert: truetransactionalDdl: falsemigrationLock: true
remix/data-table/migrations sends each migration to the adapter as a single multi-statement SQL script. mysql2 only accepts multi-statement scripts when the connection is created with multipleStatements: true:
import { createPool } from 'mysql2/promise'
let pool = createPool({
uri: process.env.DATABASE_URL,
multipleStatements: true,
})MySQL does not natively support SQL RETURNING. In this adapter, using returning on write operations throws DataTableQueryError.
Use write metadata (affectedRows, insertId) on MySQL, or switch adapters when returned rows are required.
import { DataTableQueryError } from 'remix/data-table'
try {
await db
.query(Accounts)
.insert({ email: 'a@example.com', status: 'active' }, { returning: ['id'] })
} catch (error) {
if (error instanceof DataTableQueryError) {
// insert() returning is not supported by this adapter
}
}To start a local MySQL container matching CI:
podman run --name mysql \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=remix \
-p 3306:3306 \
-d mysql:8Then run:
REMIX_DATA_TABLE_MYSQL_TEST_URL=mysql://root:root@127.0.0.1:3306/remix \
pnpm test src/lib/adapter.integration.test.tsRemove the container when you are done:
podman rm -f mysqldata-table- Core query/relations APIdata-schema- Schema parsing and validationdata-table-postgres- PostgreSQL adapterdata-table-sqlite- SQLite adapter
See LICENSE