Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 1.51 KB

README.md

File metadata and controls

61 lines (47 loc) · 1.51 KB

fastify-gundb

js-standard-style Build Status

Fastify GunDB connection plugin, with this you can share a Gun instance in every part of your server.

Install

npm i fastify-gundb --save

Usage

Add it to you project with register and you are done!
You can access Gun via fastify.gun.

const fastify = require('fastify')

fastify.register(require('fastify-gundb'), {
  name: 'db'
}, err => {
  if (err) throw err
})

fastify.get('/foo', (req, reply) => {
  const { gun } = fastify.gun
  gun.get(req.query.key).once(function(val){
    if (!val) val = { status: 404 }  
    reply.send(val)
  })
})

fastify.post('/foo', (req, reply) => {
  const { gun } = fastify.gun
  gun.get(req.body.key).put(req.body.value, (ack) => {
    if (!ack) val = { status: 'error' }  
    reply.send({ status: 'ok' })
  })
})

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

Acknowledgements

This project is kindly sponsored by:

Fastify is kindly sponsored by:

License

Licensed under MIT.