oscklmO
Convex Community3y ago
15 replies
oscklm

"expand" a field on a document, that is array of ids?

I'm trying to figure out if there is a easy way to go about this?

My video table:
export const videoValidator = {
  title: v.string(),
  description: v.optional(v.string()),
  tags: v.optional(v.string()),
  category: v.optional(v.string()),
  views: v.number(),
  likesCount: v.number(),
  likes: v.optional(v.array(v.id('likes'))),
  visibility: v.string(),
  tasks: v.optional(v.array(v.id('tasks'))),
  muxAssetId: v.optional(v.string()),
  muxMetaData: muxMetaDataValidator,
};


And i'm wondering how to best get the tasks, when i query a video that gets displayed in a detailed view in my app, where i then pass the tasks to a component that renders them in a list below the video.

What i'm doing right now:
export const getOneVideoWithTasks = query({
  args: { id: v.id('videos') },
  handler: async (ctx, args) => {
    const video = await ctx.db.get(args.id);
    const tasks = await ctx.db
      .query('tasks')
      .withIndex('by_video', (q) => q.eq('videoId', args.id))
      .order('desc')
      .take(10);
    return {
      ...video,
      tasks,
    };
  },
});


The relation and setup i'm after here:
A video can have many tasks, but a task does not store a specific video id, like its doing right now. And when i then query a video i want to be able to 'expand' the tasks off the video and get their data and not only their ids
Was this page helpful?