-
-
Notifications
You must be signed in to change notification settings - Fork 183
SQLite
Roman edited this page Mar 20, 2025
·
8 revisions
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.
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.
Get started
Middlewares and plugins
Migration from other packages
Limiters:
- Cluster
- Drizzle
- DynamoDB
- Etcd
- Memcached
- Memory
- MongoDB (with sharding support)
- MySQL
- PM2 Cluster
- PostgreSQL
- Prisma
- Redis
- SQLite
- Valkey: iovalkey and Valkey Glide
- BurstyRateLimiter
- RateLimiterUnion
- RateLimiterQueue
Wrappers:
- AWS SDK v3 Client Rate Limiter
- RLWrapperBlackAndWhite Black and White lists
- RLWrapperTimeouts Timeouts
Knowledge base:
- Block Strategy in memory
- Insurance Strategy
- Periodic sync to reduce number of requests
- Comparative benchmarks
- Smooth out traffic peaks
-
Usage example
- Minimal protection against password brute-force
- Login endpoint protection
- Websocket connection prevent flooding
- Dynamic block duration
- Different limits for authorized users
- Different limits for different parts of application
- Block Strategy in memory
- Insurance Strategy
- Third-party API, crawler, bot rate limiting