Skip to content

Commit 6f5071c

Browse files
committed
Allow specifying a singleton Redis instance
1 parent 92bca63 commit 6f5071c

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Under the hood the official [redis](https://github.com/NodeRedis/node_redis) cli
1111
npm i fastify-redis --save
1212
```
1313
## Usage
14-
Add it to you project with `register` and you are done!
14+
Add it to your project with `register` and you are done!
1515
You can access the *Redis* client via `fastify.redis`.
1616

1717
If needed, you can pass a custom ``driver`` option, such as [ioredis](https://github.com/luin/ioredis). By default the official [redis](https://github.com/NodeRedis/node_redis) client is used.
@@ -47,6 +47,20 @@ fastify.listen(3000, err => {
4747
})
4848
```
4949

50+
You may also supply an existing *Redis* client instance by passing an options
51+
object with the `client` property set to the instance.
52+
53+
```js
54+
const fastify = Fastify()
55+
const redis = require('redis').createClient({host: 'localhost', port: 6379})
56+
57+
fastify.register(fastifyRedis, {client: redis})
58+
59+
// ...
60+
// ...
61+
// ...
62+
```
63+
5064
## Acknowledgements
5165

5266
This project is kindly sponsored by:

index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ const fp = require('fastify-plugin')
44
const redis = require('redis')
55

66
function fastifyRedis (fastify, options, next) {
7-
var client = null
8-
try {
9-
// if custom redis module, default is redis.
10-
const Driver = options.driver
11-
delete options.driver
12-
client = Driver ? new Driver(options) : redis.createClient(options)
13-
} catch (err) {
14-
return next(err)
7+
var client = options.client || null
8+
9+
if (!client) {
10+
try {
11+
// if custom redis module, default is redis.
12+
const Driver = options.driver
13+
delete options.driver
14+
client = Driver ? new Driver(options) : redis.createClient(options)
15+
} catch (err) {
16+
return next(err)
17+
}
1518
}
1619

1720
fastify

test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,26 @@ test('fastify.redis should be the redis client when use the custom redis driver'
110110
})
111111
})
112112
})
113+
114+
test('fastify.redis should be a singleton', t => {
115+
t.plan(5)
116+
const fastify = Fastify()
117+
const redis = require('redis').createClient({host: 'localhost', port: 6379})
118+
119+
fastify.register(fastifyRedis, {client: redis})
120+
121+
fastify.ready(err => {
122+
t.error(err)
123+
t.is(fastify.redis, redis)
124+
125+
fastify.redis.set('key', 'value', err => {
126+
t.error(err)
127+
fastify.redis.get('key', (err, val) => {
128+
t.error(err)
129+
t.equal(val, 'value')
130+
131+
fastify.close()
132+
})
133+
})
134+
})
135+
})

0 commit comments

Comments
 (0)