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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jspm_packages

# Optional npm cache directory
.npm
package-lock.json

# Optional REPL history
.node_repl_history
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ automatically closed when the fastify instance is closed.
const fastify = require('fastify')()

fastify.register(require('fastify-redis'), { host: '127.0.0.1' })
// or
fastify.register(require('fastify-redis'), { url: 'redis://127.0.0.1', /* other redis options */ })

fastify.get('/foo', (req, reply) => {
const { redis } = fastify
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const Redis = require('ioredis')
function fastifyRedis (fastify, options, next) {
const namespace = options.namespace
delete options.namespace
let redisUrl
if (options.url) {
redisUrl = options.url
delete options.url
}

let client = options.client || null

Expand All @@ -24,7 +29,11 @@ function fastifyRedis (fastify, options, next) {

if (!client) {
try {
client = new Redis(options)
if (redisUrl) {
client = new Redis(redisUrl, options)
} else {
client = new Redis(options)
}
} catch (err) {
return next(err)
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "Plugin to share a common Redis connection across Fastify.",
"main": "index.js",
"scripts": {
"test": "standard && tap test.js",
"test": "npm run lint && npm run unit",
"lint": "standard",
"unit": "tap test.js",
"redis": "docker run -p 6379:6379 --rm redis:5"
},
"repository": {
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ test('fastify.redis should exist', (t) => {
})
})

test('fastify.redis should support url', (t) => {
t.plan(4)
const fastify = Fastify()

fastify.register(fastifyRedis, {
url: 'redis://127.0.0.1',
otherOption: 'foo'
})

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()
})
})
})
})

test('fastify.redis should be the redis client', (t) => {
t.plan(4)
const fastify = Fastify()
Expand Down