paginate()

Returns the first page of results of a Set as an Object.

In most cases, you should use pageSize() instead of paginate(). See Differences with pageSize().

Signature

paginate(): Object<A>

paginate(size: Int): Object<A>

Description

The paginate() method paginates the calling Set. It returns the first page of size Set values as an Object. When size is omitted, the default page size is 16.

When the Set has more than size values, a pagination cursor is included as the last entry on the page. Use the Set.paginate() method with the provided cursor to sequentially access subsequent pages.

The pagination cursor is stable in the sense that pagination through a set is done for a fixed snapshot time, giving you a view of your data as it existed across the whole set at the instant you started paginating. For example, given set [a, b, c] when you start paginating, one item at a time, even if you delete item c after you started reading the set, item c is returned.

The exception is if the history is no longer available for the deleted item because history_days is set to the default value of 0 or is less than the minimum valid time needed. In that case, the deleted item is not returned with the paginated results and an error is returned:
Requested timestamp <time> less than minimum allowed timestamp..

A cursor is valid for history_days plus 15 minutes.

Differences with pageSize()

pageSize() is similar to paginate(). In most cases, you should use pageSize(). pageSize() tends to produce faster queries with less compute.

Major differences between the methods include:

Parameters

Parameter Type Required Description

size

Int

Number of Set values to include in the returned page. The size parameter must be in the range 1 to 16000.

Return value

Type Description

Object that includes the following fields:

data

Array of values.

after

(optional) Cursor pointing to next page when more pages exist. The cursor is valid for history_days plus 15 minutes.

Examples

In most cases, you should use pageSize() instead of paginate(). However, paginate() is still useful if:

  • You want to access the results as an object, not a Set.

  • You need to directly access the after cursor in an FQL query.

The following example uses paginate() to update the ttl of a large collection of documents over several transactions.

The first transaction uses paginate() to fetch the results and generate an initial after cursor. Subsequent transactions use Set.paginate() to iterate through the remaining pages.

// First transaction
let page = Product.byStore(Store.byId("3927..."))
  .paginate()

// Subsequent transactions
// let page = Set.paginate("hdWDxoq...")

let data = page.data

data.forEach(document => document.update({
  // Set ttl as document timestamp + 365 days
  ttl: document.ts.add(365, "days")
}))

page {
  after
}
{
  after: "hdWC1YLGZ..."
}

Pass a size argument

Passing a size argument to paginate() is equivalent to calling paginate() on pageSize().

For example:

// Gets first 10 products
Product.sortedByPriceHighToLow()
  .paginate(10)

// Equivalent to:
// Product.sortedByPriceHighToLow()
//   .pageSize(10)
//   .paginate()

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!