nest-elastic-enterprise-search
v1.0.0
Published
Elastic App Search module based on the official @elastic/enterprise-search package
Downloads
4
Readme
Description
This package is based on official package: @nestjs/elasticsearch
Elasticsearch module for Nest based on the official @elastic/enterprise-search package.
Installation
$ npm i --save nest-elastic-enterprise-search
Usage
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
) {}
}
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
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.
API Spec
The ElasticEnterpriseService
wraps the Client
from the official @elastic/enterprise-search methods. The ElasticEnterpriseSearchModule.register()
takes options
object as an argument, read more.
License
This package is under MIT licensed.