UsefulKey

MySQL

MySQL-backed keystore adapter.

Overview

  • MySQL-backed keystore targeting a minimal query(sql, values?) API (e.g., mysql2/promise).
  • Creates the table and indexes if they do not exist.
  • metadata is stored as JSON string in a TEXT column.

Usage

import mysql from "mysql2/promise";
import { MysqlKeyStore, usefulkey } from "betterkey";

const pool = mysql.createPool({ uri: process.env.DATABASE_URL! });

const keyStore = new MysqlKeyStore(
  { query: (sql, values) => pool.query(sql, values) },
  { tableName: "usefulkey_keys" },
);

const uk = usefulkey({ adapters: { keyStore } });

Client compatibility

  • Any MySQL client exposing query(sql, params?) and returning either [rows, fields] or an object with rows.

Options

OptionTypeDefaultDescription
tableNamestring"usefulkey_keys"Name of the table to store keys.

Schema used by the adapter (created automatically if missing):

CREATE TABLE IF NOT EXISTS usefulkey_keys (
  id VARCHAR(255) PRIMARY KEY,
  user_id VARCHAR(255) NULL,
  prefix VARCHAR(64) NOT NULL,
  key_hash VARCHAR(255) NOT NULL UNIQUE,
  created_at BIGINT NOT NULL,
  expires_at BIGINT NULL,
  metadata TEXT NULL,
  uses_remaining INT NULL,
  revoked_at BIGINT NULL,
  KEY idx_usefulkey_keys_user_id (user_id),
  UNIQUE KEY idx_usefulkey_keys_key_hash (key_hash)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Notes

  • metadata is stored as serialized JSON text.
  • created_at, expires_at, and revoked_at are epoch milliseconds.