0.1.3 • Published 5 years ago

@mobizerg/nest-ioredis v0.1.3

Weekly downloads
18
License
MIT
Repository
github
Last release
5 years ago

Installation

Yarn

yarn add @mobizerg/nest-ioredis ioredis
yarn add @types/ioredis --dev

NPM

npm install @mobizerg/nest-ioredis ioredis --save
npm install @types/ioredis --save-dev

Description

Redis integration module for Nest.js based on the Ioredis package.

Usage

Import the IoredisModule in app.module.ts

import { Module } from '@nestjs/common';
import { IoredisModule } from '@mobizerg/nest-ioredis';

@Module({
    imports: [
        IoredisModule.register(options);
    ],
})
export class AppModule {}

With Async

import { Module } from '@nestjs/common';
import { IoredisModule } from '@mobizerg/nest-ioredis';

@Module({
    imports: [
        IoredisModule.registerAsync({
            imports: [ConfigModule],
            useExisting: IoredisConfigService,
        }),
    ],
})
export class AppModule {}

Example config file (async)

import { Injectable } from '@nestjs/common';
import { ConfigService } from './config.service';
import { IoredisModuleOptions, IoredisOptionsFactory } from '@mobizerg/nest-ioredis';

@Injectable()
export class IoredisConfigService implements IoredisOptionsFactory {

  constructor(private readonly config: ConfigService) {}

  createIoredisOptions(name?: string): IoredisModuleOptions {
      
    return {
      name,
      host: this.config.redisHost,
      port: this.config.redisPort,
      db: this.config.redisDatabase,
      keyPrefix: this.config.redisPrefix,
    };
  }
}

Importing inside services

import { Injectable } from '@nestjs/common';
import { InjectRedis } from '@mobizerg/nest-ioredis';
import { Redis } from 'ioredis';
import { serialize } from 'class-transformer';

@Injectable()
export class IoredisService {

  constructor(@InjectRedis(REDIS_CLIENT_NAME)
              private readonly client: Redis) {}

  async publish<T>(event: string, payload: T): Promise<number> {
    return await this.client.publish(event, serialize(payload));
  }
}

License

MIT