nestjs-redis-connection

1.0.0 • Public • Published

nestjs-redis

config redis by nestjs/config

  1. based on node-redis connection options RedisClientOptions
  RedisConfig: [
    {
      //redis[s]://[[username][:password]@][host][:port][/db-number]:
      url: 'redis://127.0.0.1:6379',
      name: 'test',
    },
  ],
  1. register redis module by static function register
RedisConnectionModule.register({
  configKey: 'RedisConfig',
  errorHandler: (error) => {
    // err handler
  }
}),
  1. get redis client by redisConnectionService
@Injectable()
export class UserService {
  @Inject()
  private readonly redisConnectionService: RedisConnectionService;

  async getcache() {
    const redisClient = this.redisConnectionService.getClient('test');
    return await redisClient.get('xx')
  }
}
  1. enable nestjs shutdownhooks, so that the destory hooks of redis connection module can exe .
  app.enableShutdownHooks();

problems

  1. if u use webpack bundle you project, plz compile node-redis by babel like this.
    rules: [
      {
        test: /\.m?js$/,
        include: [
          path.join(__dirname, 'node_modules/redis'),
          path.join(__dirname, 'node_modules/@redis'),
        ],
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          },
        },
      },
    ]

Package Sidebar

Install

npm i nestjs-redis-connection

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

15.7 kB

Total Files

20

Last publish

Collaborators

  • pengbo-study