uriyyo / fastapi-pagination

FastAPI pagination 📖
https://uriyyo-fastapi-pagination.netlify.app/
MIT License
1.1k stars 126 forks source link

Make it possible to use async version of `length_function` #1200

Open mtsarev06 opened 1 week ago

mtsarev06 commented 1 week ago

So due to the fact that the issue with the Elasticsearch support has been hanging since 2022, I've been trying to implement async ElasticSearch integration myself using async_paginator.paginate function. It's kind of hacky, but can be done by creating a wrapper function:

new_search = Document.search()

async def _pagination_transformer(sequence: AsyncSearch):
    return list(await sequence.execute())

return await paginate(
    sequence=new_search,
    transformer=_pagination_transformer
)

The thing is that in the current implementation the length_function is sync, and there is no way to make a call of a new_search.count() coroutine inside it, because it's currently in the running loop. So the only solution I came up with is calling count() before paginate, and passing the result in a lambda function, but it's too much hacky, not to mention that it will be called even if raw_params.include_total = False. So having an opportunity to use async length_function would be very nice.

uriyyo commented 1 week ago

@mtsarev06 Makes sense to me, I will take a look