Skip to content

Commit 0c4f5bc

Browse files
committed
added searchAllInElastic into ElasticRepository
1 parent 7edf000 commit 0c4f5bc

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

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
}
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
}

0 commit comments

Comments
 (0)