Swiftpack.co - Package - vapor-community/pagination

Swift Vapor CircleCI

Vapor Pagination

Pagination is based off of the Fluent 2 pagination system.

Getting Started

Add this to your Package.swift file

.package(url: "https://github.com/vapor-community/pagination.git", from: "1.0.0")

Conform your model to Paginatable

extension MyModel: Paginatable { }

Once you have done that, it's as simple as returning your query in paginated format.

func test(_ req: Request) throws -> Future<Paginated<MyModel>> {
    return try MyModel.query(on: req).paginate(for: req)
}

Even return items off of the query builder

func test(_ req: Request) throws -> Future<Paginated<MyModel>> {
    return try MyModel.query(on: req).filter(\MyModel.name == "Test").paginate(for: req)
}

Making a request with the parameters is easy is appending ?page= and/or ?per=

curl "http://localhost:8080/api/v1/models?page=1&per=10"

A response looks like this

{
  "data": [{
    "updatedAt": "2018-03-07T00:00:00Z",
    "createdAt": "2018-03-07T00:00:00Z",
    "name": "My Test Model"
  }],
  "page": {
    "position": {
      "current": 1,
      "max": 1
    },
    "data": {
      "per": 10,
      "total": 2
    }
  }
}

Github

link
Stars: 67

Used By

Total: 0

Releases

2.0.0 Alpha 1 - 2019-02-25 18:53:12

⚠️ This is a major release and currently this version does not have breaking changes. Do note that v2.0.0 will be integrated into the Fluent 4 release and these releases are only here for people to take advantage of the new Advanced Queries for the time being.

Pagination Per crash fix - 2018-11-16 06:36:22

Fixes a crash when setting the amount to 0

Slight Code Cleanup - 2018-10-10 17:49:14

Remove the RC packages - 2018-09-03 17:23:06

Models no longer need to conform to `Content` - 2018-06-26 22:20:46

Models no longer need to conform to Content

Fluent 3 RC3 Support - 2018-06-19 06:16:57

Fluent 3 RC3 Support - 2018-06-19 02:33:09

All the new syntax and query builder fixes for Fluent 3 RC3 support.

- 2018-05-29 00:03:33

Whats News

  • Syntax Change: key was renamed to pageKey
  • Syntax Change: pagePerKey was renamed to perPageKey ⚠️ This could potentially break some implementations if you are manually working with the Query Builder and Pagination
  • Page and Paginated are now public allowing you to easily create your own custom paginated responses

Fixes

The max pages and total item count are now based on the total amount of objects queried before the pagination. Marks the variables in the paginated struct public

Addendum to 1.0.2

Pagination 1.0.2 - 2018-05-22 22:37:14

Whats News

  • Syntax Change: key was renamed to pageKey
  • Syntax Change: pagePerKey was renamed to perPageKey ⚠️ This could potentially break some implementations if you are manually working with the Query Builder and Pagination
  • Page and Paginated are now public allowing you to easily create your own custom paginated responses

Fixes

The max pages and total item count are now based on the total amount of objects queried before the pagination.

Official Vapor Release - 2018-05-05 01:28:08

Upgraded the package to support the office Vapor 3.0.0 release!

Vapor 3 Pagination - 2018-05-03 02:28:34

Vapor 3 Pagination

1.0.0 Release

Checkout the read me for an example response, and how to implement and use Pagination

Query Count Fix - 2018-05-02 16:50:02

Fixed an issue where the query count would throw an error instead of returning 0

Query Fixes - 2018-04-30 21:16:51

When running a query and paginating the results with filters now works as expected.

User.query(on: req).filter(\User.name == "Test").paginate(for: req)

Query Fixes - 2018-04-26 16:26:12

Fixes an issue where the query range was incorrect

Model conformance fixes - 2018-04-25 06:25:45

Fixed a recursive issues when trying to parse the results

Pagination for Vapor 3 - 2018-04-06 23:52:34