Yvens
Yvens9mo ago

Optional one to many relationship (convex-ents)

Hello, Is there a way to make an optional one to many relationship using egdes ? I would like to affect users to a group or no group.
users: defineEnt({})
.edge("user_group", { field: "groupId" }),
user_groups: defineEnt({})
.edges("users", { ref: "groupId" })
users: defineEnt({})
.edge("user_group", { field: "groupId" }),
user_groups: defineEnt({})
.edges("users", { ref: "groupId" })
`
6 Replies
Michal Srb
Michal Srb9mo ago
Not at the moment, but you're not the first to ask! Tracking issue: https://github.com/xixixao/convex-ents/issues/5 You can model this atm without Ents (via an ID field) or as a many:many edge.
GitHub
1:many edges require single cardinality end to be required · Issue ...
This will require more configuration, especially for cascading deletes. We'll hold off on adding this until we get people asking for it.
Yvens
YvensOP9mo ago
@Michal Srb ok, if I model it without ents, i presume that I won't be able to use the edge methods on this relationship too right ? you answered so fast thank you
Michal Srb
Michal Srb9mo ago
I'd probably go with many:many, it will give you the flexibility to later change your design to allow users to be attached to multiple groups.
Yvens
YvensOP9mo ago
Ok thanks for the advice Hello @Michal Srb any idea on how to replace search since I can't use withSearchIndex with ctx.table ? I mean in convex way. For now I will implement it myself
ampp
ampp9mo ago
It's so funny i was just researching this stuff just now. The saas-starter has a .searchIndex. Its being used on members.ts like this.
const query =
search === ""
? ctx.table("teams").getX(teamId).edge("members")
: ctx
.table("members")
.search("searchable", (q) =>
q
.search("searchable", normalizeStringForSearch(search))
.eq("teamId", teamId)
);
const query =
search === ""
? ctx.table("teams").getX(teamId).edge("members")
: ctx
.table("members")
.search("searchable", (q) =>
q
.search("searchable", normalizeStringForSearch(search))
.eq("teamId", teamId)
);
I'm trying to use the complex index and search filtering in the fullstack convex template as guidance and with similar complexity so I'm struggling through trying to do it in ents.
Yvens
YvensOP9mo ago
@ampp ohhh wow I didn't know about that, let me take a look

Did you find this page helpful?