Optimize looking page loader DB calls

This commit is contained in:
Kalle
2022-02-10 17:45:59 +02:00
parent 9ec54b9440
commit 4ad46f1808
2 changed files with 8 additions and 4 deletions

View File

@@ -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: {

View File

@@ -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),