JSPM

@remix-run/data-table-mysql

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2021
  • Score
    100M100P100Q114577F
  • License MIT

MySQL adapter for @remix-run/data-table

Package Exports

  • @remix-run/data-table-mysql
  • @remix-run/data-table-mysql/package.json

Readme

data-table-mysql

MySQL adapter for remix/data-table. Use this package when you want data-table APIs backed by mysql2.

Features

  • Native mysql2 Integration: Works with mysql2/promise connection pools
  • Full data-table API Support: Queries, relations, writes, and transactions
  • MySQL Capabilities Enabled By Default:
    • returning: false
    • savepoints: true
    • upsert: true

Installation

npm i remix mysql2

Usage

import { 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.

Adapter Capabilities

data-table-mysql reports this capability set by default:

  • returning: false
  • savepoints: true
  • upsert: true

Advanced Usage

Capability Overrides For Testing

Capability overrides are mainly for tests where you want to force or disable specific behavior checks. In production, keep defaults so adapter behavior matches MySQL behavior.

import { createMysqlDatabaseAdapter } from 'remix/data-table-mysql'

let adapter = createMysqlDatabaseAdapter(pool, {
  capabilities: {
    upsert: false,
  },
})

returning On MySQL

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
  }
}

License

See LICENSE