Search code examples
reactjssupabase

How to set pagination for data in a joined table in Supabase


    export async function getPostDetails() {
  let { data: posts, error } = await supabase
    .from("posts")
    .select(
      "postId, postTitle, postDesc,created_at,userId, comment(commentId, id, postId, comment,created_at, profile(username, userAvatar)), likes(id, postId, isLiked)"
    );
  if (error) {
    console.log(error.message);
  }

  return posts;
}

This is my function for getting the posts, for now i did not set pagination for the posts since it will be easy using range directly but i cannot do the same with the comment since it is a joined table since i want to put pagination for the comments on each post. I am new to supabase so im looking for a better way to query or at least a solution to my problem thanks a lot


Solution

  • You can use the foreignTable option of the range() modifier like this:

        const { data, error } = await supabase
          .from('posts')
          .select(
            'postId, postTitle, postDesc,created_at,userId, comment(commentId, id, postId, comment,created_at, profile(username, userAvatar)), likes(id, postId, isLiked)'
          )
          .range(1, 2, { foreignTable: 'comment' })