Skip to content
Roman edited this page Mar 20, 2025 · 8 revisions

RateLimiterSQLite

It supports sqlite3, better-sqlite3 and knex. Set storeType option to make it work with better-sqlite3 or knex.

const sqlite3 = require('sqlite3').verbose();
const { RateLimiterSQLite } = require('rate-limiter-flexible');

const db = new sqlite3.Database('./rate_limiter.sqlite')
const opts = {
  storeClient: db,
  tableName: 'rate_limiter_flexible',
  points: 5, // Number of points
  duration: 1, // Per second(s)
};

const rateLimiter = new RateLimiterSQLite(opts)
rateLimiter.consume(userEmail, 2)
  .then((rateLimiterRes) => {
    // 2 points consumed
  })
  .catch((rej) => {
    // there are no more points to consume
  });

Check rateLimiterRes object description.

Async SQLite table creation

SQLite requires a rate limiter table be created before the limiter instance can be used. You can provide a callback as the second constructor parameter when creating a new instance: new RateLimiterMySQL(opts, ready). Once the table is created the ready callback will be called.

Alternatively, you can create a table for rate limiter before limiter initialization, then set tableCreated rate limiter option to true.

Clone this wiki locally