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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ npm i fastify-redis --save
```
## Usage
Add it to your project with `register` and you are done!
You can access the *Redis* client via `fastify.redis`.
You can access the *Redis* client via `fastify.redis`. The client is
automatically closed when the fastify instance is closed.

```js
const fastify = require('fastify')
Expand Down Expand Up @@ -40,7 +41,9 @@ fastify.listen(3000, err => {
```

You may also supply an existing *Redis* client instance by passing an options
object with the `client` property set to the instance.
object with the `client` property set to the instance. In this case,
the client is not automatically closed when the Fastify instance is
closed.

```js
const fastify = Fastify()
Expand Down
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ function fastifyRedis (fastify, options, next) {
} catch (err) {
return next(err)
}
fastify.addHook('onClose', close)
}

fastify
.decorate('redis', client)
.addHook('onClose', close)
fastify.decorate('redis', client)

next()
}
Expand Down
9 changes: 7 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ test('promises support', t => {
})

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

Expand All @@ -100,7 +100,12 @@ test('custom client', t => {
t.error(err)
t.equal(val, 'value')

fastify.close()
fastify.close(function (err) {
t.error(err)
fastify.redis.quit(function (err) {
t.error(err)
})
})
})
})
})
Expand Down