Mongo storage provider for the @nestjs/throttler package.
This package provides a MongoDB TTL (Time-to-Live) storage provider for the @nestjs/throttler package . It allows you to store rate limiter data in a MongoDB collection with automatic expiration of entries using TTL indexes.
Installation Install the package via npm:
npm install nestjs-throttler-storage-mongo
Yarn
yarn add nestjs-throttler-storage-mongo
To start using NestJS MongoDB Storage Provider, you need to import the ThrottlerStorageMongoService into your application module and configure it with your Mongo connection url and MongoClientOptions.
import { ThrottlerModule } from '@nestjs/throttler';
import { ThrottlerStorageMongoService } from 'nestjs-throttler-storage-mongo';
@Module({
imports: [
ThrottlerModule.forRoot({
ttl: 60,
limit: 5,
// Below are possible options on how to configure the storage service.
// connection url
storage: new ThrottlerStorageMongoService('mongodb://'),
// MongoDB connection string with connection options
storage: new ThrottlerStorageMongoService('mongodb://',{
useNewUrlParser: true,
useUnifiedTopology: true,
// Other connection options
}
),
}),
],
})
export class AppModule {}
import { ThrottlerModule } from '@nestjs/throttler';
import { ThrottlerStorageMongoService } from 'nestjs-throttler-storage-mongo';
@Module({
imports: [
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
ttl: config.get('THROTTLE_TTL'),
limit: config.get('THROTTLE_LIMIT'),
storage: new ThrottlerStorageMongoService('mongodb://'),
}),
}),
],
})
export class AppModule {}
Bugs and features related to the mongo implementation are welcome in this repository.
NestJS Throttler Mongo Storage is licensed under the MIT license.