MIT licensed by Freckle Engineering
Maintained by [email protected]
This version can be pinned in stack with:
yesod-page-cursor-2.0.1.0@sha256:ef091525337c074acfcea42378372f2ea85aa941b9a975a699c7de671545149e,2125Module documentation for 2.0.1.0
- Network- Network.HTTP- Network.HTTP.Link
 
 
- Network.HTTP
- Yesod
Depends on 9 packages(full list with versions):
yesod-page-cursor
Cursor based pagination for yesod using index friendly keyset cursors.
Primer: No Offset
getSomeR :: Handler Value
getSomeR = do
  teacherId <- Param.required "teacherId"
  mCourseId <- Param.optional "courseId"
  page <- withPageAbsolute 100 entityKey $ \Cursor {..} -> do
    let (teacherId, mCourseId) = cursorParams
    fmap (sort cursorPosition) . runDB $ selectList
      (catMaybes
        [ Just $ SomeAssignmentTeacherId ==. teacherId
        , (SomeAssignmentCourseId ==.) <$> mCourseId
        , whereClause cursorPosition
        ]
      )
      [LimitTo $ fromMaybe 100 cursorLimit, orderBy cursorPosition]
  returnJson $ keyValueEntityToJSON <$> page
 where
  whereClause = \case
    First -> Nothing
    Previous p -> Just $ persistIdField <. p
    Next p -> Just $ persistIdField >. p
    Last -> Nothing
  orderBy = \case
    First -> Asc persistIdField
    Previous _ -> Desc persistIdField
    Next _ -> Asc persistIdField
    Last -> Desc persistIdField
  sort = \case
    First -> id
    Previous _ -> reverse
    Next _ -> id
    Last -> reverse
cursorLastPosition is configurable. A page sorted by created_at may look
like:
getSortedSomeR :: Handler Value
getSortedSomeR = do
  page <- withPageAbsolute 100 $ \Cursor {..} -> do
    fmap (sort cursorPosition) . runDB $ selectList
      (whereClause cursorPosition)
      [ LimitTo $ fromMaybe 100 cursorLimit
      , orderBy cursorPosition
      ]
  returnJson $ keyValueEntityToJSON <$> page
 where
  whereClause = \case
    First -> []
    Previous (pId, createdAt) ->
      [ SomeAssingmentCreatedAt <=. createdAt
      , persistIdField <. pId
      ]
    Next (pId, createdAt) ->
      [ SomeAssingmentCreatedAt >=. createdAt
      , persistIdField >. pId
      ]
    Last -> []
  orderBy = \case
    First -> Asc SomeAssignmentCreatedAt
    Previous _ -> Desc SomeAssignmentCreatedAt
    Next _ -> Asc SomeAssignmentCreatedAt
    Last -> Desc SomeAssignmentCreatedAt
  sort = \case
    First -> id
    Previous _ -> reverse
    Next _ -> id
    Last -> reverse
Usage
Paginated requests return a single page and a link with a cursor token to retrieve the next page.
$ curl 'some-rest.com/endpoint?limit=3'
{
  "first": : "https://some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ==",
  "previous": null,
  "next": "https://some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ==",
  "data": [...]
}
The link can be used to retrieve the next page.
$ curl 'some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ=='
{
  "first": : "https://some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ==",
  "previous": "https://some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ==",
  "next": "https://some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ==",
  "data": [...]
}
If no pages remain then no link is returned
$ curl 'some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ=='
{
  "first": : "https://some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ==",
  "previous": "https://some-rest.com/endpoint?next=eyJsYXN0UG9zaXRpb24iOjMsInBhcmFtcyI6WzEsbnVsbF0sImxpbWl0IjozfQ==",
  "next": null,
  "data": [...]
}
Changes
Changelog for yesod-cursor
Unreleased
v2.0.0.10
- Add withPage{,Link}Absolutefor generate absolute pagination links
v2.0.0.10
- Support aeson-2.0
- Compile with more GHCs on CI
v2.0.0.9
- Re-order declarations in TestApp, to build on GHC 9
v2.0.0.8
- Fix test suite compilation error
v2.0.0.7
- Remove dependencies upper bounds
v2.0.0.6
- Relax dependency bounds
v2.0.0.5
- Relax persistent upper bound
- Fix errors with http-link-header 1.2
v2.0.0.4
- Relax lens upper bound
v2.0.0.3
- Test with LTS 17.1 / GHC 8.10
- Relax upper bounds
v2.0.0.2
- Relax upper bound on persistent packages
v2.0.0.1
- Fix test suite for new persistent
v2.0.0.0
- Add defaultLimit :: Intargument towithPageandwithPageLink
v1.0.0.1
- Fix missing previouslink in all but last page
v1.0.0.0
Initial release.
