Fastify plugin for elastic search
Switch branches/tags
Nothing to show
Clone or download
Latest commit e14cdda Nov 13, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore ping cluster before make plugin ready, refactor some code Mar 30, 2018
.travis.yml Update to node 10 in .travis.yml (#10) Apr 29, 2018
LICENSE Initial commit Mar 2, 2018
README.md Fix wrong require in example Mar 30, 2018
index.js Updated required fastify version Apr 3, 2018
package.json Bumped v0.4.0 Nov 13, 2018
test.js better errors Mar 30, 2018

README.md

fastify-elasticsearch

js-standard-style Build Status Greenkeeper badge

Fastify plugin for elastic search for sharing the same ES client in every part of your server.

Under the hood the official elasticsearch module is used.

Unless you give the client to this plugin, this module will pass all options to the ES Client constructor.

NB: this fastify plugin always close the elasticsearch client on server shutdown

Install

npm i fastify-elasticsearch --save

Usage

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

const fastify = require('fastify')()

fastify.register(require('fastify-elasticsearch'), {
  host: '127.0.0.1',
  port: 9200
})

fastify.get('/user/:id', function (req, reply) {
  this.elasticsearch.get({
    index: 'myindex',
    type: 'user',
    id: req.params.id
  }, function (err, response) {
    if (err) return reply.send(err)

    reply.send(response)
  })
})

fastify.listen(3000, err => {
  if (err) throw err
})

You may also supply a pre-configured instance of elasticsearch.Client:

const elasticsearch = require('elasticsearch')

const client = new elasticsearch.Client({ host: '127.0.0.1', port: 9200 })
const fastify = require('fastify')()

fastify.register(require('fastify-elasticsearch'), { client: client })
  .register(function (fastify, opts, next) {
    const elasticsearch = fastify.elasticsearch
    // ...
    // ...
    // ...
    next()
  })

License

Licensed under MIT.