Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add ElasticSearch service
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 12, 2020
1 parent 00455f8 commit 4631bbb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { StripeModule } from './modules/stripe/stripe.module';
import { TasksModule } from './providers/tasks/tasks.module';
import { UsersModule } from './modules/users/users.module';
import { WebhooksModule } from './modules/webhooks/webhooks.module';
import { ElasticSearchModule } from './providers/elasticsearch/elasticsearch.module';

@Module({
imports: [
Expand Down Expand Up @@ -61,6 +62,7 @@ import { WebhooksModule } from './modules/webhooks/webhooks.module';
StripeModule,
AuditLogsModule,
WebhooksModule,
ElasticSearchModule,
],
providers: [
{
Expand Down
2 changes: 2 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiKeyAuth, BasicAuth } from '@elastic/elasticsearch/lib/pool';
import Stripe from 'stripe';

export interface Configuration {
Expand Down Expand Up @@ -49,6 +50,7 @@ export interface Configuration {
elasticSearch: {
node: string;
retries: number;
auth?: BasicAuth | ApiKeyAuth;
aws?: {
accessKeyId: string;
secretAccessKey: string;
Expand Down
15 changes: 15 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ const configuration: Configuration = {
elasticSearch: {
node: process.env.ELASTICSEARCH_NODE,
retries: int(process.env.ELASTICSEARCH_FAIL_RETRIES, 3),
auth: process.env.ELASTICSEARCH_AUTH_USERNAME
? {
username: process.env.ELASTICSEARCH_AUTH_USERNAME,
password: process.env.ELASTICSEARCH_AUTH_PASSWORD,
}
: process.env.ELASTICSEARCH_AUTH_API_KEY
? process.env.ELASTICSEARCH_AUTH_API_KEY_ID
? {
apiKey: {
api_key: process.env.ELASTICSEARCH_AUTH_API_KEY,
id: process.env.ELASTICSEARCH_AUTH_API_KEY_ID,
},
}
: { apiKey: process.env.ELASTICSEARCH_AUTH_API_KEY }
: undefined,
aws: {
accessKeyId: process.env.ELASTICSEARCH_AWS_ACCESS_KEY_ID ?? '',
secretAccessKey: process.env.ELASTICSEARCH_AWS_SECRET_ACCESS_KEY ?? '',
Expand Down
13 changes: 11 additions & 2 deletions src/providers/elasticsearch/elasticsearch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Client } from '@elastic/elasticsearch';
import { Index } from '@elastic/elasticsearch/api/requestParams';
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import createAwsElasticsearchConnector from 'aws-elasticsearch-connector';
import AWS from 'aws-sdk';
import PQueue from 'p-queue';
import pRetry from 'p-retry';
import { Configuration } from '../../config/configuration.interface';
Expand All @@ -16,12 +18,19 @@ export class ElasticSearchService {
const config = this.configService.get<Configuration['elasticSearch']>(
'elasticSearch',
);
if (config.aws?.accessKeyId)
if (config.aws?.accessKeyId) {
AWS.config.update({
accessKeyId: config.aws.accessKeyId,
secretAccessKey: config.aws.secretAccessKey,
region: config.aws.region,
});
this.client = new Client({
...createAwsElasticsearchConnector(AWS.config),
node: config.node,
});
else
} else
this.client = new Client({
auth: config.auth,
node: config.node,
});
}
Expand Down

0 comments on commit 4631bbb

Please sign in to comment.