-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request: add populate method #19
Comments
Populate sounds like convenient sugar for doing the DB lookup and replacing the foreign key in the object with the object the foreign key refers to, so doing this in fewer lines of code? Typically this is done today by building objects with JavaScript, but some syntax explorations in https://labs.convex.dev/convex-ents show something like populate. Is this right, you're looking for a shorter way to write this code? I'm confused because this code doesn't seem to be populating, it's just returning the relevant messages for the room ID. Also note that you probably want to use indexes here for efficiency. You might find these articles useful |
Yes your right, I should've shown this code which actually would benefit from populate const messages: Message[] = await Promise.all(
result.page.map(async (message, i) => {
const previousMessage = result.page[i - 1];
const isLastMessageInChain =
!previousMessage || previousMessage.sender !== message.sender;
const { content: raw } = message;
const content = await (async () => {
switch (raw.type) {
case "video":
case "image":
return {
...raw,
url: await ctx.storage.getUrl(raw.id),
thumbnailUrl: await ctx.storage.getUrl(raw.thumbnailId),
};
case "audio":
return {
...raw,
url: await ctx.storage.getUrl(raw.id),
};
case "text":
case "location":
return raw;
}
})();
if (!message.replyOf) {
return { ...message, isLastMessageInChain } as Message;
}
const replyOf = await ctx.db.get(message.replyOf);
if (!replyOf) return { ...message, isLastMessageInChain } as Message;
return {
...message,
replyOf,
content,
isLastMessageInChain,
} as Message;
}),
);
return {
...result,
page: messages,
};
}, specifically the replyOf section const replyOf = await ctx.db.get(message.replyOf);
if (!replyOf) return { ...message, isLastMessageInChain } as Message;
return {
...message,
replyOf,
content,
isLastMessageInChain,
} as Message;
this could've been much easier if .populate("replyOf") existed, exactly as you described. |
I have this case that I think is applicable to a lot of people:
Below is a table in my schema:
and here is a function that paginates messages
it would be really cool if in the paginate or query methods there existed a populate method that i could use to populate the replyOf id field, similar to mongoose and mongo db
The text was updated successfully, but these errors were encountered: