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

@abinashpatri/mysql

v1.0.4

Published

Production-grade mysql utility library

Readme

@abinashpatri/mysql

Production-focused MySQL utility helpers for Node.js applications using mysql2/promise.

Why this package

This package gives you a small, practical MySQL toolkit with:

  • pooled connections per database key
  • built-in retry for transient query failures
  • transaction helper with auto rollback
  • health checks for one or many configured databases
  • TypeScript-ready APIs and declaration files

Features

  • Multi-database configuration
    Define multiple database connections (for example primary, analytics, readReplica) and query each by name.

  • Connection pooling
    Uses mysql2 pool with sensible defaults (waitForConnections, connectionLimit, queueLimit, keep-alive, timeout).

  • Retry support for queries
    query() supports retry options: retries, delay, and exponential factor.

  • Slow query warning
    Logs a warning when query execution takes longer than 500ms.

  • Safe transaction wrapper
    transaction() automatically:

    • begins transaction
    • commits on success
    • rolls back on error
    • releases connection in all cases
  • Health checks
    Check one DB (checkHealth) or all configured DBs (checkAllHealth).

Install

npm install @abinashpatri/mysql mysql2

Quick start

import { mysql } from "@abinashpatri/mysql";

mysql.setDatabaseConfig({
  primary: {
    host: "127.0.0.1",
    port: 3306,
    user: "root",
    password: "secret",
    database: "app_db",
  },
});

const rows = await mysql.query("primary", "SELECT 1 AS ok");
console.log(rows);

Complete demo

import { mysql } from "@abinashpatri/mysql";
import type { RowDataPacket } from "mysql2";

// 1) Configure one or more databases
mysql.setDatabaseConfig({
  primary: {
    host: "127.0.0.1",
    port: 3306,
    user: "root",
    password: "secret",
    database: "app_db",
    connectionLimit: 20,
    queueLimit: 100,
    connectTimeout: 10000,
  },
  analytics: {
    host: "127.0.0.1",
    user: "analytics_user",
    password: "analytics_secret",
    database: "analytics_db",
  },
});

// 2) Typed SELECT query
type UserRow = RowDataPacket & {
  id: number;
  email: string;
  active: number;
};

const users = await mysql.query<UserRow[]>(
  "primary",
  "SELECT id, email, active FROM users WHERE active = ?",
  [1],
);

// 3) Insert with retry strategy
await mysql.query(
  "primary",
  "INSERT INTO audit_logs(action, created_at) VALUES(?, NOW())",
  ["login"],
  {
    retry: {
      retries: 3,
      delay: 200,
      factor: 2,
    },
  },
);

// 4) Transaction example
await mysql.transaction("primary", async (conn) => {
  await conn.execute(
    "UPDATE wallets SET balance = balance - ? WHERE user_id = ?",
    [100, 1],
  );
  await conn.execute(
    "UPDATE wallets SET balance = balance + ? WHERE user_id = ?",
    [100, 2],
  );
});

// 5) Health checks
const primaryHealth = await mysql.checkHealth("primary");
const allHealth = await mysql.checkAllHealth();
console.log({ primaryHealth, allHealth });

API reference

setDatabaseConfig(configs)

Registers one or more DB configurations.

mysql.setDatabaseConfig({
  primary: {
    host: "127.0.0.1",
    user: "root",
    password: "secret",
    database: "app_db",
  },
});

query(dbName, sql, params?, options?)

Executes a query using the named pool.

  • dbName: string - configured database key
  • sql: string - SQL statement
  • params?: unknown[] - query parameters
  • options?: { retry?: { retries?: number; delay?: number; factor?: number } }

Returns a promise of query rows (generic type supported).

transaction(dbName, callback)

Runs callback inside a transaction with auto commit/rollback.

  • dbName: string
  • callback: (conn) => Promise<T>

Returns the callback result T.

checkHealth(dbName)

Checks single DB by running SELECT 1.

Returns:

{
  db: string;
  status: "healthy" | "unhealthy";
  error?: string;
}

checkAllHealth()

Checks all configured DBs and returns an array of health statuses.

Scripts

  • npm run build - Build CJS, ESM, and declaration files.
  • npm run typecheck - Run TypeScript checks (tsc --noEmit).
  • npm run dev - Watch mode build.

Production notes

  • Call setDatabaseConfig() once during app startup before first query.
  • Keep DB credentials in environment variables (do not hardcode secrets).
  • Use separate DB users with least privilege for each environment.
  • Keep retries conservative to avoid amplifying load during outages.

License

MIT