Skip to content

Commit

Permalink
followUp: Improve getUniqueBookings perf
Browse files Browse the repository at this point in the history
  • Loading branch information
SomayChauhan committed Apr 4, 2024
1 parent 0aa47bd commit c5484e4
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/trpc/server/routers/viewer/bookings/get.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ export const getHandler = async ({ ctx, input }: GetOptions) => {
};
};

const set = new Set();
const getUniqueBookings = <T extends { uid: string }>(arr: T[]) => {
const unique = arr.filter((booking) => {
const duplicate = set.has(booking.uid);
set.add(booking.uid);
return !duplicate;
const getUniqueBookings = <T extends { id: number }>(arr: T[]) => {
const unique = new Set<number>();
arr.forEach((booking) => {
unique.add(booking.id);
});
set.clear();
return unique;
return Array.from(unique.values());
};

export async function getBookings({
Expand Down Expand Up @@ -379,7 +376,7 @@ export async function getBookings({
AND: [passedBookingsStatusFilter, ...filtersCombined],
},
orderBy,
select: { uid: true, id: true },
select: { id: true },
take: take + 1,
skip,
}),
Expand Down Expand Up @@ -454,7 +451,7 @@ export async function getBookings({
await prisma.booking.findMany({
where: {
id: {
in: plainBookings.map((booking) => booking.id),
in: plainBookings,
},
},
select: bookingSelect,
Expand Down

0 comments on commit c5484e4

Please sign in to comment.