Web Dev Cody
Web Dev Cody13mo ago

Ents - How to define composite index?

I'm not sure how I can do composite indexes? I have a reactions table which I want to find all reactions by memeId and userId
5 Replies
thedevstockgirl
thedevstockgirl13mo ago
I think something like this.
No description
Web Dev Cody
Web Dev CodyOP13mo ago
is this the way I should be doing it?
const reactions = await ctx
.table("memes")
.getX(args.memeId)
.edge("reactions");

const reaction = reactions.find((r) => r.userId === user.subject);
const reactions = await ctx
.table("memes")
.getX(args.memeId)
.edge("reactions");

const reaction = reactions.find((r) => r.userId === user.subject);
I'm using Ents not the stanrdard convex approach
thedevstockgirl
thedevstockgirl13mo ago
Ah. Ok. Not sure.
Indy
Indy13mo ago
I am not familiar with ents. But I think fundamentally if you want to query the "reactions" table with a different index then you'd have to query that table directly. I believe all .edge is doing underneath is using the indexes Ents creates automatically to query the necessary tables. Otherwise you're trying to use two indexes at once to query the table. @Michal Srb can correct my understanding
Michal Srb
Michal Srb13mo ago
@Web Dev Cody you can either traverse one of the edges, or create an index as @thedevstockgirl suggested. With Ents you can do practically everything you can do with Convex, and sometimes a custom index is the way to go. I did the same with the "teamAndUser" index on my "members" table in the saas-starter. If "reactions" don't have any fields besides memeId and userId though, you can model them as a many:many edge between memes and users. You can then do
const hasReaction = ctx.table("memes").getX(args.memeId).edge("users").has(user._id)
const hasReaction = ctx.table("memes").getX(args.memeId).edge("users").has(user._id)
This will use the "composite" index under the hood (but only returns a boolean, since there's currently no way to have other information stored on the many:many edge). I'll think about if I can unlock this use case as I it's probably fairly common.

Did you find this page helpful?