@bytebitlabs/nest-opensearch
v1.1.0
Published
<p align="center"> <a href="https://bytebitlabs.com/" target="_blank"><img src="https://gravatar.com/avatar/61c80d73dfe4c4231e77940cf80fd410?size=256" width="256" alt="ByteBitLabs logo" /></a> </p>
Readme
nest-opensearch
Description
OpenSearch module for Nest based on the official @opensearch-project/opensearch package.
Installation
$ npm i --save @bytebitlabs/nest-opensearch @opensearch-project/opensearchUsage
Import OpenSearchModule:
@Module({
imports: [OpenSearchModule.register({
node: 'http://localhost:9200',
})],
providers: [...],
})
export class SearchModule {}Inject OpenSearchService:
@Injectable()
export class SearchService {
constructor(private readonly openSearchService: OpenSearchService) {}
}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
OpenSearchModule.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).
OpenSearchModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
node: configService.get('OPENSEARCH_NODE'),
}),
inject: [ConfigService],
}),2. Use class
OpenSearchModule.registerAsync({
useClass: OpenSearchConfigService
});Above construction will instantiate OpenSearchConfigService inside OpenSearchModule and will leverage it to create options object.
class OpenSearchConfigService implements OpensearchOptionsFactory {
createOpensearchOptions(): OpenSearchModuleOptions {
return {
node: 'http://localhost:9200'
};
}
}3. Use existing
OpenSearchModule.registerAsync({
imports: [ConfigModule],
useExisting: ConfigService,
}),It works the same as useClass with one critical difference - OpenSearchModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.
API Spec
The OpenSearchService wraps the Client from the official @opensearch-project/opensearch methods. The OpenSearchModule.register() takes options object as an argument, read more.
