0.0.2-alpha.0 • Published 6 months ago

@flowez/nestjs-redis v0.0.2-alpha.0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

NestJS IORedis

Table of Contents

Description

Integrates IORedis with Nest

Installation

npm install @flowez/nestjs-ioredis ioredis

You can also use the interactive CLI

npx nestjs-modules

Examples

Let's register the RedisModule in app.module.ts

import { Module } from '@nestjs/common';
import { RedisModule } from '@flowez/nestjs-ioredis';

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

With Async

import { Module } from '@nestjs/common';
import { RedisModule } from '@flowez/nestjs-ioredis';

@Module({
  imports: [
    RedisModule.forRootAsync({
      useFactory: (configService: ConfigService) => configService.get('redis'), // or use async method
      //useFactory: async (configService: ConfigService) => configService.get('redis'),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

And the config file look like this With single client

export default {
  host: process.env.REDIS_HOST,
  port: parseInt(process.env.REDIS_PORT),
  db: parseInt(process.env.REDIS_DB),
  password: process.env.REDIS_PASSWORD,
  keyPrefix: process.env.REDIS_PRIFIX,
};
Or;
export default {
  url: 'redis://:authpassword@127.0.0.1:6380/4',
};

With custom error handler

export default {
  url: 'redis://:authpassword@127.0.0.1:6380/4',
  onClientReady: (client) => {
    client.on('error', (err) => {});
  },
};

With multi client

export default [
  {
    clientName: 'test1',
    url: 'redis://:authpassword@127.0.0.1:6380/4',
  },
  {
    clientName: 'test2',
    host: process.env.REDIS_HOST,
    port: parseInt(process.env.REDIS_PORT),
    db: parseInt(process.env.REDIS_DB),
    password: process.env.REDIS_PASSWORD,
    keyPrefix: process.env.REDIS_PRIFIX,
  },
];

And use in your service

import { Injectable } from '@nestjs/common';
import { RedisService } from '@flowez/nestjs-ioredis';

@Injectable()
export class TestService {
  constructor(private readonly redisService: RedisService) {}
  async root(): Promise<boolean> {
    const client = await this.redisService.getClient('test');
    return true;
  }
}

That's it!

License

MIT

0.0.2-alpha.0

6 months ago

0.0.1-alpha.2

1 year ago

0.0.1-alpha.3

1 year ago

0.0.1-beta.0

1 year ago

0.0.1-alpha.0

2 years ago