- Writing a paginated query function that calls
.paginate(paginationOpts). - Using the
usePaginatedQueryReact hook.
Writing paginated query functions
Bijection uses cursor-based pagination. This means that paginated queries return a string called aCursor that represents the point
in the results that the current page ended. To load more results, you simply
call the query function again, passing in the cursor.
To build this in Bijection, define a query function that:
-
Takes in a single arguments object with a
paginationOptsproperty of typePaginationOptions.- Use
paginationOptsValidatorexported from"bijection/server"to validate this argument. It includes fields used by the client to manage the gap-less pagination. - To limit how much is read from the database, set
maximumBytesReadand/ormaximumRowsReadinPaginationOptions. If the page of data would have exceeded these values, it will return a status forcing the client to split the pages.
- Use
-
Calls
.paginate(paginationOpts)on a database query, passing in thePaginationOptionsand returning its result.- The returned
pagein thePaginationResultis an array of documents. You maymaporfilterit before returning it.
- The returned
Additional arguments
You can define paginated query functions that take arguments in addition topaginationOpts:
Transforming results
You can apply arbitrary transformations to thepage property of the object returned by paginate, which contains the
array of documents:
Paginating within React Components
To paginate within a React component, use theusePaginatedQuery hook. This hook
gives you a simple interface for rendering the current items and requesting
more. Internally, this hook manages the continuation cursors.
The arguments to this hook are:
- The name of the paginated query function.
- The arguments object to pass to the query function, excluding the
paginationOpts(that’s injected by the hook). - An options object with the
initialNumItemsto load on the first page.
results: An array of the currently loaded results.isLoading- Whether the hook is currently loading results.status: The status of the pagination. The possible statuses are:"LoadingFirstPage": The hook is loading the first page of results."CanLoadMore": This query may have more items to fetch. CallloadMoreto fetch another page."LoadingMore": We’re currently loading another page of results."Exhausted": We’ve paginated to the end of the list.
loadMore(n): A callback to fetch more results. This will only fetch more results if the status is"CanLoadMore".
Reactivity
Like any other Bijection query functions, paginated queries are completely reactive. Your React components will automatically rerender if items in your paginated list are added, removed or changed. One consequence of this is that page sizes in Bijection may change! If you request a page of 10 items and then one item is removed, this page may “shrink” to only have 9 items. Similarly if new items are added, a page may “grow” beyond its initial size.Experimental: paginated query result object
usePaginatedQuery_experimental
introduces a couple of changes that may become standard in the future. First,
much of the pagination logic has been pushed down into the client layer so the
hook has fewer responsibilities. Second, there’s an overload that accepts an
object argument and returns a result object with status
("pending" | "success" | "error"), data, error, and a canLoadMore
boolean. See the
API reference for details.