Skip to content

Pagination and Range

Pre-release
Pre-release
Compare
Choose a tag to compare
@tanner0101 tanner0101 released this 16 Jan 22:50
7dc374e

Adds paginate method to QueryBuilder which returns a Page of models. Page includes the array of models and a PageMetadata struct including information on current page, items per page, and total number of items.

Fluent adds a paginate(for:Request) method that automatically decodes the PageRequest from the Request's query string.

struct TodoController {
    func index(req: Request) throws -> EventLoopFuture<Page<Todo>> {
        Todo.query(on: req.db).paginate(for: req)
    }
}

Page can also map its items to different types.

let todos = Todo.query(on: req.db).paginate(for: req).map { page in
    page.map(Todo.Public.init)
}
print(todos) // ELFuture<Page<Todo.Public>>

In addition to pagination, QueryBuilder now supports range methods accepting Swift's Range literals.

query.range(2..<5) // returns at most 3 results, offset by 2
query.range(..<5) // returns at most 5 results