fastify / fastify-redis

Plugin to share a common Redis connection across Fastify.
MIT License
198 stars 31 forks source link

Include option to choose client creation method (node-redis or ioredis) #175

Closed rhighs closed 1 year ago

rhighs commented 1 year ago

Prerequisites

πŸš€ Feature Proposal

Since ioredis supports node-redis configuration objects it would be nice to include a plugin option that enables developers to create a client via ioredis createClient(...) rather than new Redis()

Motivation

Compatibility with node-redis config objects, although for the sake of clarity it might just be fine to pass a ready made ioredis client to fastify-redis at init time.

Example

const fastifyRedis = require('@fastify/redis') 

const nodeRedisConfig = {
  socket: {
    host: '127.0.0.1',
    port: 6379, // Redis port
    family: 4   // 4 (IPv4) or 6 (IPv6) 
  }
  password: '***'
}

fastify.register(require('@fastify/redis'), { 
  ...nodeRedisConfig
  clientCreationMethod: fastifyRedis.creationMethod.NODE_REDIS
})
// fastify-redis/indes.js
....

module.exports = fp(fastifyRedis, {
  fastify: '4.x',
  name: '@fastify/redis'
})
module.exports.default = fastifyRedis
module.exports.fastifyRedis = fastifyRedis

module.exports.creationMethod = {
  NODE_REDIS: "node-redis",
  IOREDIS: "io-redis"
}
climba03003 commented 1 year ago

Why can't you create yourself?

const ioredis = require('ioredis')
const nodeRedisConfig = {
  socket: {
    host: '127.0.0.1',
    port: 6379, // Redis port
    family: 4   // 4 (IPv4) or 6 (IPv6) 
  },
  password: '***'
}

fastify.register(require('@fastify/redis'), { 
  client: ioredis.createClient({
    ...nodeRedisConfig,
    creationMethod: 'node-redis'
  })
})

https://github.com/luin/ioredis/blob/92aefaac13f42ad14986fde47c640ff8edc415ef/lib/Redis.ts#L76-L78 Given ioredis.createClient is a wrapper of new Redis. I didn't see any special we need to done.

rhighs commented 1 year ago

Yep, silly me it's way better like this, closing... Thank you! @climba03003