1.0.1-alpha • Published 11 months ago

nestjs-throttler-storage-mongo v1.0.1-alpha

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Mongo storage provider for the @nestjs/throttler package.

npm version npm download

MongoDB Storage Provider for @nestjs/throttler

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

Usage

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 {}

Inject another config module and service:

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 {}

Issues

Bugs and features related to the mongo implementation are welcome in this repository.

License

NestJS Throttler Mongo Storage is licensed under the MIT license.