Skip to content

Commit 59bec9b

Browse files
perf: only run total count query on first page in findByIdsWithPagination
Wrap the count query in a !cursor guard so it only runs on the first page request, avoiding an extra database query on every scroll. Consistent with the hasFixedHosts optimization in HostRepository. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com>
1 parent 683ec22 commit 59bec9b

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

packages/features/users/repositories/UserRepository.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,20 @@ export class UserRepository {
468468
const items = hasMore ? users.slice(0, limit) : users;
469469
const nextCursor = hasMore ? items[items.length - 1].id : undefined;
470470

471-
const countWhere: Record<string, unknown> = {
472-
id: { in: ids },
473-
};
474-
if (search) {
475-
countWhere.OR = [
476-
{ name: { contains: search, mode: "insensitive" } },
477-
{ email: { contains: search, mode: "insensitive" } },
478-
];
471+
// Only count on the first page to avoid an extra query on every scroll
472+
let total: number | undefined;
473+
if (!cursor) {
474+
const countWhere: Record<string, unknown> = {
475+
id: { in: ids },
476+
};
477+
if (search) {
478+
countWhere.OR = [
479+
{ name: { contains: search, mode: "insensitive" } },
480+
{ email: { contains: search, mode: "insensitive" } },
481+
];
482+
}
483+
total = await this.prismaClient.user.count({ where: countWhere });
479484
}
480-
const total = await this.prismaClient.user.count({ where: countWhere });
481485

482486
return { users: items, nextCursor, total };
483487
}

0 commit comments

Comments
 (0)