How can I grab an all linked ents from a linked ent

Hi, this is my schema:
const schema = defineEntSchema({
privateChats: defineEnt({})
.field("support", v.boolean(), { default: false })
.edges("users", { to: "users" }),
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.edges("chats", { to: "privateChats" }),
});
const schema = defineEntSchema({
privateChats: defineEnt({})
.field("support", v.boolean(), { default: false })
.edges("users", { to: "users" }),
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.edges("chats", { to: "privateChats" }),
});
I have a user identifier. Now I want to grab every chat of this user and the information about the others in the chat. How can I do that? I tried something like that, but it obviously did not work:
// Retrieve the user entity by its ID
const user = await ctx
.table("users")
.getX("clerkId", identity.tokenIdentifier);

// Traverse the edge to get the chats linked to the user
const chats = await user.edgeX("chats").docs();

return chats.map(async (chat) => ({
...chat,
users: await chat.edge("users"),
}));
// Retrieve the user entity by its ID
const user = await ctx
.table("users")
.getX("clerkId", identity.tokenIdentifier);

// Traverse the edge to get the chats linked to the user
const chats = await user.edgeX("chats").docs();

return chats.map(async (chat) => ({
...chat,
users: await chat.edge("users"),
}));
3 Replies
Michal Srb
Michal Srb•11mo ago
I think you were pretty close:
const chatsWithParticipants = await ctx
.table("users")
.getX("clerkId", identity.tokenIdentifier)
.edge("chats")
.map(async (chat) => ({
...chat,
users: await chat.edge("users")
});
const chatsWithParticipants = await ctx
.table("users")
.getX("clerkId", identity.tokenIdentifier)
.edge("chats")
.map(async (chat) => ({
...chat,
users: await chat.edge("users")
});
FleetAdmiralJakob 🗕 🗗 🗙
looks good to me what's the difference between edge and edgeX and get and getX?

Did you find this page helpful?