SQLite Store

The SQLite store provides persistent rate limit storage using a local SQLite database. Perfect for single-server deployments that need data to survive restarts.

Installation

The SQLite store requires better-sqlite3 as a peer dependency:

npm install better-sqlite3
pnpm add better-sqlite3
yarn add better-sqlite3
bun add better-sqlite3

Usage

import { hitlimit } from '@joint-ops/hitlimit'
import { sqliteStore } from '@joint-ops/hitlimit/stores/sqlite'

app.use(hitlimit({
  limit: 100,
  window: '1m',
  store: sqliteStore({
    path: './rate-limits.db'
  })
}))

Options

OptionTypeDefaultDescription
pathstring':memory:'Path to SQLite database file
tableNamestring'hitlimit'Name of the rate limit table
cleanupIntervalnumber60000Interval (ms) to clean expired entries

Full Configuration

sqliteStore({
  path: './data/rate-limits.db',
  tableName: 'api_limits',
  cleanupInterval: 300000  // Clean every 5 minutes
})

Characteristics

  • Speed: Fast - local file I/O with WAL mode
  • Persistence: Data survives restarts
  • Scalability: Single server only
  • Dependencies: Requires better-sqlite3

When to Use

  • Single-server production deployments
  • When rate limits must persist across restarts
  • Serverless functions with persistent storage
  • Edge deployments where Redis is not available

Database Schema

The store automatically creates the following table:

CREATE TABLE IF NOT EXISTS hitlimit (
  key TEXT PRIMARY KEY,
  count INTEGER NOT NULL,
  reset_at INTEGER NOT NULL
)