Skip to content

Commit 8bcb212

Browse files
authored
do not quit given client on close (#25)
* do not quit given client on close * typo
1 parent 4a06e8a commit 8bcb212

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ npm i fastify-redis --save
1212
```
1313
## Usage
1414
Add it to your project with `register` and you are done!
15-
You can access the *Redis* client via `fastify.redis`.
15+
You can access the *Redis* client via `fastify.redis`. The client is
16+
automatically closed when the fastify instance is closed.
1617

1718
```js
1819
const fastify = require('fastify')
@@ -40,7 +41,9 @@ fastify.listen(3000, err => {
4041
```
4142

4243
You may also supply an existing *Redis* client instance by passing an options
43-
object with the `client` property set to the instance.
44+
object with the `client` property set to the instance. In this case,
45+
the client is not automatically closed when the Fastify instance is
46+
closed.
4447

4548
```js
4649
const fastify = Fastify()

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ function fastifyRedis (fastify, options, next) {
1212
} catch (err) {
1313
return next(err)
1414
}
15+
fastify.addHook('onClose', close)
1516
}
1617

17-
fastify
18-
.decorate('redis', client)
19-
.addHook('onClose', close)
18+
fastify.decorate('redis', client)
2019

2120
next()
2221
}

test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ test('promises support', t => {
8484
})
8585

8686
test('custom client', t => {
87-
t.plan(5)
87+
t.plan(7)
8888
const fastify = Fastify()
8989
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
9090

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

103-
fastify.close()
103+
fastify.close(function (err) {
104+
t.error(err)
105+
fastify.redis.quit(function (err) {
106+
t.error(err)
107+
})
108+
})
104109
})
105110
})
106111
})

0 commit comments

Comments
 (0)