-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from aleksasiriski/ms/refactor/search
refactor(search): fuzzySearchFilters with variable dbfields
- Loading branch information
Showing
3 changed files
with
38 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,45 @@ | ||
import { sql, ilike, type Column, type SQL } from 'drizzle-orm'; | ||
|
||
type FuzzySearchFiltersOptions = { | ||
distance?: number; | ||
substr?: boolean; | ||
}; | ||
|
||
export function fuzzySearchFilters( | ||
dbField: Column, | ||
dbFields: Column[], | ||
searchQuery: string, | ||
distance: number, | ||
substr: boolean = false | ||
opts: FuzzySearchFiltersOptions = {} | ||
): SQL[] { | ||
// Assert dbField is valid | ||
if (dbField === null || dbField === undefined) { | ||
throw new Error('Invalid dbField'); | ||
// Assert dbFields are valid | ||
if (dbFields === null || dbFields === undefined || dbFields.length === 0) { | ||
throw new Error('Invalid dbFields'); | ||
} | ||
|
||
// Assert searchQuery is valid | ||
if (searchQuery === null || searchQuery === undefined || searchQuery === '') { | ||
throw new Error('Invalid searchQuery'); | ||
} | ||
|
||
// Assert distance is valid | ||
if (distance === null || distance === undefined) { | ||
throw new Error('Invalid distance'); | ||
} | ||
|
||
// Assert substr is valid | ||
if (substr === null || substr === undefined) { | ||
throw new Error('Invalid substr'); | ||
} | ||
|
||
return [ | ||
sql`LEVENSHTEIN(LOWER(${dbField}), ${searchQuery}) <= ${distance}`, | ||
ilike(dbField, `${substr ? '%' : ''}${searchQuery}%`) | ||
]; | ||
} | ||
|
||
export function fuzzyConcatSearchFilters( | ||
dbField1: Column, | ||
dbField2: Column, | ||
searchQuery: string, | ||
distance: number, | ||
substr: boolean = false | ||
): SQL[] { | ||
// Assert dbField1 is valid | ||
if (dbField1 === null || dbField1 === undefined) { | ||
throw new Error('Invalid dbField1'); | ||
} | ||
|
||
// Assert dbField2 is valid | ||
if (dbField2 === null || dbField2 === undefined) { | ||
throw new Error('Invalid dbField2'); | ||
// Assert substr option is valid | ||
if (opts.substr === null) { | ||
throw new Error('Invalid substr option'); | ||
} | ||
|
||
// Assert searchQuery is valid | ||
if (searchQuery === null || searchQuery === undefined || searchQuery === '') { | ||
throw new Error('Invalid searchQuery'); | ||
// Assert distance option is valid | ||
if (opts.distance === null || (opts.distance !== undefined && opts.distance <= 0)) { | ||
throw new Error('Invalid distance option'); | ||
} | ||
|
||
// Assert distance is valid | ||
if (distance === null || distance === undefined) { | ||
throw new Error('Invalid distance'); | ||
} | ||
const concatQuery = dbFields | ||
.map((field) => sql`${field}`) | ||
.reduce((prev, curr) => sql`${prev} || ' ' || ${curr}`); | ||
|
||
// Assert substr is valid | ||
if (substr === null || substr === undefined) { | ||
throw new Error('Invalid substr'); | ||
} | ||
// @ts-expect-error because there is no typedef for sql as first param in ilike function | ||
const ilikeFilter = ilike(concatQuery, `${opts.substr ? '%' : ''}${searchQuery}%`); | ||
const levenshteinFilter = | ||
opts.distance !== undefined | ||
? [sql`LEVENSHTEIN(LOWER(${concatQuery}), ${searchQuery}) <= ${opts.distance}`] | ||
: []; | ||
|
||
return [ | ||
sql`LEVENSHTEIN(LOWER(${dbField1}) || ' ' || LOWER(${dbField2}), ${searchQuery}) <= ${distance}`, | ||
// @ts-expect-error because there is no typedef for sql as first param in ilike function | ||
ilike(sql`${dbField1} || ' ' || ${dbField2}`, `${substr ? '%' : ''}${searchQuery}%`) | ||
]; | ||
return [ilikeFilter, ...levenshteinFilter]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters