Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

fastify-leveldb

CI NPM version Known Vulnerabilities js-standard-style

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

Under the hood Levelup is used, the options that you pass to register will be passed to Levelup.

Install

npm i fastify-leveldb --save

Usage

Add it to you project with register, configure the database name and you are done!

You can access LevelDB via fastify.level[name].

const fastify = require('fastify')()

fastify.register(
  require('fastify-leveldb'),
  { name: 'db' }
)

fastify.get('/foo', async function (req, reply) {
  const val = await this.level.db.get(req.query.key)
  return val
})

fastify.post('/foo', async function (req, reply) {
  await this.level.db.put(req.body.key, req.body.value)
  return { status: 'ok' }
})

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

By default, Leveldown is used for the store but you can use any Abstact-leveldown compliant store, such as memdown.

First, you must install the store:

npm install memdown

Next, initialize the plugin with the given store:

fastify.register(require('fastify-leveldb'), {
  name: 'db',
  options: {
    store: require('memdown')
  }
})

By default the path where the db will be created is the name option, but you can also pass a custom path as well.

fastify.register(
  require('fastify-leveldb'),
  { name: 'db', path: '.local' }
)

Acknowledgements

This project is kindly sponsored by:

License

Licensed under MIT.

About

Plugin to share a common LevelDB connection across Fastify.

Resources

License

Packages

No packages published