Giter Site home page Giter Site logo

nest-ioredis's Introduction

Nest Logo

A Ioredis integration module for Nest.js framework.

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

nest-ioredis's People

Contributors

frozenex avatar mobizerg avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

alex86w

nest-ioredis's Issues

Working example?

I tried following the steps for the async setup along with creating an injectable service, but I couldn't sort out how to make the IosredisService available for my other NestJS modules. Is it something the IosredisModule should export? Do you have a working example where other modules rely on the service?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.