Skip to content

Configuration ​

Plugin Options ​

OptionTypeDefaultDescription
clientstring'default'Named Redis client to use
keyPrefixstring'_ctr:'Prefix for counter keys (must be non-empty)

The plugin is deliberately thin - behavior lives in per-call options.

Synchronous Setup ​

typescript
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-redisx/core';
import { CountersPlugin } from '@nestjs-redisx/counters';

@Module({
  imports: [
    RedisModule.forRoot({
      clients: {
        host: 'localhost',
        port: 6379,
      },
      plugins: [
        new CountersPlugin({
          keyPrefix: '_ctr:', // default
        }),
      ],
    }),
  ],
})
export class AppModule {}

Async Registration ​

typescript
CountersPlugin.registerAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({
    keyPrefix: config.get<string>('COUNTERS_PREFIX') ?? '_ctr:',
  }),
}),

Per-Call Options ​

OptionApplies toDescription
ttlincrement, decrement, setFor mutations: window TTL applied only when the mutation creates the key. For set: a plain key TTL

Validation Rules (fail-fast) ​

InputRule
key / sequenceNon-empty string
amount (by)Non-zero safe integer (negatives fine)
value (set)Safe integer
ttlPositive integer milliseconds

Violations throw InvalidCounterConfigError before any Redis round-trip - Number(unsetEnvVar) (NaN) is reported truthfully rather than corrupting a counter.

Released under the MIT License.