Skip to content

Commit

Permalink
fix: mongoose ilike comparism wasn capable of searching with fixed st…
Browse files Browse the repository at this point in the history
…art/end letter
  • Loading branch information
simnado committed Jan 2, 2025
1 parent c4731d0 commit 4f90c93
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,31 @@ describe('ComparisonBuilder', (): void => {
it('should build like sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'like', '%hello%')).toEqual({
stringType: {
$regex: /.*hello.*/
$regex: /^.*hello.*$/
}
})
})

it('should build notLike sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'notLike', '%hello%')).toEqual({
stringType: {
$not: { $regex: /.*hello.*/ }
$not: { $regex: /^.*hello.*$/ }
}
})
})

it('should build iLike sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'iLike', '%hello%')).toEqual({
stringType: {
$regex: /.*hello.*/i
$regex: /^.*hello.*$/i
}
})
})

it('should build notILike sql fragment', (): void => {
expect(createComparisonBuilder().build('stringType', 'notILike', '%hello%')).toEqual({
stringType: {
$not: { $regex: /.*hello.*/i }
$not: { $regex: /^.*hello.*$/i }
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/query-mongoose/src/query/comparison.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ComparisonBuilder<Entity extends Document> {

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

if (cmp.startsWith('not')) {
return { $not: { $regex: regExp } }
Expand Down

0 comments on commit 4f90c93

Please sign in to comment.