© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago•
32 replies
harry

Duplicates when Paginating

When using the following pagination code there are some duplicates,
  export let data: PageData;

  let hasMore = true;
  let perPage = 50;
  let page = -1;
  let messages:
    | {
        id: string;
        profile_id: string;
        repository_id: string;
        content: string;
        created_at: string;
        updated_at: string;
        flags: number;
      }[] = [];

  const loadMoreMessages = async ({
    detail: { loaded, complete },
  }: {
    detail: StateChanger;
  }) => {
    console.log("loading....");

    page++;

    const limits = getPagination(page, perPage)
    console.log(limits);
    let newMessages = await supabaseClient
      .from("messages")
      .select("*")
      .eq("repository_id", data.repo?.id ?? "-")
      .order("created_at", { ascending: false })
      .range(limits.from, limits.to);

    if ((newMessages.data?.length ?? perPage) < perPage) {
      complete();
      console.log("exceqd");
      console.log(messages);
      hasMore = false;
      return;
    }

    messages = [...((newMessages.data ?? []).reverse()), ...messages];
    loaded();
  };

  export const getPagination = (page: number, size: number) => {
    const limit = size ? +size : 25;
    const from = page ? page * limit : 0;
    const to = page ? from + size : size;

    return { from, to };
  };
  export let data: PageData;

  let hasMore = true;
  let perPage = 50;
  let page = -1;
  let messages:
    | {
        id: string;
        profile_id: string;
        repository_id: string;
        content: string;
        created_at: string;
        updated_at: string;
        flags: number;
      }[] = [];

  const loadMoreMessages = async ({
    detail: { loaded, complete },
  }: {
    detail: StateChanger;
  }) => {
    console.log("loading....");

    page++;

    const limits = getPagination(page, perPage)
    console.log(limits);
    let newMessages = await supabaseClient
      .from("messages")
      .select("*")
      .eq("repository_id", data.repo?.id ?? "-")
      .order("created_at", { ascending: false })
      .range(limits.from, limits.to);

    if ((newMessages.data?.length ?? perPage) < perPage) {
      complete();
      console.log("exceqd");
      console.log(messages);
      hasMore = false;
      return;
    }

    messages = [...((newMessages.data ?? []).reverse()), ...messages];
    loaded();
  };

  export const getPagination = (page: number, size: number) => {
    const limit = size ? +size : 25;
    const from = page ? page * limit : 0;
    const to = page ? from + size : size;

    return { from, to };
  };

duplicates:
[
    "Example Message: 195",
    "Example Message: 145",
    "Example Message: 100",
    "Example Message: 96",
    "Example Message: 95",
    "Example Message: 50",
    "Example Message: 47",
    "Example Message: 48",
    "Example Message: 49"
]
[
    "Example Message: 195",
    "Example Message: 145",
    "Example Message: 100",
    "Example Message: 96",
    "Example Message: 95",
    "Example Message: 50",
    "Example Message: 47",
    "Example Message: 48",
    "Example Message: 49"
]
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Paginating two joined tables with range()
SupabaseSSupabase / help-and-questions
4y ago
Paginating a table with getStaticProps in Next.js
SupabaseSSupabase / help-and-questions
4y ago
Insert filter to void duplicates?
SupabaseSSupabase / help-and-questions
4y ago
unable to find user from email identity for duplicates
SupabaseSSupabase / help-and-questions
3y ago