fastapi-pagination icon indicating copy to clipboard operation
fastapi-pagination copied to clipboard

Support sorting by column name and it's direction

Open Akkarine opened this issue 2 years ago • 3 comments

Great project! And I think this would be nice additional feature.

Akkarine avatar Jul 26 '22 19:07 Akkarine

You should already be able to do this by specifying sorting attributes on the query being passed. From teh SQLALchemy example, for example:

@app.get("/users/default", response_model=Page[UserOut])
@app.get("/users/limit-offset", response_model=LimitOffsetPage[UserOut])
def get_users(db: Session = Depends(get_db)) -> Any:
    return paginate(db.query(User).order_by(User.name.desc()))

aledt avatar Jul 29 '22 15:07 aledt

Yes, you could do that, but wouldn't be great to have such close-theme feature out of the box? Also, probably, as said in #219 , there is room left for optimizations, that could be missed, with passing prepared queries.

Akkarine avatar Aug 02 '22 07:08 Akkarine

Hi @Akkarine,

As @aledt said, you can specify order_by when a passing query to paginate function:

@app.get("/users", response_model=Page[UserOut])
def get_users(db: Session = Depends(get_db)) -> Any:
    return paginate(db.query(User).order_by(User.name.desc()))

New version 0.10.0 has minor optimization that will reset order_by when executing count query.

uriyyo avatar Sep 04 '22 14:09 uriyyo

@Akkarine I am closing this issue.

Please, take a look at https://github.com/uriyyo/fastapi-pagination/issues/219#issuecomment-1250150968 to see the root cause of performance issues.

uriyyo avatar Sep 18 '22 10:09 uriyyo