set.pageSize()

Learn: Pagination

Sets the maximum elements per page in paginated results.

Lazy loading:

Yes

Signature

pageSize(size: Int): Set<A>

Description

The pageSize() method sets the maximum elements per page in paginated results.

If a subsequent page is available, the result includes an after cursor. To iterate through paginated results, pass the after cursor to Set.paginate().

Method chaining

pageSize() should typically be the last method call in an FQL expression.

pageSize() only affects the rendering of a set, not subsequent operations. Methods chained to pageSize() access the entire calling set, not a page of results.

after cursor

Differences with paginate()

The following table outlines differences between set.pageSize() and set.paginate():

Difference set.pageSize() set.paginate()

Use case

Use in most cases.

Use when needing to access an 'after' cursor or paginated results within an FQL query.

Return type

Returns a set.

Returns an object.

Loading strategy

Lazy loading. Only fetches results as needed.

Eager loading. Fetches results instantly, even if the results aren’t returned or used.

Client driver methods

Compatible with driver pagination methods.

Incompatible with driver pagination methods.

Projection

Supports projections.

Doesn’t support projections.

Set instance methods

Doesn’t support set instance methods.

Parameters

Parameter Type Required Description

size

Int

Yes

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

Return value

Type Description

Set

Set that includes the following field:

after

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

Examples

Basic example

// Calls `pageSize()` with a size of `2`.
Product.all().pageSize(2)
{
  // The returned set contains two elements or fewer.
  data: [
    {
      id: "<PRODUCT_DOCUMENT_ID>",
      coll: Product,
      ts: Time("2099-07-31T12:58:51.680Z"),
      name: "cups",
      description: "Translucent 9 Oz, 100 ct",
      price: 698,
      stock: 100,
      category: Category("<CATEGORY_DOCUMENT_ID>")
    },
    {
      id: "<PRODUCT_DOCUMENT_ID>",
      coll: Product,
      ts: Time("2099-07-31T12:58:51.680Z"),
      name: "donkey pinata",
      description: "Original Classic Donkey Pinata",
      price: 2499,
      stock: 50,
      category: Category("<CATEGORY_DOCUMENT_ID>")
    }
  ],
  after: "hdW..."
}

Paginate in reverse

Paginated queries don’t include a before cursor. Instead, you can use a range search and document IDs or other unique field values to paginate in reverse. For example:

  1. Run an initial paginated query:

    Product.all().pageSize(2)
    {
      data: [
        {
          id: "111",
          coll: Product,
          ts: Time("2099-08-16T14:00:59.075Z"),
          name: "cups",
          ...
        },
        {
          id: "222",
          coll: Product,
          ts: Time("2099-08-16T14:00:59.075Z"),
          name: "donkey pinata",
          ...
        }
      ],
      after: "hdW..."
    }
  2. Page forward until you find the document you want to start reversing from:

    Set.paginate("hdW...")

    Copy the ID of the document:

    {
      data: [
        {
          id: "333",
          coll: Product,
          ts: Time("2099-08-16T14:00:59.075Z"),
          name: "pizza",
          ...
        },
        {
          // Begin reverse pagination from this doc ID.
          id: "444",
          coll: Product,
          ts: Time("2099-08-16T14:00:59.075Z"),
          name: "avocados",
          ...
        }
      ],
      after: "hdW..."
    }
  3. To reverse paginate, run the original query with:

    // "444" is the ID of the document to reverse from.
    Product.all({ to: "444" }).reverse().pageSize(2)
    {
      data: [
        {
          // The results of the previous query are reversed.
          id: "444",
          coll: Product,
          ts: Time("2099-08-16T14:00:59.075Z"),
          name: "avocados",
          ...
        },
        {
          id: "333",
          coll: Product,
          ts: Time("2099-08-16T14:00:59.075Z"),
          name: "pizza",
          ...
        }
      ],
      after: "hdW..."
    }

    To get historical snapshots of documents at the time of the original query, use an at expression:

    // Time of the original query.
    let originalQueryTime = Time.fromString("2099-08-16T14:30:00.000Z")
    at (originalQueryTime) {
      // "444" is the ID of the document to reverse from.
      Product.all({ to: "444" }).reverse().pageSize(2)
    }
  4. Repeat the previous step to continue paginating in reverse:

    Product.all({ to: "333" }).reverse().pageSize(2)

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!