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.
Installation
CursorPagination is available through Swift Package Manager. To install, add the following to your Package.swift file.
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 KitchenSink: CursorPaginatable{}
Or you can set some default configurations by implementing any of the following static class vars.
extension KitchenSink: CursorPaginatable{
public static var defaultPageSorts: [CursorSort<KitchenSink>] {
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.
You can allow the client to dynamically dictate how the results are sorted via query parameters.
⚠️ WARNING:
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:
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"
TODO
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)
Contributing
We would love you to contribute to CursorPagination, check the CONTRIBUTING file for more info.
License
CursorPagination is available under the MIT license. See the LICENSE file for more info.
CursorPagination
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.
Installation
CursorPagination is available through Swift Package Manager. To install, add the following to your Package.swift file.
Usage
Check the app included in this project for a complete example. Here are some of the basics:
1. Import the library
2. Extend your model class to adopt CusorPaginatable protocol
You can simply declare the protocol adoption and inherit the default implementations.
Or you can set some default configurations by implementing any of the following static class vars.
3. Setup your routes
Setup routes to return a
Future<CursorPage<YourModel>>
. When you run your query, simply callpaginate(request: sorts: )
on your class or on a QueryBuilder.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:
Then use the
nextPageCursor
in your next request:curl "http://localhost:8080/modelsByDate?limit=5&cursor=W3sia2V5IjoiZGF0ZUZpZWxkIiwidmFsdWUiOjMzODQ2Nzc4NjgsImRpcmVjdGlvbiI6ImRlc2NlbmRpbmcifSx7ImtleSI6ImlkIiwidmFsdWUiOjQ2LCJkaXJlY3Rpb24iOiJhc2NlbmRpbmcifV0="
When there are no more results, a cursor will not be returned.
Compound Sorts
Sorting on multiple properties works as well.
NOTE:
Dynamic Sorting
You can allow the client to dynamically dictate how the results are sorted via query parameters.
⚠️ WARNING:
Setup a dynamically sortable request handler like so:
Then in your request, each sorts and orders via the
sort[]
andorder[]
parameters respectively (order matters):curl "http://localhost:8080/dynamicModels?limit=5&sort[]=booleanField&order[]=descending&sort[]=stringField&order[]=ascending"
TODO
Contributing
We would love you to contribute to CursorPagination, check the CONTRIBUTING file for more info.
License
CursorPagination is available under the MIT license. See the LICENSE file for more info.