This package is based on official package: @nestjs/elasticsearch
Elasticsearch module for Nest based on the official @elastic/enterprise-search package.
$ npm i --save nest-elastic-enterprise-search
Import ElasticEnterpriseSearchModule
:
@Module({
imports: [ElasticEnterpriseSearchModule.register({
url: 'http://localhost:9200',
})],
providers: [...],
})
export class SearchModule {}
Inject ElasticEnterpriseService
:
@Injectable()
export class SearchService {
constructor(
private readonly elasticEnterpriseService: ElasticEnterpriseService
) {}
}
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
ElasticEnterpriseSearchModule.registerAsync({
useFactory: () => ({
url: 'http://localhost:9200'
})
});
Obviously, our factory behaves like every other one (might be async
and is able to inject dependencies through inject
).
ElasticEnterpriseSearchModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
url: configService.get('ELASTICSEARCH_ENTERPRISE_URL'),
}),
inject: [ConfigService],
}),
2. Use class
ElasticEnterpriseSearchModule.registerAsync({
useClass: ElasticsearchConfigService
});
Above construction will instantiate ElasticsearchConfigService
inside ElasticEnterpriseSearchModule
and will leverage it to create options object.
class ElasticEnterpriseSearchConfigService
implements ElasticEnterpriseSearchOptionsFactory
{
createElasticEnterpriseSearchOptions(): ElasticEnterpriseSearchModuleOptions {
return {
url: 'http://localhost:9200'
};
}
}
3. Use existing
ElasticEnterpriseSearchModule.registerAsync({
imports: [ConfigModule],
useExisting: ConfigService,
}),
It works the same as useClass
with one critical difference - ElasticEnterpriseSearchModule
will lookup imported modules to reuse already created ConfigService
, instead of instantiating it on its own.
The ElasticEnterpriseService
wraps the Client
from the official @elastic/enterprise-search methods. The ElasticEnterpriseSearchModule.register()
takes options
object as an argument, read more.
This package is under MIT licensed.