Skip to content

Commit

Permalink
perf: use PGTRGM for owner search
Browse files Browse the repository at this point in the history
  • Loading branch information
loicguillois committed Dec 24, 2024
1 parent 9a9bcab commit 8163bc6
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions server/src/repositories/ownerRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,29 @@ const searchOwners = async (
perPage?: number
): Promise<PaginatedResultApi<OwnerApi>> => {
const filterQuery = db(ownerTable)
.whereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q
)
.orWhereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q?.split(' ').reverse().join(' ')
);

const filteredCount: number = await db(ownerTable)
.whereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q
)
.orWhereRaw(
`upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`,
q?.split(' ').reverse().join(' ')
)
.count('id')
.first()
.then((_) => Number(_?.count));
.select('*')
.whereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q}%`]
)
.orWhereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q.split(' ').reverse().join(' ')}%`]
)
.orderBy('id', 'desc');

const filteredCount = await db(ownerTable)
.whereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q}%`]
)
.orWhereRaw(
`immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`,
[`%${q.split(' ').reverse().join(' ')}%`]
)
.count('id')
.first()
.then((row) => Number(row?.count));

const totalCount = await db(ownerTable)
.count('id')
Expand Down

0 comments on commit 8163bc6

Please sign in to comment.