Skip to content

svtslv/nestjs-ioredis

Repository files navigation

NestJS IORedis

NPM Version Package License

Table of Contents

Description

Integrates IORedis with Nest

Installation

npm install @svtslv/nestjs-ioredis ioredis
npm install -D @types/ioredis

You can also use the interactive CLI

npx nestjs-modules

Examples

docker run -p 6379:6379 redis

RedisModule.forRoot(options, connection?)

import { Module } from '@nestjs/common';
import { RedisModule } from '@svtslv/nestjs-ioredis';
import { AppController } from './app.controller';

@Module({
  imports: [
    RedisModule.forRoot({
      config: { 
        // host: 'localhost',
        // port: 6379,
        url: 'redis://localhost:6379',
      },
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

RedisModule.forRootAsync(options, connection?)

import { Module } from '@nestjs/common';
import { RedisModule } from '@svtslv/nestjs-ioredis';
import { AppController } from './app.controller';

@Module({
  imports: [
    RedisModule.forRootAsync({
      useFactory: () => ({
        config: { 
          // host: 'localhost',
          // port: 6379,
          url: 'redis://localhost:6379',
        },
      }),
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

InjectRedis(connection?)

import { Controller, Get, } from '@nestjs/common';
import { InjectRedis, Redis } from '@svtslv/nestjs-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 };
  }
}

License

MIT