Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dragonfly decorator #185

Closed
wants to merge 8 commits into from
Closed
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
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm i @fastify/redis

Add it to your project with `register` and you are done!

### Create a new Redis Client
### Create a new Redis/Dragonfly Client

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.

Expand Down Expand Up @@ -79,6 +79,48 @@ fastify.listen({ port: 3000 }, err => {
})
```

### Accessing the Dragonfly Client

Once you have registered your plugin, you can access the Dragonfly client via `fastify.dragonfly`.

The client is automatically closed when the fastify instance is closed.

```js
'use strict'

const Fastify = require('fastify')
const fastifyRedis = require('@fastify/redis')

const fastify = Fastify({ logger: true })

fastify.register(fastifyRedis, {
host: '127.0.0.1',
password: 'your strong password here',
port: 6379, // Dragonfly port
family: 4 // 4 (IPv4) or 6 (IPv6),
isDragonfly: true
})

fastify.get('/foo', (req, reply) => {
const { dragonfly } = fastify
dragonfly.get(req.query.key, (err, val) => {
reply.send(err || val)
})
})

fastify.post('/foo', (req, reply) => {
const { dragonfly } = fastify
dragonfly.set(req.body.key, req.body.value, (err) => {
reply.send(err || { status: 'ok' })
})
})

fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
```

### Using an existing Redis client

You may also supply an existing *Redis* client instance by passing an options
Expand Down
30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ const fp = require('fastify-plugin')
const Redis = require('ioredis')

function fastifyRedis (fastify, options, next) {
const { namespace, url, closeClient = false, ...redisOptions } = options
const { namespace, url, closeClient = false, isDragonfly = false, ...redisOptions } = options

const decoratorName = isDragonfly ? 'dragonfly' : 'redis'

let client = options.client || null

if (namespace) {
if (!fastify.redis) {
fastify.decorate('redis', Object.create(null))
if (!fastify[decoratorName]) {
fastify.decorate(decoratorName, Object.create(null))
}

if (fastify.redis[namespace]) {
return next(new Error(`Redis '${namespace}' instance namespace has already been registered`))
if (fastify[decoratorName][namespace]) {
return next(new Error(`${decoratorName} '${namespace}' instance namespace has already been registered`))
}

const closeNamedInstance = (fastify) => {
return fastify.redis[namespace].quit()
return fastify[decoratorName][namespace].quit()
}

if (client) {
Expand All @@ -39,14 +41,14 @@ function fastifyRedis (fastify, options, next) {
fastify.addHook('onClose', closeNamedInstance)
}

fastify.redis[namespace] = client
fastify[decoratorName][namespace] = client
} else {
if (fastify.redis) {
if (fastify[decoratorName]) {
return next(new Error('@fastify/redis has already been registered'))
} else {
if (client) {
if (closeClient === true) {
fastify.addHook('onClose', close)
fastify.addHook('onClose', close(decoratorName))
}
} else {
try {
Expand All @@ -59,10 +61,10 @@ function fastifyRedis (fastify, options, next) {
return next(err)
}

fastify.addHook('onClose', close)
fastify.addHook('onClose', close(decoratorName))
}

fastify.decorate('redis', client)
fastify.decorate(decoratorName, client)
}
}

Expand Down Expand Up @@ -122,8 +124,10 @@ function fastifyRedis (fastify, options, next) {
}
}

function close (fastify) {
return fastify.redis.quit()
function close (decoratorName) {
return function (fastify) {
return fastify[decoratorName].quit()
}
}

module.exports = fp(fastifyRedis, {
Expand Down
Loading