Skip to content

Commit 67a63d8

Browse files
authored
Merge pull request #9 from wotermelon/master
to support ioredis.
2 parents 50a553a + 75d4c87 commit 67a63d8

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
44

55
Fastify Redis connection plugin, with this you can share the same Redis connection in every part of your server.
66

7-
Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the options that you pass to `register` will be passed to the Redis client.
7+
Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the ``options`` that you pass to `register` will be passed to the Redis client.
88

99
## Install
1010
```
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 you project with `register` and you are done!
1515
You can access the *Redis* client via `fastify.redis`.
16+
17+
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.
18+
19+
1620
```js
1721
const fastify = require('fastify')
1822

1923
fastify.register(require('fastify-redis'), {
24+
driver: require('ioredis'),
2025
host: '127.0.0.1'
2126
}, err => {
2227
if (err) throw err

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ const redis = require('redis')
55

66
function fastifyRedis (fastify, options, next) {
77
var client = null
8-
98
try {
10-
client = redis.createClient(options)
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)
1113
} catch (err) {
1214
return next(err)
1315
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"homepage": "https://github.com/fastify/fastify-redis#readme",
2727
"devDependencies": {
2828
"fastify": "^0.30.2",
29+
"ioredis": "^3.2.1",
2930
"standard": "^10.0.3",
3031
"tap": "^10.7.2"
3132
},

test.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ t.beforeEach(done => {
2727
test('fastify.redis should exist', t => {
2828
t.plan(3)
2929
const fastify = Fastify()
30-
3130
fastify.register(fastifyRedis, {
3231
host: '127.0.0.1'
3332
}, (err) => {
@@ -66,3 +65,48 @@ test('fastify.redis should be the redis client', t => {
6665
})
6766
})
6867
})
68+
69+
test('fastify.redis should exist when use the custom redis driver', t => {
70+
t.plan(3)
71+
const fastify = Fastify()
72+
73+
fastify.register(fastifyRedis, {
74+
driver: require('ioredis'),
75+
host: '127.0.0.1'
76+
}, (err) => {
77+
t.error(err)
78+
})
79+
80+
fastify.ready(err => {
81+
t.error(err)
82+
t.ok(fastify.redis)
83+
84+
fastify.close()
85+
})
86+
})
87+
88+
test('fastify.redis should be the redis client when use the custom redis driver', t => {
89+
t.plan(5)
90+
const fastify = Fastify()
91+
92+
fastify.register(fastifyRedis, {
93+
driver: require('ioredis'),
94+
host: '127.0.0.1'
95+
}, (err) => {
96+
t.error(err)
97+
})
98+
99+
fastify.ready(err => {
100+
t.error(err)
101+
102+
fastify.redis.set('key', 'value', err => {
103+
t.error(err)
104+
fastify.redis.get('key', (err, val) => {
105+
t.error(err)
106+
t.equal(val, 'value')
107+
108+
fastify.close()
109+
})
110+
})
111+
})
112+
})

0 commit comments

Comments
 (0)