vapor-community / pagination

Simple Vapor 3 Pagination
MIT License
64 stars 15 forks source link

The pagination fails to work with groupBy and sort #20

Closed belkhadir closed 5 years ago

belkhadir commented 5 years ago

Describe the bug

i'm try to using sort inside my queries and use with it Pagination : func getNews(_ req: Request) throws -> Future<Paginated<Article>> { return try Article.query(on: req).sort(\Article.publishedAt).paginate(for: req) } But it's fails to return any data and getting this error: { "error": true, "reason": "column \"Article.publishedAt\" must appear in the GROUP BY clause or be used in an aggregate function" }

anthonycastelli commented 5 years ago

@belkhadir When using the pagination method, the best way to handle custom sorts is to pass them into the pagination function. You'll want to do something more of

func getNews(_ req: Request) throws -> Future<Paginated<Article>> { 
    return try Article.query(on: req).paginate(for: req, [(\Article.publishedAt).querySort()])
}

The way things are setup is you have to specify the sorts manually. I have to still look into a way of using the query builders sort method to return things

belkhadir commented 5 years ago

Do you have any documentation ?

Prince2k3 commented 5 years ago

@anthonycastelli this still doesn't work. Instead it returns a Future<Page

> and not a Future<Paginated
>.

shearos commented 4 years ago

@Prince2k3 I'm a bit late but maybe for those in the future.

I was wondering the same but you can see that in QueryBuilder+Paginatable.swift that the Page is mapped.

extension QueryBuilder where Result: Paginatable & Content, Result.Database == Database {
    /// Returns a paginated response using page number from the request data
    public func paginate(for req: Request) throws -> Future<Paginated<Result>> {
        return try self.paginate(for: req).map(to: Paginated<Result>.self) { $0.response() }
    }
}

The response() method converts it to Paginated.

hope it helps