npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@routier/mysql-plugin

v0.5.2

Published

MySQL plugin for Routier

Downloads

253

Readme

@routier/mysql-plugin

Routier storage backed by a MySQL server, through mysql2.

import { DataStore } from "@routier/datastore";
import { MysqlDbPlugin } from "@routier/mysql-plugin";

class AppStore extends DataStore {
  constructor() {
    super(new MysqlDbPlugin({
      host: "localhost",
      port: 3306,
      database: "app",
      user: "app",
      password: process.env.MYSQL_PASSWORD,
      pool: { max: 10 },
    }));
  }
}

Pass connectionString instead of the discrete fields to use a URI:

new MysqlDbPlugin({ connectionString: "mysql://app:secret@localhost:3306/app" });

The two forms are mutually exclusive. Supplying both throws, because there is no correct precedence to guess — a URI that disagrees with an explicit host means the configuration says something the caller does not believe.

There is no pool.min. mysql2 opens connections on demand and has no minimum-size setting.

Contracts

Durability

MySQL's own, subject to innodb_flush_log_at_trx_commit.

Every save runs inside one transaction on one pooled connection. Table creation happens before the transaction opens: MySQL commits the open transaction implicitly when it runs DDL, so a table created mid-save would end that save's transaction and leave a later failure with nothing to roll back.

Column types

| Schema type | Column type | Notes | |---|---|---| | s.string() | VARCHAR(255) | | | s.number() | DOUBLE | Not DECIMAL: mysql2 returns DECIMAL as a string | | s.boolean() | BOOLEAN | A synonym for TINYINT(1); the plugin decodes 0/1 back to booleans | | s.date() | DATETIME | Stored in UTC; the pool sets timezone: 'Z' | | s.object(), s.array() | JSON | One column per root property |

Select-back after a write

MySQL has no RETURNING, so the plugin reads written rows back with a second statement. It picks one of three strategies:

  1. Caller-supplied keys — select by key IN (…).
  2. Composite keys — select by an OR of full-key conjunctions.
  3. A single AUTO_INCREMENT key — select the range insertId … insertId + n - 1.

Strategy 3 assumes the insert allocated a contiguous id block. That holds for the single INSERT statement the plugin emits when innodb_autoinc_lock_mode is 0 or 1 and auto_increment_increment is 1.

Under innodb_autoinc_lock_mode = 2 (interleaved), or a non-1 auto_increment_increment on a multi-source setup, the block is not contiguous. The plugin then reads back the wrong number of rows and fails the save with a message naming both settings. It does not echo the wrong rows.

Set innodb_autoinc_lock_mode to 0 or 1 if you use numeric identity keys.

Concurrency

The plugin holds a mysql2 pool. Each save takes one connection for the length of its transaction and returns it on every path, including a failed rollback.

Optimistic concurrency is supported. Wrap the plugin in ConcurrencyDbPlugin. A stale write matches no row; the plugin detects it from affectedRows — there is no RETURNING to be empty — rolls the transaction back, and reports OptimisticConcurrencyError naming the row.

Schema migration

Initialization only. The plugin runs CREATE TABLE IF NOT EXISTS when a table is missing. It never alters an existing table.

A schema change that adds, removes, or renames a column does not change a table that already exists, and the next write fails on the missing column. Migrate the database yourself.

Disposal

Call store.destroyAsync() to end the pool. This closes the sockets; it does not drop tables.

A pool that is never ended keeps the Node event loop alive. In tests, destroy every store you create.

Failure semantics

  • A statement error rolls the whole transaction back and fails the save. Because DDL runs first, the rollback is total.
  • A duplicate key fails the save and returns the connection to the pool.
  • An undefined parameter is bound as SQL NULL. mysql2 rejects undefined outright, where other drivers accept it, so an entity that omits an optional property would otherwise fail to insert.

Supported versions

Node 18 or later. MySQL 8.0 or later, for the JSON column type; the suites run against mysql:8.0.

See also

  • e2e/src/mysqlContainer.test.ts — behaviour against a real server, plus the contract kit
  • e2e/src/dialectConformance.test.ts — the shared SQL matrix