typeorm-rate-limit-store
v1.1.1
Published
A typeOrm store for the `express-rate-limit` middleware
Downloads
188
Readme
rate-limit-typeorm
A TypeORM
store for express-rate-limit
.
Designed for use with TypeORM as a backing store for express rate limiting in a clustered environment.
Installation
npm install typeorm-rate-limit-store
Usage
You will need to implement an entity for the rate-limiting model, and then instantiate the store by passing in that model.
Implement the IExpressRateLimitModel
entity:
// ExpressRateLimitStoreModel.ts
import { Column, Entity, PrimaryColumn } from "typeorm";
import type {IExpressRateLimitModel} from 'typeorm-rate-limit-store';
@Entity()
export class ExpressRateLimitStoreModel implements IExpressRateLimitModel{
@PrimaryColumn()
public key: string;
@Column()
public totalHits: number;
@Column()
public resetTime: Date;
}
Instantiate the store passing in the model:
import {ExpressRateLimitTypeOrmStore} from 'typeorm-rate-limit-store';
import {ExpressRateLimitStoreModel} from "ExpressRateLimitStoreModel.js";
const repository = getDatastore().getRepository(ExpressRateLimitStoreModel)
app.use(
rateLimit({
windowMs: 1000,
limit: 1,
standardHeaders: true,
store: new ExpressRateLimitTypeOrmStore(repository),
}),
);
Options:
The ExpressRateLimitTypeOrmStore
constructor takes the following parameters:
| Option | Description | Required |
|------------|:--------------------------------------------------------------------------------------------------------------------|----------|
| repository | The type orm repository that is built from the IExpressRateLimitModel
interface | True |
| prefix | A string that will prefix the key, use this if you require multiple instances of the ExpressRateLimitTypeOrmStore
| False |
Both CJS and ESM modules are provided, but it is assumed you will be using ESM as this package exports that as the default.