Skip to content

Commit ef8d651

Browse files
authored
šŸ› mongoose ilike comparism could not search with fixed start/end letter (#341)
this PR fixes misbehaviour described in #298
2 parents 14175fb + 60d7112 commit ef8d651

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

ā€Žpackages/query-mongoose/__tests__/query/comparison.builder.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,31 @@ describe('ComparisonBuilder', (): void => {
7373
it('should build like sql fragment', (): void => {
7474
expect(createComparisonBuilder().build('stringType', 'like', '%hello%')).toEqual({
7575
stringType: {
76-
$regex: /.*hello.*/
76+
$regex: /^.*hello.*$/
7777
}
7878
})
7979
})
8080

8181
it('should build notLike sql fragment', (): void => {
8282
expect(createComparisonBuilder().build('stringType', 'notLike', '%hello%')).toEqual({
8383
stringType: {
84-
$not: { $regex: /.*hello.*/ }
84+
$not: { $regex: /^.*hello.*$/ }
8585
}
8686
})
8787
})
8888

8989
it('should build iLike sql fragment', (): void => {
9090
expect(createComparisonBuilder().build('stringType', 'iLike', '%hello%')).toEqual({
9191
stringType: {
92-
$regex: /.*hello.*/i
92+
$regex: /^.*hello.*$/i
9393
}
9494
})
9595
})
9696

9797
it('should build notILike sql fragment', (): void => {
9898
expect(createComparisonBuilder().build('stringType', 'notILike', '%hello%')).toEqual({
9999
stringType: {
100-
$not: { $regex: /.*hello.*/i }
100+
$not: { $regex: /^.*hello.*$/i }
101101
}
102102
})
103103
})

ā€Žpackages/query-mongoose/__tests__/query/where.builder.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('WhereBuilder', (): void => {
5454
{
5555
$and: [
5656
{
57-
$and: [{ numberType: { $eq: 1 } }, { stringType: { $regex: /foo.*/ } }, { boolType: { $eq: true } }]
57+
$and: [{ numberType: { $eq: 1 } }, { stringType: { $regex: /^foo.*$/ } }, { boolType: { $eq: true } }]
5858
}
5959
]
6060
}
@@ -88,8 +88,8 @@ describe('WhereBuilder', (): void => {
8888
},
8989
{
9090
$and: [
91-
{ $and: [{ $and: [{ numberType: { $gt: 10 } }, { stringType: { $regex: /foo.*/ } }] }] },
92-
{ $and: [{ $and: [{ numberType: { $lt: 20 } }, { stringType: { $regex: /.*bar/ } }] }] }
91+
{ $and: [{ $and: [{ numberType: { $gt: 10 } }, { stringType: { $regex: /^foo.*$/ } }] }] },
92+
{ $and: [{ $and: [{ numberType: { $lt: 20 } }, { stringType: { $regex: /^.*bar$/ } }] }] }
9393
]
9494
}
9595
)
@@ -147,14 +147,14 @@ describe('WhereBuilder', (): void => {
147147
{
148148
$and: [
149149
{
150-
$and: [{ numberType: { $gt: 10 } }, { stringType: { $regex: /foo.*/ } }]
150+
$and: [{ numberType: { $gt: 10 } }, { stringType: { $regex: /^foo.*$/ } }]
151151
}
152152
]
153153
},
154154
{
155155
$and: [
156156
{
157-
$and: [{ numberType: { $lt: 20 } }, { stringType: { $regex: /.*bar/ } }]
157+
$and: [{ numberType: { $lt: 20 } }, { stringType: { $regex: /^.*bar$/ } }]
158158
}
159159
]
160160
}

ā€Žpackages/query-mongoose/src/query/comparison.builder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class ComparisonBuilder<Entity extends Document> {
9898

9999
private likeComparison<F extends keyof Entity>(cmp: string, val: EntityComparisonField<Entity, F>): FilterQuery<RegExp> {
100100
const regExpStr = escapeRegExp(`${String(val)}`).replace(/%/g, '.*')
101-
const regExp = new RegExp(regExpStr, cmp.includes('ilike') ? 'i' : undefined)
101+
const regExp = new RegExp(`^${regExpStr}$`, cmp.includes('ilike') ? 'i' : undefined)
102102

103103
if (cmp.startsWith('not')) {
104104
return { $not: { $regex: regExp } }

0 commit comments

Comments
Ā (0)