# @bm629/nest-elasticsearch

> Nestjs library for elasticsearch client

Latest version **1.0.6** (published 2020-06-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @bm629/nest-elasticsearch
pnpm add @bm629/nest-elasticsearch
yarn add @bm629/nest-elasticsearch
bun add @bm629/nest-elasticsearch
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-06-27 |
| First published | 2020-05-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 44 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| Author | Bhushan Modi |
| Maintainers | bm629 |
| Keywords | nestjs, nest, typescript, elasticsearch, client |

## Links

- npm: https://www.npmjs.com/package/@bm629/nest-elasticsearch
- Repository: https://github.com/bm629/nest-elasticsearch
- Homepage: https://github.com/bm629/nest-elasticsearch#readme
- Issues: https://github.com/bm629/nest-elasticsearch/issues
- npm.io page: https://npm.io/package/@bm629/nest-elasticsearch

## Dependencies (8)

- [es5](https://npm.io/package/es5.md) npm:@elastic/elasticsearch@5
- [es6](https://npm.io/package/es6.md) npm:@elastic/elasticsearch@6
- [es7](https://npm.io/package/es7.md) npm:@elastic/elasticsearch@7
- [rxjs](https://npm.io/package/rxjs.md) ^6.5.5
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^7.2.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^7.2.0
- [@nestjs/testing](https://npm.io/package/@nestjs/testing.md) ^7.2.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-06-27
- 1.0.5 — 2020-06-09
- 1.0.4 — 2020-05-25
- 1.0.3 — 2020-05-17
- 1.0.2 — 2020-05-09
- 1.0.1 — 2020-05-03
- 1.0.0 — 2020-05-03

## README

[![npm version](https://badge.fury.io/js/%40bm629%2Fnest-elasticsearch.svg)](https://badge.fury.io/js/%40bm629%2Fnest-elasticsearch)
[![Coverage Status](https://coveralls.io/repos/github/bm629/nest-elasticsearch/badge.svg?branch=master)](https://coveralls.io/github/bm629/nest-elasticsearch?branch=master)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/bm629/nest-elasticsearch.png?branch=master)](https://travis-ci.org/@bm629/nest-elasticsearch)

## Description

[Elasticsearch](https://github.com/elastic/elasticsearch) module for [Nest](https://github.com/nestjs/nest).
This module supports elasticsearch version 5.x to 7.x

## Installation

```bash
# For version 7.x
$ npm i --save @bm629/nest-elasticsearch es7@npm:@elastic/elasticsearch

# For version 6.x
$ npm i --save @bm629/nest-elasticsearch es6@npm:@elastic/elasticsearch@6

# For version 5.x
$ npm i --save @bm629/nest-elasticsearch es5@npm:@elastic/elasticsearch@5
```

## Usage

Import Elasticsearch Module

```typescript

// For elastic client version 7.x
@Module({
  imports: [ElasticsearchModule.forRoot({
    node: 'localhost:9200',
  })],
  providers: [...],
})
export class SearchModule {}

// OR

// For elastic client version 6.x
@Module({
  imports: [ElasticsearchModule.forRoot({
    node: 'localhost:9200',
    version: 'v6'
  })],
  providers: [...],
})
export class SearchModule {}


// OR


// For elastic client version 5.x
@Module({
  imports: [ElasticsearchModule.forRoot({
    node: 'localhost:9200',
    version: 'v5'
  })],
  providers: [...],
})
export class SearchModule {}

```

Import Database module in AppModule

```typescript
// app.module.ts

@Module({
  imports: [SearchModule],
  controllers: [CatsController],
  providers: [CatSevice],
})
export class AppModule {}
```

Inject Elasticsearch client

```typescript
// cat.service.ts

import { Client } from 'es7';

export class CatService {
  contructor(@InjectElasticsearchClient() private readonly elasticsearchClient: Client) {}
}
```

For more information, see test cases. You can find details in the [`__tests__/`](https://github.com/bm629/nest-elasticsearch/tree/master/__tests__) folder of this repository.

## Async Options

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

**1. Use factory**

```typescript
ElasticsearchModule.forRootAsync({
  useFactory: () => ({
    node: 'localhost:9200',
  }),
});
```

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

```typescript
ElasticsearchModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    node: configService.getString('ELASTICSEARCH_NODE'),
  }),
  inject: [ConfigService],
}),
```

**2. Use class**

```typescript
ElasticsearchModule.forRootAsync({
  useClass: ElasticsearchConfigService,
});
```

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

```typescript
class ElasticsearchConfigService implements ElasticsearchOptionsFactory {
  createElasticsearchOptions(): ElasticsearchModuleOptions {
    return {
      node: 'localhost:9200',
    };
  }
}
```

**3. Use existing**

```typescript
ElasticsearchModule.forRootAsync({
  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.

## License

[MIT licensed](LICENSE).

---
_Source: https://npm.io/package/@bm629/nest-elasticsearch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
