From 4ad46f1808d4bf41aeb72072c1420659ba0a0d2c Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Thu, 10 Feb 2022 17:45:59 +0200 Subject: [PATCH] Optimize looking page loader DB calls --- app/models/LFGGroup.server.ts | 5 +++-- app/routes/play/looking.tsx | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/LFGGroup.server.ts b/app/models/LFGGroup.server.ts index be0e4ce8a..834002550 100644 --- a/app/models/LFGGroup.server.ts +++ b/app/models/LFGGroup.server.ts @@ -163,15 +163,16 @@ export function findActiveByMember(user: { id: string }) { }); } -export function findLookingByType(type: LfgGroupType) { +export function findLooking() { return db.lfgGroup.findMany({ where: { - type, + active: true, looking: true, }, select: { id: true, ranked: true, + type: true, members: { select: { user: { diff --git a/app/routes/play/looking.tsx b/app/routes/play/looking.tsx index dfcb7c938..19d3ba48b 100644 --- a/app/routes/play/looking.tsx +++ b/app/routes/play/looking.tsx @@ -184,7 +184,10 @@ interface LookingLoaderData { export const loader: LoaderFunction = async ({ context }) => { const user = requireUser(context); - const ownGroup = await LFGGroup.findActiveByMember(user); + const [ownGroup, allGroups] = await Promise.all([ + LFGGroup.findActiveByMember(user), + LFGGroup.findLooking(), + ]); if (!ownGroup) return redirect("/play"); if (ownGroup.matchId) return redirect(`/play/match/${ownGroup.matchId}`); if (!ownGroup.looking) return redirect("/play/add-players"); @@ -193,7 +196,7 @@ export const loader: LoaderFunction = async ({ context }) => { ownGroup.type === "VERSUS" && ownGroup.members.length === LFG_GROUP_FULL_SIZE; - const groups = await LFGGroup.findLookingByType(ownGroup.type); + const groups = allGroups.filter((g) => g.type === ownGroup.type); const likesGiven = ownGroup.likedGroups.reduce( (acc, lg) => acc.add(lg.targetId),