Swiftpack.co -  Appsaurus/CursorPagination as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Opaque cursor pagination for Vapor's Fluent.
.package(url: "https://github.com/Appsaurus/CursorPagination.git", from: "0.1.5")


Swift Vapor Swift Package Manager License

CursorPagination is a library for Vapor's Fluent that allows you to paginate queries using an opaque cursor. If you are looking for offset based pagination for Fluent, check out Pagination. If you're not sure what cursor pagination is, or whether you need cursor or offset pagination, I recommend this great writeup from the Slack team explaining the difference between the two, and the pros and cons of each.


CursorPagination is available through Swift Package Manager. To install, add the following to your Package.swift file.

let package = Package(
    name: "YourProject",
    dependencies: [
        .package(url: "https://github.com/Appsaurus/CursorPagination", from: "0.1.0"),
    targets: [
      .target(name: "YourApp", dependencies: ["CursorPagination", ... ])


Check the app included in this project for a complete example. Here are some of the basics:

1. Import the library

import CusorPagination

2. Extend your model class to adopt CusorPaginatable protocol

You can simply declare the protocol adoption and inherit the default implementations.

extension ExampleModel: CursorPaginatable{}

Or you can set some default configurations by implementing any of the following static class vars.

extension ExampleModel: CursorPaginatable{
	public static var defaultPageSorts: [CursorSort<ExampleModel>] {
		return [idKey.descendingSort]
	public static var defaultPageSize: Int {
		return 20

	public static var maxPageSize: Int? {
		return 50

3. Setup your routes

Setup routes to return a Future<CursorPage<YourModel>>. When you run your query, simply call paginate(request: sorts: ) on your class or on a QueryBuilder.

router.get("modelsByDate") { request -> Future<CursorPage<ExampleModel>> in
	return try ExampleModel.paginate(request: request,
					 sorts: .descending(\.dateField))

On your first request, omitting a cursor tells the method that you are starting at the first page.

curl "http://localhost:8080/modelsByDate?limit=5"

Results in:

         "stringField":"Hammes Glen",
         "stringField":"Stracke Green",
         "stringField":"Schowalter Branch",
         "stringField":"Ignacio Springs",
         "stringField":"Braun Rapid",

Then use the nextPageCursor in your next request:

curl "http://localhost:8080/modelsByDate?limit=5&cursor=W3sia2V5IjoiZGF0ZUZpZWxkIiwidmFsdWUiOjMzODQ2Nzc4NjgsImRpcmVjdGlvbiI6ImRlc2NlbmRpbmcifSx7ImtleSI6ImlkIiwidmFsdWUiOjQ2LCJkaXJlY3Rpb24iOiJhc2NlbmRpbmcifV0="

         "stringField":"Aditya Crossroad",
         "stringField":"Emmitt Ridges",
         "stringField":"Evelyn Rest",
         "stringField":"Fisher Trail",
         "stringField":"Paucek Plains",

When there are no more results, a cursor will not be returned.

Compound Sorts

Sorting on multiple properties works as well.


In order to break ties, the last sort must be on a unique property, otherwise a sort on a default unique property (Fluent id) will be applied.

router.get("modelsByBooleanAndString") { request -> Future<CursorPage<ExampleModel>> in
	return try ExampleModel.paginate(request: request,
					 sorts: .descending(\.booleanField), .ascending(\.stringField))

Dynamic Sorting

You can allow the client to dynamically dictate how the results are sorted via query parameters.


Because the dynamic sorting API has no way to resolve KeyPaths from string based parameters, it uses runtime reflection to build the cursor. You may not want to use this API in production until Swift ABI is stable.

Setup a dynamically sortable request handler like so:

router.get("dynamicModels") { request -> Future<CursorPage<ExampleModel>> in
	return try ExampleModel.paginate(dynamicRequest: request)

Then in your request, each sorts and orders via the sort[] and order[] parameters respectively (order matters): curl "http://localhost:8080/dynamicModels?limit=5&sort[]=booleanField&order[]=descending&sort[]=stringField&order[]=ascending"


  • ☑ Optional support
  • ☑ Compound sorts
  • ☑ Dynamic sorting
  • ☑ Allow for additional filters on queries
  • ☐ Aggregate support (might be some use cases here)
  • ☐ Nil sort order preference
  • ☐ Allow for customization of CursorPage's JSON structure
  • ☐ Integrate CircleCI
  • ☐ Explore database specific optimized implementations (benchmark first to see if needed)


We would love you to contribute to CursorPagination, check the CONTRIBUTING file for more info.


CursorPagination is available under the MIT license. See the LICENSE file for more info.


Stars: 2
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.


Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics