nestjs-opensearch
OpenSearch module for NestJS framework
Installation
·
Usage
·
Issues
Installation
For NPM:
$ npm i --save nestjs-opensearch @opensearch-project/opensearch
For Yarn:
$ yarn add nestjs-opensearch @opensearch-project/opensearch
Module configuration
Module for single connection:
import { OpensearchModule } from 'nestjs-opensearch';
@Module({
imports: [
OpensearchModule.forRoot({
node: 'https://*****.es.amazonaws.com',
}),
],
providers: (...),
})
export class SearchModule { }
Module for multiple connections:
import { OpensearchModule } from 'nestjs-opensearch';
@Module({
imports: [
OpensearchModule.forRoot({
clientName: 'foo',
node: 'https://*****.es.amazonaws.com',
}),
OpensearchModule.forRoot({
clientName: 'bar',
node: 'https://*****.es.amazonaws.com',
}),
],
providers: (...),
})
export class SearchModule { }
Module for async configuration using useFactory:
import { OpensearchModule } from 'nestjs-opensearch';
@Module({
imports: [
OpensearchModule.forRootAsync({
clientName: 'baz',
imports: [ ConfigModule ],
inject: [ ConfigService ],
useFactory: (configService) => ({
node: configService.get<string>('opensearch.node'),
}),
}),
],
providers: (...),
})
export class SearchModule { }
Module for async configuration using useClass:
import type { ClientOptions } from '@opensearch-project/opensearch';
import { OpensearchModule, OpensearchClientOptionsFactory } from 'nestjs-opensearch';
@Injectable()
export class OpensearchConfigService implements OpensearchClientOptionsFactory {
public async createOpensearchClientOptions(): Promise<ClientOptions> {
const configs = await fetch(...);
return {
node: configs.node,
};
}
}
@Module({
imports: [
OpensearchModule.forRootAsync({
clientName: 'qux',
useClass: OpensearchConfigService,
}),
],
providers: (...),
})
export class SearchModule { }
Client usage
import { InjectOpensearchClient, OpensearchClient } from 'nestjs-opensearch';
@Injectable()
export class SearchService {
public constructor(
private readonly searchClient: OpensearchClient,
@InjectOpensearchClient()
private readonly alsoSearchClient: OpensearchClient,
@InjectOpensearchClient('foo')
private readonly fooSearchClient: OpensearchClient,
) { }
}