Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nestjs-modules/ioredis

nest-modules188.2kMIT2.0.2TypeScript support: included

Nest - a ioredis module (@ioredis)

nest, nestjs, nest-modules, nestjs-modules, redis, ioredis

readme

Nest Logo

A ioredis module for Nest framework (node.js) using ioredis library

NPM Version Package License NPM Downloads

Installation

with npm

npm install --save @nestjs-modules/ioredis ioredis

with yarn

yarn add @nestjs-modules/ioredis ioredis

How to use?

RedisModule.forRoot(options, connection?)

Single Type (forRoot)
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-modules/ioredis';
import { AppController } from './app.controller';

@Module({
  imports: [
    RedisModule.forRoot({
      type: 'single',
      url: 'redis://localhost:6379',
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}
Cluster Type (forRoot)
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-modules/ioredis';
import { AppController } from './app.controller';

@Module({
  imports: [
    RedisModule.forRoot({
      type: 'cluster',
      nodes: [
        {
          host: '127.0.0.1',
          port: 6379
        },
        {
          host: '127.0.0.2',
          port: 6379
        }
      ],
      options: {
        redisOptions: {
          password: '123456'
        }
      }
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

RedisModule.forRootAsync(options, connection?)

Single Type (forRootAsync)
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-modules/ioredis';
import { AppController } from './app.controller';

@Module({
  imports: [
    RedisModule.forRootAsync({
      useFactory: () => ({
        type: 'single',
        url: 'redis://localhost:6379',
      }),
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}
Cluster Type (forRootAsync)
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-modules/ioredis';
import { AppController } from './app.controller';

@Module({
  imports: [
    RedisModule.forRootAsync({
      useFactory: () => ({
        type: 'cluster',
        nodes: [
          {
            host: '127.0.0.1',
            port: 6379
          },
            {
            host: '127.0.0.2',
            port: 6379
          }
        ],
        options: {
          redisOptions: {
            password: '123456'
          }
        }
      }),
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

InjectRedis(connection?)

import Redis from 'ioredis';
import { Controller, Get } from '@nestjs/common';
import { InjectRedis } from '@nestjs-modules/ioredis';

@Controller()
export class AppController {
  constructor(
    @InjectRedis() private readonly redis: Redis,
  ) {}

  @Get()
  async getHello() {
    await this.redis.set('key', 'Redis data!');
    const redisData = await this.redis.get("key");
    return { redisData };
  }
}

How to use the Redis indicator for the Terminus library?"

//health.module.ts
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { RedisHealthModule, } from '@nestjs-modules/ioredis';

@Module({
  imports: [TerminusModule, RedisHealthModule],
  controllers: [HealthController]
})
export class HealthModule {}
//health.controller.ts
import { Controller, Get } from '@nestjs/common';
import {
  HealthCheckService,
  HealthCheck,
  HealthCheckResult
} from '@nestjs/terminus';
import { RedisHealthIndicator } from './redis.health';

@Controller('health')
export class HealthController {
  constructor(
    private health: HealthCheckService,
    private redis: RedisHealthIndicator,
  ) {}

  @Get()
  @HealthCheck()
  check(): Promise<HealthCheckResult> {
    return this.health.check([
      async () => this.redis.isHealthy('redis'),
    ]);
  }
}

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.0.2 (2024-02-24)

Bug Fixes

2.0.1 (2023-12-22)

Bug Fixes

  • The 'port' and 'host' fields should no longer appear as mandatory (a2d519a)

2.0.0 (2023-12-22)

Features

  • add cluster support and foo samples (ad220d7)

Bug Fixes

  • Add two Redis instance options, one when a URL exists and one when it doesn't (f5d1819)

1.2.4 (2023-12-21)

Bug Fixes

  • correct the documentation of the library regarding the health indicator (c2ec01d)

1.2.3 (2023-12-21)

1.2.2 (2023-12-21)

Bug Fixes

  • correct the use of the redis health indicator (8f61004)

1.2.1 (2023-12-21)

1.2.0 (2023-12-21)

Features

  • add redis health indicator (ffc2ad7)

1.1.2 (2023-12-21)

1.1.1 (2023-12-20)

Bug Fixes

1.1.0 (2023-12-20)

1.0.1 (2022-06-19)

Features

  • add ioredis nestjs module (fd1acfc)

Bug Fixes

  • the connection parameter should be optional (3825c0c)
  • upgrade to latest version (d4c5842)