Skip to content

Commit 47dd4e5

Browse files
feat: range queries
1 parent 71612fd commit 47dd4e5

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed

src/lib/queries/get-bool.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { faker } from '@faker-js/faker'
22
import { HomeDocument, PropertyType } from 'test/module'
33
import { getBoolQuery } from './get-bool'
4+
import { getRangeQuery } from './get-range'
45
import { getTermQuery } from './get-term'
56
import { getTermsQuery } from './get-terms'
67

@@ -16,6 +17,10 @@ describe('getBoolQuery', () => {
1617
must: {
1718
term: { address: { value: 'test' } }
1819
}
20+
}),
21+
getRangeQuery('propertyAreaSquared', {
22+
gte: 500,
23+
lte: 1000
1924
})
2025
]
2126
})
@@ -30,6 +35,11 @@ describe('getBoolQuery', () => {
3035
bool: {
3136
must: { term: { address: { value: 'test' } } }
3237
}
38+
},
39+
{
40+
range: {
41+
propertyAreaSquared: { gte: 500, lte: 1000 }
42+
}
3343
}
3444
]
3545
}

src/lib/queries/get-must.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Document } from 'lib/types'
22
import { TermQuery } from './get-term'
33
import { TermsQuery } from './get-terms'
44
import { BoolQuery } from './get-bool'
5+
import { RangeQuery } from './get-range'
56

6-
export type MustQueryBody<TDocument extends Document> = TermQuery<TDocument> | TermsQuery<TDocument> | BoolQuery<TDocument>
7+
export type MustQueryBody<TDocument extends Document> = BoolQuery<TDocument> | TermQuery<TDocument> | TermsQuery<TDocument> | RangeQuery<TDocument>
78

89
export type MustQuery<TDocument extends Document> = {
910
must?: MustQueryBody<TDocument> | Array<MustQueryBody<TDocument>>

src/lib/queries/get-range.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getRangeQuery } from './get-range'
2+
3+
describe('getRangeQuery', () => {
4+
it('accepts optional term queries', () => {
5+
const query = getRangeQuery('propertyAreaSquared', {
6+
gte: 150,
7+
lte: 250
8+
})
9+
10+
expect(query).toEqual({
11+
range: {
12+
propertyAreaSquared: { gte: 150, lte: 250 }
13+
}
14+
})
15+
})
16+
})

src/lib/queries/get-range.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Document, Keyword, KeywordType } from 'lib/types'
2+
3+
export type RangeQueryOptions<
4+
TDocument extends Document,
5+
TKeyword extends Keyword<TDocument> = Keyword<TDocument>
6+
> = {
7+
gt?: KeywordType<TDocument, TKeyword>
8+
gte?: KeywordType<TDocument, TKeyword>
9+
lt?: KeywordType<TDocument, TKeyword>
10+
lte?: KeywordType<TDocument, TKeyword>
11+
}
12+
13+
export type RangeQueryBody<
14+
TDocument extends Document,
15+
TKeyword extends Keyword<TDocument> = Keyword<TDocument>
16+
> = {
17+
[x in TKeyword]?: RangeQueryOptions<TDocument, TKeyword>
18+
}
19+
20+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21+
export type RangeQuery<
22+
TDocument extends Document,
23+
TKeyword extends Keyword<TDocument> = Keyword<TDocument>
24+
> = {
25+
range: RangeQueryBody<TDocument, TKeyword>
26+
}
27+
28+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29+
export const getRangeQuery = <
30+
TDocument extends Document,
31+
TKeyword extends Keyword<TDocument> = Keyword<TDocument>
32+
>(field: TKeyword, options: RangeQueryOptions<TDocument, TKeyword>): RangeQuery<TDocument, TKeyword> => ({
33+
range: { [field]: options } as RangeQueryBody<TDocument, TKeyword>
34+
})

src/lib/queries/get-should.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Document } from 'lib/types'
22
import { TermQuery } from './get-term'
33
import { TermsQuery } from './get-terms'
44
import { BoolQuery } from './get-bool'
5+
import { RangeQuery } from './get-range'
56

6-
export type ShouldQueryBody<TDocument extends Document> = TermQuery<TDocument> | TermsQuery<TDocument> | BoolQuery<TDocument>
7+
export type ShouldQueryBody<TDocument extends Document> = BoolQuery<TDocument> | TermQuery<TDocument> | TermsQuery<TDocument> | RangeQuery<TDocument>
78

89
export type ShouldQuery<TDocument extends Document> = {
910
should?: ShouldQueryBody<TDocument> | Array<ShouldQueryBody<TDocument>>

src/lib/queries/get-term.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { HomeDocument } from 'test/module'
21
import { getTermQuery } from './get-term'
32

43
describe('getTermQuery', () => {
54
it('accepts only schema fields', () => {
6-
const query = getTermQuery<HomeDocument>('address', 'test')
5+
const query = getTermQuery('address', 'test')
76

87
expect(query).toEqual({
98
term: {

src/lib/queries/get-terms.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { HomeDocument } from 'test/module'
21
import { getTermsQuery } from './get-terms'
32

43
describe('getTermsQuery', () => {
54
it('accepts only schema fields', () => {
6-
const query = getTermsQuery<HomeDocument>('address', ['one', 'two', 'three'])
5+
const query = getTermsQuery('address', ['one', 'two', 'three'])
76

87
expect(query).toEqual({
98
terms: {

0 commit comments

Comments
 (0)