Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Firestore - Get pagination cursor from denormalized data

I have a paginated list of posts, where my cursor is re-calculated on each fetch, assigning the last QuerySnapshot document to it:

// Calculate the new startAfter
if (querySnapshot.size) {
  startAfter = querySnapshot.docs[querySnapshot.docs.length - 1];
}

Somehow, using doc snapshots as cursors is one of the most "secure" ways to avoid breaking paginations.

I am thinking about optimizing some parts of my app with denormalizations. For example, as each user of my app can upload infinite posts that are shown on their profiles 10 by 10, it might be a good idea to add the last 10 user posts (denormalization) to the user's document (an array field).

With this, I avoid making the first 10 post fetch of each visited profile. But... how do I assign the first startAfter cursor? I mean, is it possible to write a doc snapshot in a document or something like that to get the same behavior?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you look at the documentation for startAfter, you'll see there are two overloads:

startAfter ( snapshot : DocumentSnapshot < any > ) : Query < T >

Creates and returns a new Query that starts after the provided document (exclusive). The starting position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.

Parameters

snapshot: DocumentSnapshot<any>

The snapshot of the document to start after.

And

startAfter ( ... fieldValues : any [] ) : Query < T >

Creates and returns a new Query that starts after the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.

Parameters

Rest ...fieldValues: any[]

The field values to start this query after, in order of the query's order by.

Since you don't have a DocumentSnapshot, you can use the second overload and pass the relevant field values.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!