Skip to content

Commit 370a5f4

Browse files
authored
Merge pull request #20 from fastify/ioredis
Use ioredis by default
2 parents 39585d5 + 6c5ae24 commit 370a5f4

File tree

5 files changed

+31
-69
lines changed

5 files changed

+31
-69
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: node_js
22

33
node_js:
4+
- "9"
45
- "8"
56
- "6"
6-
- "4"
77

88
services:
99
- redis-server

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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 [ioredis](https://github.com/luin/ioredis) is used as client, the ``options`` that you pass to `register` will be passed to the Redis client.
88

99
## Install
1010
```
@@ -14,18 +14,10 @@ npm i fastify-redis --save
1414
Add it to your project with `register` and you are done!
1515
You can access the *Redis* client via `fastify.redis`.
1616

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-
2017
```js
2118
const fastify = require('fastify')
2219

23-
fastify.register(require('fastify-redis'), {
24-
driver: require('ioredis'),
25-
host: '127.0.0.1'
26-
}, err => {
27-
if (err) throw err
28-
})
20+
fastify.register(require('fastify-redis'), { host: '127.0.0.1' })
2921

3022
fastify.get('/foo', (req, reply) => {
3123
const { redis } = fastify
@@ -52,9 +44,9 @@ object with the `client` property set to the instance.
5244

5345
```js
5446
const fastify = Fastify()
55-
const redis = require('redis').createClient({host: 'localhost', port: 6379})
47+
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
5648

57-
fastify.register(fastifyRedis, {client: redis})
49+
fastify.register(fastifyRedis, { client: redis })
5850

5951
// ...
6052
// ...

index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
'use strict'
22

33
const fp = require('fastify-plugin')
4-
const redis = require('redis')
4+
const Redis = require('ioredis')
55

66
function fastifyRedis (fastify, options, next) {
77
var client = options.client || null
88

99
if (!client) {
1010
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)
11+
client = new Redis(options)
1512
} catch (err) {
1613
return next(err)
1714
}
@@ -29,6 +26,6 @@ function close (fastify, done) {
2926
}
3027

3128
module.exports = fp(fastifyRedis, {
32-
fastify: '>=0.39',
29+
fastify: '>=1.x',
3330
name: 'fastify-redis'
3431
})

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "standard && tap test.js",
8-
"redis": "docker run -p 6379:6379 redis:3.0.7"
8+
"redis": "docker run -p 6379:6379 --rm redis:3.0.7"
99
},
1010
"repository": {
1111
"type": "git",
@@ -16,7 +16,8 @@
1616
"redis",
1717
"database",
1818
"speed",
19-
"cache"
19+
"cache",
20+
"ioredis"
2021
],
2122
"author": "Tomas Della Vedova - @delvedor (http://delved.org)",
2223
"license": "MIT",
@@ -25,13 +26,13 @@
2526
},
2627
"homepage": "https://github.com/fastify/fastify-redis#readme",
2728
"devDependencies": {
28-
"fastify": "^0.39.0",
29-
"ioredis": "^3.2.1",
30-
"standard": "^10.0.3",
31-
"tap": "^11.0.1"
29+
"fastify": "^1.2.1",
30+
"redis": "^2.8.0",
31+
"standard": "^11.0.1",
32+
"tap": "^11.1.3"
3233
},
3334
"dependencies": {
34-
"fastify-plugin": "^0.2.1",
35-
"redis": "^2.8.0"
35+
"fastify-plugin": "^0.2.2",
36+
"ioredis": "^3.2.2"
3637
}
3738
}

test.js

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,23 @@ t.beforeEach(done => {
1010

1111
fastify.register(fastifyRedis, {
1212
host: '127.0.0.1'
13-
}, (err) => {
14-
t.error(err)
1513
})
1614

1715
fastify.ready(err => {
1816
t.error(err)
1917

20-
fastify.redis.flushdb(() => {
18+
fastify.redis.flushall(() => {
2119
fastify.close()
2220
done()
2321
})
2422
})
2523
})
2624

2725
test('fastify.redis should exist', t => {
28-
t.plan(3)
26+
t.plan(2)
2927
const fastify = Fastify()
3028
fastify.register(fastifyRedis, {
3129
host: '127.0.0.1'
32-
}, (err) => {
33-
t.error(err)
3430
})
3531

3632
fastify.ready(err => {
@@ -42,13 +38,11 @@ test('fastify.redis should exist', t => {
4238
})
4339

4440
test('fastify.redis should be the redis client', t => {
45-
t.plan(5)
41+
t.plan(4)
4642
const fastify = Fastify()
4743

4844
fastify.register(fastifyRedis, {
4945
host: '127.0.0.1'
50-
}, (err) => {
51-
t.error(err)
5246
})
5347

5448
fastify.ready(err => {
@@ -66,57 +60,35 @@ test('fastify.redis should be the redis client', t => {
6660
})
6761
})
6862

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)
63+
test('promises support', t => {
64+
t.plan(2)
9065
const fastify = Fastify()
9166

9267
fastify.register(fastifyRedis, {
93-
driver: require('ioredis'),
9468
host: '127.0.0.1'
95-
}, (err) => {
96-
t.error(err)
9769
})
9870

9971
fastify.ready(err => {
10072
t.error(err)
10173

102-
fastify.redis.set('key', 'value', err => {
103-
t.error(err)
104-
fastify.redis.get('key', (err, val) => {
105-
t.error(err)
74+
fastify.redis.set('key', 'value')
75+
.then(() => {
76+
return fastify.redis.get('key')
77+
})
78+
.then(val => {
10679
t.equal(val, 'value')
107-
10880
fastify.close()
10981
})
110-
})
82+
.catch(err => t.fail(err))
11183
})
11284
})
11385

114-
test('fastify.redis should be a singleton', t => {
86+
test('custom client', t => {
11587
t.plan(5)
11688
const fastify = Fastify()
117-
const redis = require('redis').createClient({host: 'localhost', port: 6379})
89+
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
11890

119-
fastify.register(fastifyRedis, {client: redis})
91+
fastify.register(fastifyRedis, { client: redis })
12092

12193
fastify.ready(err => {
12294
t.error(err)

0 commit comments

Comments
 (0)