Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,17 @@ const fastify = require('fastify')()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })

fastify.register(require('fastify-redis'), { client: redis })
```

Note: by default, *fastify-redis* will **not** automatically close the client
connection when the Fastify server shuts down. To opt-in to this behavior,
register the client like so:

// ...
// ...
// ...
```js
fastify.register(require('fastify-redis'), {
client: redis,
closeClient: true
})
```

## Registering multiple Redis client instances
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ function fastifyRedis (fastify, options, next) {
}

fastify.decorate('redis', client)
if (options.closeClient === true) {
fastify.addHook('onClose', close)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would need to support when this is false in all cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that. The issue is around passing in a specific client and managing that outside instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, if somebody is not passing a client in and closeClient: false, then closing the client seems off.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unclear why they would do that or why it would even be supported. They wouldn't be able to manage it without first pulling the instance from Fastify.

}
}

Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,37 @@ test('custom client', (t) => {
})
})

test('custom client gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })

fastify.register(fastifyRedis, { client: redis, closeClient: true })

fastify.ready((err) => {
t.error(err)
t.is(fastify.redis, redis)

fastify.redis.set('key', 'value', (err) => {
t.error(err)
fastify.redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')

const origQuit = fastify.redis.quit
fastify.redis.quit = (cb) => {
t.pass('redis client closed')
origQuit.call(fastify.redis, cb)
}

fastify.close(function (err) {
t.error(err)
})
})
})
})
})

test('custom client inside a namespace', (t) => {
t.plan(7)
const fastify = Fastify()
Expand Down