From 25cb787f6710d7bfc8981ecc337e753545598716 Mon Sep 17 00:00:00 2001 From: Ashutoshbind15 Date: Fri, 18 Oct 2024 19:46:59 +0530 Subject: [PATCH] index on the admin table --- app/play/page.tsx | 1 - convex/maps.ts | 16 ++++++++++------ convex/schema.ts | 2 +- convex/users.ts | 7 +++++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/play/page.tsx b/app/play/page.tsx index b1f1fa5..c7d7749 100644 --- a/app/play/page.tsx +++ b/app/play/page.tsx @@ -106,7 +106,6 @@ export default function PlayPage() {
{filteredMaps.map((map) => ( - admin.userId === userId); + const admin = await ctx.db + .query("admins") + .withIndex("by_userId", (q) => q.eq("userId", userId)) + .first(); - if (isAdmin) { + if (admin) { return await ctx.db .query("maps") .filter((q) => q.eq(q.field("isReviewed"), args.isReviewed)) @@ -250,10 +252,12 @@ export const approveMap = mutation({ throw new Error("Unauthorized"); } - const admins = await ctx.db.query("admins").collect(); - const isAdmin = admins.some((admin) => admin.userId === userId); + const admin = await ctx.db + .query("admins") + .withIndex("by_userId", (q) => q.eq("userId", userId)) + .first(); - if (!isAdmin) { + if (!admin) { throw new Error("Unauthorized"); } else { const maps = await ctx.db.query("maps").collect(); diff --git a/convex/schema.ts b/convex/schema.ts index 97fc31b..6cabf20 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -64,5 +64,5 @@ export default defineSchema({ }).index("by_mapId_userId", ["mapId", "userId"]), admins: defineTable({ userId: v.id("users"), - }), + }).index("by_userId", ["userId"]), }); diff --git a/convex/users.ts b/convex/users.ts index 9d25d0d..e68413e 100644 --- a/convex/users.ts +++ b/convex/users.ts @@ -34,8 +34,11 @@ export const isAdmin = query({ return false; } - const admins = await ctx.db.query("admins").collect(); + const admin = await ctx.db + .query("admins") + .withIndex("by_userId", (q) => q.eq("userId", userId)) + .first(); - return admins.some((admin) => admin.userId === userId); + return !!admin; }, });