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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

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

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.
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.

## Install
```
npm i fastify-redis --save
```
## Usage
Add it to you project with `register` and you are done!
Add it to you project with `register` and you are done!
You can access the *Redis* client via `fastify.redis`.

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.


```js
const fastify = require('fastify')

fastify.register(require('fastify-redis'), {
driver: require('ioredis'),
host: '127.0.0.1'
}, err => {
if (err) throw err
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const redis = require('redis')

function fastifyRedis (fastify, options, next) {
var client = null

try {
client = redis.createClient(options)
// if custom redis module, default is redis.
const Driver = options.driver
delete options.driver
client = Driver ? new Driver(options) : redis.createClient(options)
} catch (err) {
return next(err)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"homepage": "https://github.com/fastify/fastify-redis#readme",
"devDependencies": {
"fastify": "^0.30.2",
"ioredis": "^3.2.1",
"standard": "^10.0.3",
"tap": "^10.7.2"
},
Expand Down
46 changes: 45 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ t.beforeEach(done => {
test('fastify.redis should exist', t => {
t.plan(3)
const fastify = Fastify()

fastify.register(fastifyRedis, {
host: '127.0.0.1'
}, (err) => {
Expand Down Expand Up @@ -66,3 +65,48 @@ test('fastify.redis should be the redis client', t => {
})
})
})

test('fastify.redis should exist when use the custom redis driver', t => {
t.plan(3)
const fastify = Fastify()

fastify.register(fastifyRedis, {
driver: require('ioredis'),
host: '127.0.0.1'
}, (err) => {
t.error(err)
})

fastify.ready(err => {
t.error(err)
t.ok(fastify.redis)

fastify.close()
})
})

test('fastify.redis should be the redis client when use the custom redis driver', t => {
t.plan(5)
const fastify = Fastify()

fastify.register(fastifyRedis, {
driver: require('ioredis'),
host: '127.0.0.1'
}, (err) => {
t.error(err)
})

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

fastify.redis.set('key', 'value', err => {
t.error(err)
fastify.redis.get('key', (err, val) => {
t.error(err)
t.equal(val, 'value')

fastify.close()
})
})
})
})