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

@toshihiko/mysql-adapter

v2.0.0-alpha.0

Published

Promise-only MySQL adapter for Toshihiko v2.

Readme

Toshihiko MySQL Adapter

npm CI Coverage

The Promise-only MySQL Adapter for Toshihiko v2. It uses the mysql2 Promise Pool and requires Node.js 22 or newer.

Installation

npm install toshihiko @toshihiko/mysql-adapter

Usage

Install the package and use the mysql dialect name:

import { Toshihiko, Type } from 'toshihiko';

const toshihiko = new Toshihiko('mysql', {
  database: 'app',
  host: '127.0.0.1',
  password: 'secret',
  username: 'root',
});

const User = toshihiko.define('users', [
  { name: 'id', column: 'user_id', type: Type.Integer, primaryKey: true },
  { name: 'name', type: Type.String },
]);

const users = await User.where({ id: { $gte: 1 } }).find(true);

find, count, writes, raw execution, and transaction methods all return native Promises.

Configuration

MySQLAdapterOptions extends mysql2 PoolOptions. The Adapter handles these Toshihiko-specific fields and passes the remaining driver options to mysql2.createPool().

| Field | Type | Default | Description | |---|---|---:|---| | database | string | 'toshihiko' | Database name and Cache namespace | | host | string | 'localhost' | MySQL host name or IP address | | port | number | 3306 | MySQL port | | user | string | '' | MySQL user name | | username | string | — | Compatibility spelling for user; wins when both are present | | password | string | '' | MySQL password | | pool | MySQLPool | — | Reuses an existing Promise Pool instead of creating one | | showSql | false \| true \| ((sql: string) => void) | false | Enables console logging or calls a custom SQL logger | | cache | Cache \| CacheOptions | — | Existing Cache instance or module configuration inherited by Models | | package | string | — | Compatibility field; the runtime driver remains mysql2 |

Driver fields such as connectionLimit, charset, ssl, and connectTimeout are also accepted. See the mysql2 PoolOptions documentation.

The Adapter constructor and a prebuilt Adapter instance can also be passed directly when an application needs explicit dependency injection:

import { MySQLAdapter } from '@toshihiko/mysql-adapter';

const toshihiko = new Toshihiko(MySQLAdapter, {
  database: 'app',
});

Cache

Attach a cache to Toshihiko or to one model. Models inherit the Toshihiko-level cache unless their own cache option replaces or disables it.

import { MemcachedCache } from '@toshihiko/memcached-cache';

const cache = new MemcachedCache('127.0.0.1:11211');
const toshihiko = new Toshihiko('mysql', {
  cache,
  database: 'app',
});

const UncachedAudit = toshihiko.define('audit', auditSchema, { cache: false });

MySQL reads cached rows and fills misses without changing their input order. Updates and deletes invalidate matching primary-key entries before changing the database. Pass { noCache: true } to find to bypass cache reads for one query.

Raw SQL

execute accepts an optional transaction connection before the SQL string. All forms are Promise-only.

await toshihiko.execute(
  'UPDATE `users` SET `name` = ? WHERE `user_id` = ?',
  ['Alice', 1],
);

const connection = await User.beginTransaction();
try {
  await User.conn(connection).execute(
    'DELETE FROM `users` WHERE `user_id` = ?',
    [1],
  );
  await User.commit(connection);
} catch (error) {
  await User.rollback(connection);
  throw error;
}

Committed or rolled-back connections are always released to the pool, including error paths.

SQL Logging

Set showSql to a function to receive formatted SQL statements:

const adapter = new MySQLAdapter({
  database: 'app',
  showSql: (sql) => console.log(sql),
});

The Adapter also emits a sql event for every statement and a log event when the pool creates a connection. Passwords and injected pool objects are not retained in the public options property.

SQL helpers

The SQL builder provides makeWhere, makeFind, $eq, $neq, $in, $between, $and, and $or. Raw update expressions such as {{score + 1}} are also supported, but they must contain trusted application-owned SQL rather than user input.

Real MySQL 5.7 and MySQL 8.4 integration tests run in GitHub Actions. The local test suite uses a Promise Pool contract double and does not require Docker.

License

MIT