Javalin: Pagination

Created on 20 May 2020  Â·  7Comments  Â·  Source: tipsy/javalin

Is there a best practice for handling pagination?

Perhaps using an off the shelf middleware?

Minimum requirements:
I want to specify offset / limits as query parameters that I can use in my SQL.

Advanced:
Cursor-based pagination

INFO QUESTION

Most helpful comment

Spring's PagingAndSortingRepository will do all the work for you, but it will cost you a lot with your application's memory consumption ...

You can create DTO to receive the data and use your logic to search for the data according to the request parameters

Extension method to get params from request
fun Context.getPageable(): Pageable = use(Pageable::class.java)

Pageable DTO to deserialize from request

open class Pageable {
    constructor()
    constructor(ctx: Context) : this() {
        this.pageNumber = ctx.queryParam("pageNumber")?.toInt() ?: 0
        this.pageSize = ctx.queryParam("pageSize")?.toInt() ?: 0
        this.orderBy = ctx.queryParam("orderBy")
        this.filter = ctx.queryParam("filter")
        this.totalSize = 0
    }
    var totalSize: Int = 0    
    var pageNumber: Int = 0
        set(value) {
            if (value <= 0) {
                field = 0
                return
            }
            field = value
        }
    var pageSize: Int = 10
        set(value) {
            if (value == 0) {
                field = 10
                return
            }
            field = value
        }
    val offset: Int
        get() {
            if (pageNumber == 0) {
                return pageNumber * pageSize
            }
            return (pageNumber - 1) * pageSize
        }
    var orderBy: String? = null
    var result: List<Any> = emptyList()
    var filter: String? = null
}

All 7 comments

Is there a best practice for handling pagination?

No, there's nothing in Javalin related to this (or databases in general).

I'm familiar with Spring's PagingAndSortingRepository – do you know of something like that I can use here without pulling in all of Spring's dependencies?

I understand this has nothing to do with Javalin. But I think capturing this here will be useful to future users who are searching GH's issues.

Spring's PagingAndSortingRepository will do all the work for you, but it will cost you a lot with your application's memory consumption ...

You can create DTO to receive the data and use your logic to search for the data according to the request parameters

Extension method to get params from request
fun Context.getPageable(): Pageable = use(Pageable::class.java)

Pageable DTO to deserialize from request

open class Pageable {
    constructor()
    constructor(ctx: Context) : this() {
        this.pageNumber = ctx.queryParam("pageNumber")?.toInt() ?: 0
        this.pageSize = ctx.queryParam("pageSize")?.toInt() ?: 0
        this.orderBy = ctx.queryParam("orderBy")
        this.filter = ctx.queryParam("filter")
        this.totalSize = 0
    }
    var totalSize: Int = 0    
    var pageNumber: Int = 0
        set(value) {
            if (value <= 0) {
                field = 0
                return
            }
            field = value
        }
    var pageSize: Int = 10
        set(value) {
            if (value == 0) {
                field = 10
                return
            }
            field = value
        }
    val offset: Int
        get() {
            if (pageNumber == 0) {
                return pageNumber * pageSize
            }
            return (pageNumber - 1) * pageSize
        }
    var orderBy: String? = null
    var result: List<Any> = emptyList()
    var filter: String? = null
}

Thanks @felipewom.

Yeah, that's my concern with pulling in Spring. I'm using Javalin for a reason and it defeats the purpose.

Thank you for the code snippet. I love it.

One question: What's an extension method here?

fun Context.getPageable(): Pageable = use(Pageable::class.java)

As an FYI / note: https://dinject.io/docs/javalin/beanparam

DInject Javalin generator has @BeanParam (just like the JAX-RS @BeanParam)
... so that is one way to easily reuse common parameters like (firstRow,
maxRows, sort, filter etc). You might be able to do something similar.

https://dinject.io/docs/javalin/beanparam

On Thu, 21 May 2020 at 06:00, Ganesh Swami notifications@github.com wrote:

Thanks @felipewom https://github.com/felipewom.

Yeah, that's my concern with pulling in Spring. I'm using Javalin for a
reason and it defeats the purpose.

Thank you for the code snippet. I love it.

One question: What's an extension method here?

fun Context.getPageable(): Pageable = use(Pageable::class.java)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/tipsy/javalin/issues/972#issuecomment-631633120, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AABTATJRCBQTBGMVCEZ7HB3RSQLDJANCNFSM4NFSPOYA
.

@gane5h @felipewom's example is in Kotlin. Extension methods a way to "add methods" on classes you don't own. In this case the extension method just adds a convenience method on Javalin's Context class to get pagination information.

If you're not familiar with Kotlin it would be the same as:

class PaginationUtils {
    public static Pageable getPageable(Context ctx) {
        return use(Pageable.class);
    } 
}

Thanks guys. This gives me enough info to proceed. Closing the issue now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gane5h picture gane5h  Â·  3Comments

bvicenzo picture bvicenzo  Â·  3Comments

ShikaSD picture ShikaSD  Â·  5Comments

mikexliu picture mikexliu  Â·  3Comments

TobiasWalle picture TobiasWalle  Â·  3Comments