Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to register multiple Redis client instances with Typescript #102

Closed
2 tasks done
CloudPower97 opened this issue Jul 29, 2021 · 1 comment · Fixed by #110
Closed
2 tasks done

How to register multiple Redis client instances with Typescript #102

CloudPower97 opened this issue Jul 29, 2021 · 1 comment · Fixed by #110
Labels
bug Confirmed bug good first issue Good for newcomers

Comments

@CloudPower97
Copy link

CloudPower97 commented Jul 29, 2021

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure it has not already been reported

Fastify version

3.19.2

Plugin version

4.3.1

Node.js version

v14.16.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.4

Description

I'm trying to register multiple Redis client instances as follow:

export default fp<FastifyRedisPlugin>(async (fastify) => {
  await fastify
    .register(fastifyRedis, {
      host: fastify.config.REDIS_HOST,
      port: fastify.config.REDIS_PORT,
      password: fastify.config.REDIS_PASSWORD,
      namespace: 'hello',
    })
    .register(fastifyRedis, {
      host: fastify.config.REDIS_HOST,
      port: fastify.config.REDIS_PORT,
      password: fastify.config.REDIS_PASSWORD,
      namespace: 'world',
    });
}

I would expect that, as explained in the docs, I would be able to do something like that:

// Here we will use the `hello` named instance
fastify.get('/hello', (req, reply) => {
  const { redis } = fastify

  redis.hello.get(req.query.key, (err, val) => {
    reply.send(err || val)
  })
})

fastify.post('/hello', (req, reply) => {
  const { redis } = fastify

  redis['hello'].set(req.body.key, req.body.value, (err) => {
    reply.send(err || { status: 'ok' })
  })
})

// Here we will use the `world` named instance
fastify.get('/world', (req, reply) => {
  const { redis } = fastify

  redis['world'].get(req.query.key, (err, val) => {
    reply.send(err || val)
  })
})

But whenever I try to access redis['hello'] TypeScript complains about that with the following error:

Element implicitly has an 'any' type because expression of type '"hello"' can't be used to index type 'Redis'.
  Property 'hello' does not exist on type 'Redis'.

I thought that maybe I would have been able to fix this issue by myself doing that, albeit this is not documented anywhere:

import type { Redis } from 'ioredis'

declare module 'fastify' {
  interface FastifyInstance {
    redis: {
      hello: Redis;
      world: Redis;
    };
  }
}

But this time TypeScript greets me with this error:

Subsequent property declarations must have the same type.  Property 'redis' must be of type 'Redis', but here has type '{ test: Redis; test2: Redis; }'.

So this seems a dead end to me.

Can someone shed a light on this for me?

Thanks in advance.

Steps to Reproduce

Just follow the Registering multiple Redis client instances step in Readme.MD with a project bootstrapped with fastify-cli and TypeScript.

Expected Behavior

I expect that one can register multiple Redis instances and correctly access them, even when using TypeScript.

@Eomm
Copy link
Member

Eomm commented Jul 30, 2021

I think there is a bug in our types

Looking at mongodb, that it should works

https://github.com/fastify/fastify-mongodb/blob/23c460cbe8269ebe36612f9ae7fec5cf3c6df600/index.d.ts#L48

Here, this logic is missing

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

cc @fastify/typescript

@mcollina mcollina added good first issue Good for newcomers bug Confirmed bug labels Jul 30, 2021
@climba03003 climba03003 linked a pull request Sep 25, 2021 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug good first issue Good for newcomers
Projects
None yet
3 participants