Check out v4 of the Fauna CLI
v4 of the Fauna CLI is now in beta. The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start. |
set.pageSize()
Description
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.
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 |
Supports set instance methods. |
Doesn’t support set instance methods. |
Examples
Basic example
// Calls `pageSize()` with a size of `2`.
Product.all().pageSize(2)
{
// The returned Set contains two elements or fewer.
data: [
{
id: "111",
coll: Product,
ts: Time("2099-07-31T12:58:51.680Z"),
name: "cups",
description: "Translucent 9 Oz, 100 ct",
price: 698,
stock: 100,
category: Category("123")
},
{
id: "222",
coll: Product,
ts: Time("2099-07-31T12:58:51.680Z"),
name: "donkey pinata",
description: "Original Classic Donkey Pinata",
price: 2499,
stock: 50,
category: Category("123")
}
],
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:
-
Run an initial paginated query:
Product.all().pageSize(2)
{ data: [ { id: "111", coll: Product, ts: Time("2099-08-16T14:00:59.075Z"), name: "cups", description: "Translucent 9 Oz, 100 ct", price: 698, stock: 100, category: Category("123") }, { id: "222", coll: Product, ts: Time("2099-08-16T14:00:59.075Z"), name: "donkey pinata", description: "Original Classic Donkey Pinata", price: 2499, stock: 50, category: Category("123") } ], after: "hdW..." }
-
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", description: "Frozen Cheese", price: 499, stock: 100, category: Category("456") }, { // Begin reverse pagination from this doc ID. id: "444", coll: Product, ts: Time("2099-08-16T14:00:59.075Z"), name: "avocados", description: "Conventional Hass, 4ct bag", price: 399, stock: 1000, category: Category("789") } ], after: "hdW..." }
-
To reverse paginate, run the original query with:
-
A range search with a
to
argument containing the previous document ID. -
set.reverse()
: Append this to the query. -
set.pageSize()
: If used, place it afterset.reverse()
.
// "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", description: "Conventional Hass, 4ct bag", price: 399, stock: 1000, category: Category("789") }, { id: "333", coll: Product, ts: Time("2099-08-16T14:00:59.075Z"), name: "pizza", description: "Frozen Cheese", price: 499, stock: 100, category: Category("456") } ], 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) }
-
-
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!