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

Paginated query with order by #127

Open
Daplex opened this issue Feb 27, 2024 · 3 comments
Open

Paginated query with order by #127

Daplex opened this issue Feb 27, 2024 · 3 comments
Labels
Bug Something isn't working

Comments

@Daplex
Copy link

Daplex commented Feb 27, 2024

Hi!

I've got an issue having a query working with a sort by.

  const dataPromise = typesaurus.companies.query(($) => [
    $.field(sortBy?.id ?? "createdAt").order(sortBy?.direction ?? "desc"),
    $.field($.docId()).order($.startAfter(after)),
    $.limit(pageSize),
  ]);

I'm trying to sort by a field and then paginate thanks to the docId of after which is the last element of the previous page.

The startAfter doesn't seem to work, maybe because there are two orderBy in the query.

I've managed to have this use case working with the firebase-admin sdk:

  let query = db
    .collection("companies")
    .orderBy(sortBy?.id ?? "createdAt", sortBy?.direction ?? "desc");

  if (after) {
    const afterDocument = await db.collection("companies").doc(after).get();
    query = query.startAfter(afterDocument);
  }

  const dataPromise = query.limit(pageSize).get();

Thanks for the help!

@kossnocorp
Copy link
Owner

Hey, @Daplex, thank you for reporting that, and sorry for the silence. I was on vacation. I will investigate and share a solution or publish an update that fixes it.

@kossnocorp kossnocorp added the Bug Something isn't working label May 13, 2024
@abubakir1997
Copy link

I am facing the same issue, the pagination doesn't work, it only repeats the same page. I tried it with vanilla firebase/firestore and passed the snapshot object and pagination worked just fine.

ProductCollection(context)
      .query(($) => [
        $.field('updatedAt').order('desc'),
        $.field($.docId()).order('desc', $.startAfter(lastProduct ? ProductId(context, lastProduct?.id) : undefined)),
        $.limit(30),
      ])

@tjx666
Copy link

tjx666 commented Sep 4, 2024

I use:

db.xxxx.query(($) => {
    return [
      $.field('created_at').order('desc'),
      ...(cursor ? [$.field($.docId()).order($.startAfter(cursor))] : []),
      $.limit(PAGE_SIZE),
    ];
  })

throw error:

The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants