UsefulKey

SQLite

File-backed rate limit store.

Overview

  • SQLite-backed rate limit store implementing fixed windows and token bucket.
  • Targets a better-sqlite3-like API and creates required tables/indexes on first use.

Usage

import Database from "better-sqlite3";
import { SqliteRateLimitStore, usefulkey } from "betterkey";

const db = new Database("usefulkey.db");

const rateLimitStore = new SqliteRateLimitStore(db, {
  tableName: "usefulkey_rate_limits",
});

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

Client compatibility

  • Any object exposing prepare(sql) returning { run(...), get(...) }.

Options

OptionTypeDefaultDescription
tableNamestring"usefulkey_rate_limits"Base table name for fixed windows (bucket table is suffixed with _buckets).

Tables

CREATE TABLE IF NOT EXISTS usefulkey_rate_limits (
  namespace TEXT NOT NULL,
  identifier TEXT NOT NULL,
  count INTEGER NOT NULL,
  reset INTEGER NOT NULL,
  PRIMARY KEY(namespace, identifier)
);
CREATE INDEX IF NOT EXISTS idx_usefulkey_rate_limits_reset ON usefulkey_rate_limits(reset);

CREATE TABLE IF NOT EXISTS usefulkey_rate_limits_buckets (
  namespace TEXT NOT NULL,
  identifier TEXT NOT NULL,
  tokens REAL NOT NULL,
  lastRefill INTEGER NOT NULL,
  capacity INTEGER NOT NULL,
  refillTokens REAL NOT NULL,
  refillIntervalMs INTEGER NOT NULL,
  PRIMARY KEY(namespace, identifier)
);
CREATE INDEX IF NOT EXISTS idx_usefulkey_rate_limits_buckets_lastRefill ON usefulkey_rate_limits_buckets(lastRefill);