Skip to content

Commit d5c33e6

Browse files
committed
added bodybuilder for elastic search criteria
1 parent 574417b commit d5c33e6

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

package-lock.json

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@types/uuid": "^8.3.0",
4545
"@types/uuid-validate": "0.0.1",
4646
"body-parser": "^1.19.0",
47+
"bodybuilder": "^2.3.0",
4748
"bson": "^4.2.0",
4849
"compression": "^1.7.4",
4950
"connect-flash": "^0.1.1",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ export class ElasticBackofficeCourseRepository
1919
}
2020

2121
matching(criteria: Criteria): Promise<BackofficeCourse[]> {
22-
throw new Error('Method not implemented.');
22+
return this.searchByCriteria(criteria, BackofficeCourse.fromPrimitives);
2323
}
2424
}

src/Contexts/Shared/domain/criteria/Order.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class Order {
1717

1818
return new Order(new OrderBy(orderBy), OrderType.fromValue(orderType || OrderTypes.ASC));
1919
}
20+
2021
static none(): Order {
2122
return new Order(new OrderBy(''), new OrderType(OrderTypes.NONE));
2223
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Client as ElasticClient } from '@elastic/elasticsearch';
22
import { ResponseError } from '@elastic/elasticsearch/lib/errors';
33
import httpStatus from 'http-status';
44
import { AggregateRoot } from '../../../domain/AggregateRoot';
5+
import { Criteria } from '../../../domain/criteria/Criteria';
6+
import bodybuilder, { Bodybuilder } from 'bodybuilder';
57

68
type Hit = { _source: any };
79

@@ -15,17 +17,27 @@ export abstract class ElasticRepository<T extends AggregateRoot> {
1517
}
1618

1719
protected async searchAllInElastic(unserializer: (data: any) => T): Promise<T[]> {
20+
const body = bodybuilder().query('match_all');
21+
body.build();
22+
return this.searchInElasticWithSourceBuilder(unserializer, body);
23+
}
24+
25+
protected async searchByCriteria(criteria: Criteria, unserializer: (data: any) => T): Promise<T[]> {
26+
// TODO elastic search converter from Criteria
27+
const body = bodybuilder().query('match_all');
28+
body.build();
29+
return this.searchInElasticWithSourceBuilder(unserializer, body);
30+
}
31+
32+
private async searchInElasticWithSourceBuilder(unserializer: (data: any) => T, body: Bodybuilder): Promise<T[]> {
1833
const client = await this.client();
1934

2035
try {
2136
const response = await client.search({
2237
index: this.moduleName(),
23-
body: {
24-
query: {
25-
match_all: {}
26-
}
27-
}
38+
body
2839
});
40+
2941
return response.body.hits.hits.map((hit: Hit) => unserializer({ ...hit._source }));
3042
} catch (e) {
3143
if (this.isNotFoundError(e)) {

0 commit comments

Comments
 (0)