pythonsqlalchemypaginationdatabase-cursor

How to use a cursor pagination in sql alchemy


I have a postgres database and trying to build an API with SqlAlchemy and I'm wondering how to implement a cursor pagination.

I looked at other examples here and also the flask-sqlAlchemy package, but both seem to use a combination of offset and limit. I'm looking for something more performant, similar to what django rest-framework is using. I can't seem to find anything like that in the documentation. I did find a third party package that seems to handle that here.

I was wondering if there is no built in way to handle cursor pagination?


Solution

  • Here is a basic way to make it:

    skip_id = 0
    num_rows = 10
    
    query = (
        select(MyModel)
        .filter(skip_id > MyModel.id)
        .order_by(MyModel.id.asc())
        .fetch(num_rows))
    
    session.scalars(query)
    

    On frontend side, send last id it gets, back to backend to get next 10 (or N as you want) rows. Note, that you can't get total pages number with that pagination, so you have to load them eventually like said here. Also, if you have complex ordering, you can't use cursor based pagination due to using ids. You can solve it with using pre-computed weights in a special column istead of ids, for example.