You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fastify-opensearch

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-opensearch

Fastify plugin for AWS Opensearch

2.1.1
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

fastify-opensearch

CI NPM version Known Vulnerabilities js-standard-style

Fastify plugin for AWS Opensearch for sharing the same Opensearch client in every part of your server.
Under the hood, the official opensearch module is used. Based on fastify-elasticsearch plugin.

Install

npm i fastify-opensearch

Usage

Add it to your project with register and you are done!
The plugin accepts the same options as the client.

const fastify = require('fastify')()

fastify.register(require('fastify-opensearch'), { node: 'http://localhost:9200' })

fastify.get('/user', async function (req, reply) {
  const { body } = await this.opensearch.search({
    index: 'tweets',
    body: {
      query: { match: { text: req.query.q }}
    }
  })

  return body.hits.hits
})

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

By default, fastify-opensearch will try to ping the cluster as soon as you start Fastify, but in some cases pinging may not be supported due to the user permissions. If you want, you can disable the initial ping with the healthcheck option:

fastify.register(require('fastify-opensearch'), {
  node: 'http://localhost:9200',
  healthcheck: false
})

If you need to connect to different clusters, you can also pass a namespace option:

const fastify = require('fastify')()

fastify.register(require('fastify-opensearch'), {
  node: 'http://localhost:9200',
  namespace: 'cluster1'
})

fastify.register(require('fastify-opensearch'), {
  node: 'http://localhost:9201',
  namespace: 'cluster2'
})

fastify.get('/user', async function (req, reply) {
  const { body } = await this.opensearch.cluster1.search({
    index: 'tweets',
    body: {
      query: { match: { text: req.query.q }}
    }
  })

  return body.hits.hits
})

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

Versioning

By default the latest and greatest version of the Opensearch client is used, see the compatibility to understand if the embedded client is correct for you.
If it is not, you can pass a custom client via the client option.

const fastify = require('fastify')()
const { Client } = require('@opensearch-project/opensearch')

fastify.register(require('fastify-opensearch'), {
  client: new Client({ node: 'http://localhost:9200' })
})

fastify.get('/user', async function (req, reply) {
  const { body } = await this.opensearch.search({
    index: 'tweets',
    body: {
      query: { match: { text: req.query.q }}
    }
  })

  return body.hits.hits
})

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

License

Licensed under MIT.

Keywords

fastify

FAQs

Package last updated on 04 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts