Skip to content

Commit d2372e8

Browse files
committed
pass config as parameter in elastic repository for index name
1 parent 3a9e9c0 commit d2372e8

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

src/Contexts/Backoffice/Courses/infrastructure/config/index.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,38 @@ const backofficeConfig = convict({
1414
env: 'ELASTIC_URL',
1515
default: 'http://localhost:9200'
1616
},
17-
indexName: 'backofficecourses',
17+
indexName: {
18+
doc: 'The Elastic index name for this context',
19+
format: String,
20+
env: 'ELASTIC_INDEX_NAME',
21+
default: 'backofficecourses'
22+
},
1823
config: {
19-
settings: {
20-
index: {
21-
number_of_replicas: 0 // for local development
22-
}
23-
},
24-
mappings: {
25-
properties: {
26-
id: {
27-
type: 'keyword',
28-
index: true
29-
},
30-
name: {
31-
type: 'text',
32-
index: true,
33-
fielddata: true
34-
},
35-
duration: {
36-
type: 'text',
37-
index: true,
38-
fielddata: true
24+
doc: 'The Elastic config for this context',
25+
format: '*',
26+
env: 'ELASTIC_CONFIG',
27+
default: {
28+
settings: {
29+
index: {
30+
number_of_replicas: 0 // for local development
31+
}
32+
},
33+
mappings: {
34+
properties: {
35+
id: {
36+
type: 'keyword',
37+
index: true
38+
},
39+
name: {
40+
type: 'text',
41+
index: true,
42+
fielddata: true
43+
},
44+
duration: {
45+
type: 'text',
46+
index: true,
47+
fielddata: true
48+
}
3949
}
4050
}
4151
}

src/Contexts/Backoffice/Courses/infrastructure/persistence/ElasticBackofficeCourseRepository.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import { Criteria } from '../../../../Shared/domain/criteria/Criteria';
22
import { ElasticRepository } from '../../../../Shared/infrastructure/persistence/elasticsearch/ElasticRepository';
33
import { BackofficeCourse } from '../../domain/BackofficeCourse';
44
import { BackofficeCourseRepository } from '../../domain/BackofficeCourseRepository';
5-
import config from '../config';
65

76
export class ElasticBackofficeCourseRepository
87
extends ElasticRepository<BackofficeCourse>
98
implements BackofficeCourseRepository {
10-
protected moduleName(): string {
11-
return config.get('elastic.indexName');
12-
}
13-
149
async searchAll(): Promise<BackofficeCourse[]> {
1510
return this.searchAllInElastic(BackofficeCourse.fromPrimitives);
1611
}

src/Contexts/Shared/infrastructure/persistence/elasticsearch/ElasticRepository.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ import bodybuilder, { Bodybuilder } from 'bodybuilder';
44
import httpStatus from 'http-status';
55
import { AggregateRoot } from '../../../domain/AggregateRoot';
66
import { Criteria } from '../../../domain/criteria/Criteria';
7+
import ElasticConfig from './ElasticConfig';
78
import { ElasticCriteriaConverter, TypeQueryEnum } from './ElasticCriteriaConverter';
89

910
type Hit = { _source: any };
1011

1112
export abstract class ElasticRepository<T extends AggregateRoot> {
1213
private criteriaConverter: ElasticCriteriaConverter;
1314

14-
constructor(private _client: Promise<ElasticClient>) {
15+
constructor(private _client: Promise<ElasticClient>, private config: ElasticConfig) {
1516
this.criteriaConverter = new ElasticCriteriaConverter();
1617
}
1718

18-
protected abstract moduleName(): string;
19+
protected moduleName(): string {
20+
return this.config.indexName;
21+
}
1922

2023
protected client(): Promise<ElasticClient> {
2124
return this._client;

src/apps/backoffice/backend/dependency-injection/Courses/application.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
services:
2-
32
Backoffice.courses.ElasticConfig:
43
factory:
54
class: ../../../../../Contexts/Backoffice/Courses/infrastructure/persistence/BackofficeElasticConfigFactory
@@ -12,7 +11,7 @@ services:
1211

1312
Backoffice.courses.BackofficeCourseRepository:
1413
class: ../../../../../Contexts/Backoffice/Courses/infrastructure/persistence/ElasticBackofficeCourseRepository
15-
arguments: ['@Shared.ElasticConnectionManager']
14+
arguments: ['@Shared.ElasticConnectionManager', '@Backoffice.courses.ElasticConfig']
1615

1716
Backoffice.courses.CoursesFinder:
1817
class: ../../../../../Contexts/Backoffice/Courses/application/SearchAll/CoursesFinder

0 commit comments

Comments
 (0)