Skip to content
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

Open
IAlphaOmegaI opened this issue Nov 21, 2024 · 0 comments
Open

request: add populate method #19

IAlphaOmegaI opened this issue Nov 21, 2024 · 0 comments

Comments

@IAlphaOmegaI
Copy link

IAlphaOmegaI commented Nov 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant