UsefulKey

Postgres

Postgres-backed fixed window and token bucket.

Overview

  • Postgres-backed rate limit store implementing fixed windows and a rolling token bucket.
  • Creates the required tables and indexes if they do not exist.

Usage

import { Client } from "pg";
import { PostgresRateLimitStore, usefulkey } from "betterkey";

const client = new Client({ connectionString: process.env.DATABASE_URL });
await client.connect();

const rateLimitStore = new PostgresRateLimitStore(client, {
  tableName: "usefulkey_rate_limits",
});

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

Client compatibility

  • Any pg-like client exposing query(sql, params?).

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 BIGINT 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 DOUBLE PRECISION NOT NULL,
  lastRefill BIGINT NOT NULL,
  capacity INTEGER NOT NULL,
  refillTokens DOUBLE PRECISION NOT NULL,
  refillIntervalMs BIGINT NOT NULL,
  PRIMARY KEY(namespace, identifier)
);
CREATE INDEX IF NOT EXISTS idx_usefulkey_rate_limits_buckets_lastRefill ON usefulkey_rate_limits_buckets(lastRefill);