VaporSimplePagination is a little wrapper to use pagination known by other frameworks like Laravel
Add the following dependency to your Package.swift
.Package(url: "https://github.com/mludi/VaporSimplePagination", majorVersion: 0)
Run vapor build --clean
to install it
Import the Library by
import VaporSimplePagination
drop.get("paginated") { request in
guard let page = request.data["page"]?.int else {
return try JSON(Post.all().makeNode())
}
guard let pagininated = Post.paginate(limit: 15, page: page, description: "posts", makeJSON: true) else {
throw Abort.badRequest
}
return try JSON(node: pagininated)
}
You can pass true as makeJSON - param if your Model has overriden the func makeJSON() throws -> JSON {}
function.
Sample response looks like the following
{
"current_page": 1,
"per_page": 15,
"posts": [
{
"content": "test",
"created_at": "2016-11-23 18:45:16",
"id": 1,
"image_path": "",
"updated_at": "2016-11-23 18:45:16",
"user": {
"created_at": "2016-11-23 18:45:16",
"id": 1,
"updated_at": "2016-11-23 18:45:16",
"username": "matz"
}
},
{
"content": "test2",
"created_at": "2016-11-23 18:45:16",
"id": 1,
"image_path": "",
"updated_at": "2016-11-23 18:45:16",
"user": {
"created_at": "2016-11-23 18:45:16",
"id": 1,
"updated_at": "2016-11-23 18:45:16",
"username": "matz"
}
}
],
"total": 28,
"total_pages": 2
}