convex-js
convex-js copied to clipboard
request: add populate method
I have this case that I think is applicable to a lot of people:
Below is a table in my schema:
messages: defineTable({
room: v.id("rooms"),
replyOf: v.id("messages"),
content: v.union(
v.object({
type: v.literal("text"),
text: v.string(),
}),
v.object({
type: v.literal("image"),
thumbnailUrl: v.string(),
url: v.string(),
}),
v.object({
type: v.literal("video"),
url: v.string(),
thumbnailUrl: v.string(),
duration: v.number(),
}),
v.object({
type: v.literal("voice"),
url: v.string(),
duration: v.number(),
}),
v.object({
type: v.literal("location"),
location: v.object({
latitude: v.float64(),
longitude: v.float64(),
}),
}),
),
}),
and here is a function that paginates messages
export const getRoomMessages = query({
args: { pager: paginationOptsValidator, roomId: v.id("rooms") },
handler: async (ctx, { pager, roomId }) => {
const rooms = await ctx.runQuery(api.services.chat.getUserRooms);
const isUserPartOfRoom =
rooms.find((room) => room._id === roomId) !== undefined;
if (!isUserPartOfRoom)
throw new ConvexError("User is not in requested room");
return await ctx.db
.query("messages")
.filter((q) => q.eq(q.field("room"), roomId))
.paginate(pager);
},
});
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