Skip to content

Commit 071f040

Browse files
feat: catalogs
1 parent 135f94c commit 071f040

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

src/nestjs/elasticsearch.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ClientOptions } from '@elastic/elasticsearch'
2-
import { ElasticsearchModule as BaseElasticsearchModule, ElasticsearchService as ElasticsearchBaseService } from '@nestjs/elasticsearch'
2+
import { ElasticsearchModule as BaseElasticsearchModule } from '@nestjs/elasticsearch'
33
import { Module, DynamicModule, Provider } from '@nestjs/common'
44
import { ClassConstructor } from 'lib/types'
55
import { ELASTICSEARCH_CATALOG_NAME, ELASTICSEARCH_CATALOG_PREFIX } from 'lib/constants'
@@ -28,9 +28,9 @@ export class ElasticsearchModule {
2828
}
2929

3030
return {
31-
inject: [ElasticsearchBaseService],
31+
inject: [ElasticsearchService],
3232
provide: `${ELASTICSEARCH_CATALOG_PREFIX}:${name}`,
33-
useFactory: (base: ElasticsearchBaseService) => new Catalog(base, document)
33+
useFactory: (base: ElasticsearchService) => new Catalog(base, document)
3434
}
3535
})
3636

src/nestjs/elasticsearch.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class ElasticsearchService {
2020
}
2121

2222
withCatalog<TDocument extends Document>(catalogDocument: ClassConstructor<TDocument>) {
23-
return new Catalog(this.elasticsearchBaseService, catalogDocument)
23+
return new Catalog(this, catalogDocument)
2424
}
2525

2626
search<TDocument extends Document>(document: ClassConstructor<TDocument>, options: SearchOptions<TDocument>) {

src/nestjs/injectables/catalog.injectable.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import { Injectable } from '@nestjs/common'
2-
import { ElasticsearchService as ElasticsearchBaseService } from '@nestjs/elasticsearch'
32
import { ClassConstructor, Document, ElasticsearchCatalog } from 'lib/types'
43
import { QueryBuilder } from 'lib/builders'
54
import { ELASTICSEARCH_CATALOG_NAME } from 'lib/constants'
6-
import { ElasticsearchResult, getSearchRequest, getSearchResponse, SearchOptions } from 'lib/elasticsearch'
5+
import { SearchOptions } from 'lib/elasticsearch'
6+
import { ElasticsearchService } from '..'
77

88
@Injectable()
99
export class Catalog<TDocument extends Document> implements ElasticsearchCatalog<TDocument> {
1010
private readonly index: string
1111

1212
constructor(
13-
private readonly elasticsearchBaseService: ElasticsearchBaseService,
13+
private readonly es: ElasticsearchService,
1414
private readonly document: ClassConstructor<TDocument>
1515
) {
1616
this.index = Reflect.getMetadata(ELASTICSEARCH_CATALOG_NAME, document)
1717
}
1818

1919
search(options: SearchOptions<TDocument>) {
20-
const request = getSearchRequest<TDocument>(this.index, options)
21-
22-
return this.elasticsearchBaseService.search<ElasticsearchResult<TDocument>>(request)
23-
.then(response => getSearchResponse(this.document, response))
20+
return this.es.search(this.document, options)
2421
.catch(error => {
2522
console.error(error)
2623

src/nestjs/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { ELASTICSEARCH_CATALOG_PREFIX } from 'lib/constants'
2+
3+
export const getCatalogInjectionToken = (index: string) => `${ELASTICSEARCH_CATALOG_PREFIX}:${index}`
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { setupNestApplication } from 'test/toolkit'
2+
import { ElasticsearchModule } from 'nestjs/elasticsearch.module'
3+
import { HomeDocument, PropertyType } from 'test/module'
4+
import { getBoolQuery, getTermQuery } from 'lib/queries'
5+
import { validateSync } from 'class-validator'
6+
import { getCatalogInjectionToken } from 'nestjs/utils'
7+
import { Catalog } from 'nestjs/injectables'
8+
9+
describe('Making a search', () => {
10+
const { app } = setupNestApplication({
11+
imports: [
12+
ElasticsearchModule.register({
13+
node: 'http://localhost:9200'
14+
}),
15+
ElasticsearchModule.forFeature([
16+
HomeDocument
17+
])
18+
]
19+
})
20+
21+
it('has catalog available', () => {
22+
const catalog = app.get<Catalog<HomeDocument>>(getCatalogInjectionToken('homes'))
23+
24+
expect(catalog).toBeDefined()
25+
})
26+
27+
it('makes a test search query', async () => {
28+
const catalog = app.get<Catalog<HomeDocument>>(getCatalogInjectionToken('homes'))
29+
const result = await catalog.search({
30+
size: 10,
31+
body: {
32+
query: getBoolQuery({
33+
must: [
34+
getTermQuery('propertyType.keyword', PropertyType.Flat)
35+
]
36+
})
37+
}
38+
})
39+
40+
const allDocumentsAreValid = result.documents.every(document => {
41+
const errors = validateSync(document)
42+
43+
return !errors.length
44+
})
45+
46+
expect(allDocumentsAreValid).toBe(true)
47+
})
48+
})

0 commit comments

Comments
 (0)