fastify / fastify-redis

Plugin to share a common Redis connection across Fastify.
MIT License
198 stars 31 forks source link

feat: decorate for dragonfly support #184

Closed ilteoood closed 1 year ago

ilteoood commented 1 year ago

Prerequisites

🚀 Feature Proposal

Dragonfly aims to be 100% Redis compatible.

It would be great to support it in this plugin.

Motivation

Dragonfly doesn't provide any additional tool, they suggest to use what the Redis community provides because their APIs aims to be 100% compatible with it.

Example

'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}`)
})