Jordan
Jordan
CCConvex Community
Created by Jordan on 1/23/2025 in #support-community
filter index based on multiple possible values?
consider the following query, where we want to select all posts made by a user, with a particular status of pending. due to lots of users, and lots of posts per user, we have an index set up to index posts by ["userId", "status"]. therefore, we query like so:
const userPosts = await ctx.db
.query("posts")
.withIndex("by_userId_status", (q) => q.eq("userId", user?._id).eq('status', 'pending')
.collect();
const userPosts = await ctx.db
.query("posts")
.withIndex("by_userId_status", (q) => q.eq("userId", user?._id).eq('status', 'pending')
.collect();
now consider we want to allow querying based off multiple status values. lets, select all posts made by a given user that are pending or in_progress. we would have to write the following:
const userPosts = await ctx.db
.query("posts")
.withIndex("by_userId_status", (q) => q.eq("userId", user?._id).eq('status', 'pending').eq('status', 'in_progress))
.collect();
const userPosts = await ctx.db
.query("posts")
.withIndex("by_userId_status", (q) => q.eq("userId", user?._id).eq('status', 'pending').eq('status', 'in_progress))
.collect();
but what if we wanted to make list of statuses an argument, that could be passed from the FE (i.e. a user filters their own posts down to any combination of pending, in_progress completed or cancelled in order to do this, we currently have to use the convex-helper filter or a basic TS array-filter after we query the results from the index. but cannot actively filter out within withIndex , which defeats the purpose of a by_userId_status index. it would look something like this:
const userPosts = await ctx.db
.query("posts")
.withIndex("by_userId_status", (q) => q.eq("userId", user?._id)
.collect();

// where args.status is of type: v.array(v.string())
return userPosts.filter(({status})=>args.status.includes(status)))
const userPosts = await ctx.db
.query("posts")
.withIndex("by_userId_status", (q) => q.eq("userId", user?._id)
.collect();

// where args.status is of type: v.array(v.string())
return userPosts.filter(({status})=>args.status.includes(status)))
how might we achieve the desired performance with the approach of using arguments to index off of?
15 replies
CCConvex Community
Created by Jordan on 12/7/2024 in #support-community
Any idea why `NEXT_PUBLIC_CONVEX_URL` is required in a vercel deployment of using convex auth?
this caused many hours of investigation and confusion. for some reason, upon deploying from vercel (preview & prod), NEXT_PUBLIC_CONVEX_URL was required ENV. there was an application server error due to it being missing. I assume triggered via convex-auth provider. During local development, this was never needed. My local .env did not include it. I did have NEXT_PUBLIC_CONVEX_CLOUD_URL which what was used when initializing the client auth via const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_CLOUD_URL!); however, no reference to NEXT_PUBLIC_CONVEX_URL within the project. we added the ENV and things started working in preview deployments, then forgot about it. turns out having that random ENV set incorrectly in our Prod environments caused auth to not work in production. However, everything failed silently. Convex logs indicated 200 success responses from the user creation, but no user was ever created. after remembering that random ENV that was required, we went and fixed the typo and everything works now. frustrating, and dont need help solving that issue anymore... but i am very confused and looking for insight, there's no mention of that being required in the auth provider docs.
10 replies