#Convex Ents - how convex links edges to ents
7 messages · Page 1 of 1 (latest)
In Convex, the relationship between entities (ents) is established through edges. In your example, the users ent has an edge called profile and the profiles ent has an edge called user. These edges represent the relationships between the ents.
The ref option in the edge method is used to specify the field in the other ent that this edge refers to. In your example, the profile edge in the users ent refers to the ownerId field in the profiles ent. Similarly, the user edge in the profiles ent refers to the ownerId field in the users ent.
The names of the ents (users and profiles) and the names of the edges (profile and user) do not have to match. The relationship is established through the ref option in the edge method, not through the names of the ents or edges.
Here is the code from your example:
defineEntSchema({
users: defineEnt({
name: v.string(),
}).edge("profile", { ref: "ownerId", optional: true }),
profiles: defineEnt({
bio: v.string(),
}).edge("user", { field: "ownerId" }),
});
In this code, the users ent has an edge called profile that refers to the ownerId field in the profiles ent. The profiles ent has an edge called user that refers to the ownerId field in the users ent. This establishes a relationship between the users and profiles ents.
Please note that the information provided is based on the general understanding of Convex and ents. For more specific details, you may want to refer to the Convex Ents documentation (https://labs.convex.dev/convex-ents) or ask in the Convex community on Discord (https://convex.dev/community).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
but how does scheme know what edge relates to what entity? what if i had other ents which has owerId in there. how would the users ent know which ownerId field it needs to link to in the many ents the schema could have? @lapis roost
Let me check the knowledge base for you. One moment! :books:
In Convex, the relationship between entities (ents) is established through edges. The edge's name is used to infer the related ent. By convention, Convex assumes that the plural form of the edge's name corresponds to the related ent. For example, an edge named profile in the users ent would correspond to an ent named profiles.
However, this convention might not always hold, especially for irregular plurals or non-English words. In such cases, you can explicitly specify the related ent using the to option in the edge or edges method.
Here's an example:
defineEntSchema({
users: defineEnt({
name: v.string(),
}).edge("profile", { to: "profiles", ref: "ownerId", optional: true }),
profiles: defineEnt({
bio: v.string(),
}).edge("user", { to: "users", field: "ownerId" }),
});
In this code, the to option is used to explicitly specify that the profile edge in the users ent corresponds to the profiles ent, and that the user edge in the profiles ent corresponds to the users ent.
Please note that the information provided is based on the general understanding of Convex and ents. For more specific details, you may want to refer to the Convex Ents documentation (https://labs.convex.dev/convex-ents) or ask in the Convex community on Discord (https://convex.dev/community).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@tidal sun kapa.ai is still learning and improving, please let us know how it did by reacting below
Convex Ents - how convex links edges to entities