Coder Social home page Coder Social logo

sonlam806 / elasticsearch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nestjs/elasticsearch

0.0 0.0 0.0 1.58 MB

Elasticsearch module based on the official elasticsearch package 🌿

Home Page: https://nestjs.com

License: MIT License

JavaScript 20.44% TypeScript 79.56%

elasticsearch's Introduction

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Travis Linux Coverage Discord Backers on Open Collective Sponsors on Open Collective

Description

Elasticsearch module for Nest based on the official @elastic/elasticsearch package.

Installation

$ npm i --save @nestjs/elasticsearch @elastic/elasticsearch

Usage

Import ElasticsearchModule:

@Module({
  imports: [ElasticsearchModule.register({
    node: 'http://localhost:9200',
  })],
  providers: [...],
})
export class SearchModule {}

Inject ElasticsearchService:

@Injectable()
export class SearchService {
  constructor(private readonly elasticsearchService: ElasticsearchService) {}
}

Async options

Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.

1. Use factory

ElasticsearchModule.registerAsync({
  useFactory: () => ({
    node: 'http://localhost:9200'
  })
});

Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).

ElasticsearchModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    node: configService.get('ELASTICSEARCH_NODE'),
  }),
  inject: [ConfigService],
}),

2. Use class

ElasticsearchModule.registerAsync({
  useClass: ElasticsearchConfigService
});

Above construction will instantiate ElasticsearchConfigService inside ElasticsearchModule and will leverage it to create options object.

class ElasticsearchConfigService implements ElasticsearchOptionsFactory {
  createElasticsearchOptions(): ElasticsearchModuleOptions {
    return {
      node: 'http://localhost:9200'
    };
  }
}

3. Use existing

ElasticsearchModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
}),

It works the same as useClass with one critical difference - ElasticsearchModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.

API Spec

The ElasticsearchService wraps the Client from the official @elastic/elasticsearch methods. The ElasticsearchModule.register() takes options object as an argument, read more.

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

elasticsearch's People

Contributors

renovate-bot avatar kamilmysliwiec avatar dependabot[bot] avatar renovate[bot] avatar jbw avatar tony133 avatar berger92 avatar chris72205 avatar brunnerlivio avatar mszalek avatar probil avatar micalevisk avatar romanchuk avatar lhr0909 avatar caucik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.