Skip to content

Commit 2fdbcd5

Browse files
authored
Merge pull request #88 from CodelyTV/part_five_elastic
Part five elastic
2 parents 705a96e + 0c4f5bc commit 2fdbcd5

File tree

7 files changed

+56
-36
lines changed

7 files changed

+56
-36
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import config from '../../../../../apps/backoffice/backend/config/config';
2+
import ElasticConfig from '../../../../Shared/infrastructure/persistence/elasticsearch/ElasticConfig';
3+
4+
export class BackofficeElasticConfigFactory {
5+
static createConfig(): ElasticConfig {
6+
return {
7+
url: config.get('elastic.url')
8+
};
9+
}
10+
}

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { ElasticRepository } from '../../../../Shared/infrastructure/persistence
22
import { BackofficeCourse } from '../../domain/BackofficeCourse';
33
import { BackofficeCourseRepository } from '../../domain/BackofficeCourseRepository';
44

5-
type ElasticBackofficeCourseDocument = { _source: { id: string; duration: string; name: string } };
6-
75
export class ElasticBackofficeCourseRepository
86
extends ElasticRepository<BackofficeCourse>
97
implements BackofficeCourseRepository {
@@ -12,23 +10,10 @@ export class ElasticBackofficeCourseRepository
1210
}
1311

1412
async searchAll(): Promise<BackofficeCourse[]> {
15-
const client = await this.client();
16-
17-
const response = await client.search({
18-
index: this.moduleName(),
19-
body: {
20-
query: {
21-
match_all: {}
22-
}
23-
}
24-
});
25-
26-
return response.body.hits.hits.map((hit: ElasticBackofficeCourseDocument) =>
27-
BackofficeCourse.fromPrimitives({ ...hit._source })
28-
);
13+
return this.searchAllInElastic(BackofficeCourse.fromPrimitives);
2914
}
3015

3116
async save(course: BackofficeCourse): Promise<void> {
32-
return this.persist(this.moduleName(), course);
17+
return this.persist(course);
3318
}
3419
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Client as ElasticClient } from '@elastic/elasticsearch';
2-
import config from '../../../../../apps/backoffice/backend/config/config';
32
import { Nullable } from '../../../domain/Nullable';
3+
import ElasticConfig from './ElasticConfig';
44

55
export class ElasticClientFactory {
66
private static clients: { [key: string]: ElasticClient } = {};
77

8-
static async createClient(contextName: string): Promise<ElasticClient> {
8+
static async createClient(contextName: string, config: ElasticConfig): Promise<ElasticClient> {
99
let client = ElasticClientFactory.getClient(contextName);
1010

1111
if (!client) {
12-
client = await ElasticClientFactory.createAndConnectClient();
12+
client = await ElasticClientFactory.createAndConnectClient(config);
1313

1414
ElasticClientFactory.registerClient(client, contextName);
1515
}
@@ -21,8 +21,8 @@ export class ElasticClientFactory {
2121
return ElasticClientFactory.clients[contextName];
2222
}
2323

24-
private static async createAndConnectClient(): Promise<ElasticClient> {
25-
const client = new ElasticClient({ node: config.get('elastic.url') });
24+
private static async createAndConnectClient(config: ElasticConfig): Promise<ElasticClient> {
25+
const client = new ElasticClient({ node: config.url });
2626

2727
return client;
2828
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type ElasticConfig = { url: string };
2+
3+
export default ElasticConfig;
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Client as ElasticClient } from '@elastic/elasticsearch';
22
import { AggregateRoot } from '../../../domain/AggregateRoot';
33

4+
type Hit = { _source: any };
5+
46
export abstract class ElasticRepository<T extends AggregateRoot> {
57
constructor(private _client: Promise<ElasticClient>) {}
68

@@ -10,10 +12,25 @@ export abstract class ElasticRepository<T extends AggregateRoot> {
1012
return this._client;
1113
}
1214

13-
protected async persist(index: string, aggregateRoot: T): Promise<void> {
14-
const document = { ...aggregateRoot.toPrimitives() };
15+
protected async searchAllInElastic(unserializer: (data: any) => T): Promise<T[]> {
16+
const client = await this.client();
17+
18+
const response = await client.search({
19+
index: this.moduleName(),
20+
body: {
21+
query: {
22+
match_all: {}
23+
}
24+
}
25+
});
26+
27+
return response.body.hits.hits.map((hit: Hit) => unserializer({ ...hit._source }));
28+
}
29+
30+
protected async persist(aggregateRoot: T): Promise<void> {
1531
const client = await this.client();
32+
const document = { ...aggregateRoot.toPrimitives() };
1633

17-
await client.index({ index: index, body: document, refresh: 'wait_for' }); // wait_for wait for a refresh to make this operation visible to search
34+
await client.index({ index: this.moduleName(), body: document, refresh: 'wait_for' }); // wait_for wait for a refresh to make this operation visible to search
1835
}
1936
}

src/apps/backoffice/backend/config/dependency-injection/Shared/application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
factory:
1414
class: ../../../../../../Contexts/Shared/infrastructure/persistence/elasticsearch/ElasticClientFactory
1515
method: 'createClient'
16-
arguments: ['mooc']
16+
arguments: ['mooc', '@Apps.Backoffice.Backend.ElasticConfig']
1717

1818
Shared.QueryHandlersInformation:
1919
class: ../../../../../../Contexts/Shared/infrastructure/QueryBus/QueryHandlersInformation
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
services:
2-
Apps.Backoffice.Backend.MongoConfig:
3-
factory:
4-
class: ../../../../../../Contexts/Backoffice/Courses/infrastructure/persistence/BackofficeMongoConfigFactory
5-
method: 'createConfig'
2+
Apps.Backoffice.Backend.MongoConfig:
3+
factory:
4+
class: ../../../../../../Contexts/Backoffice/Courses/infrastructure/persistence/BackofficeMongoConfigFactory
5+
method: 'createConfig'
66

7-
Apps.Backoffice.Backend.controllers.StatusGetController:
8-
class: ../../../controllers/StatusGetController
9-
arguments: []
7+
Apps.Backoffice.Backend.ElasticConfig:
8+
factory:
9+
class: ../../../../../../Contexts/Backoffice/Courses/infrastructure/persistence/BackofficeElasticConfigFactory
10+
method: 'createConfig'
1011

11-
Apps.Backoffice.Backend.controllers.CoursesGetController:
12-
class: ../../../controllers/CoursesGetController
13-
arguments: ['@Shared.QueryBus']
12+
Apps.Backoffice.Backend.controllers.StatusGetController:
13+
class: ../../../controllers/StatusGetController
14+
arguments: []
15+
16+
Apps.Backoffice.Backend.controllers.CoursesGetController:
17+
class: ../../../controllers/CoursesGetController
18+
arguments: ['@Shared.QueryBus']

0 commit comments

Comments
 (0)