Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Session #17

Open
elektracodes opened this issue Mar 12, 2024 · 1 comment
Open

Async Session #17

elektracodes opened this issue Mar 12, 2024 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@elektracodes
Copy link

Hello!

Can this work with AsyncSession?
I am trying to work with the examples but I cannot seem to be able to overwrite the sorter.

I get this error

fastapi_listing/sorter/page_sorter.py
File "/opt/pysetup/.venv/lib/python3.11/site-packages/fastapi_listing/sorter/page_sorter.py", line 20, in sort_dsc_util
  query = query.order_by(inst_field.desc())
AttributeError: 'coroutine' object has no attribute 'order_by'

I have overwrite the get_default_read to work with async, but I cannot see how to do that in the page_sorter.

class VideoDao(GenericDao):
    def __init__(self, db: AsyncSession) -> None:
        super().__init__(db)

    name = "videoresponse"
    model = Videos

    async def get_default_read(self, fields_to_read: Optional[list]):
        query = await self.db.query(Videos)
        return query
@danielhasan1
Copy link
Owner

Allow me to ask you a couple of questions first.
I don't think Asyncsession works with query api(correct me if I'm wrong)
you should have faced an error like AttributeError: 'async_sessionmaker' object has no attribute 'query'
but your code was able to call page_sorter
it doesn't support AsyncSession natively for now but there is a way around
you could use select statement at dao level and in page_builder you could pass async_session as extra_context
where you can do session.execute(stmt) and call all or fetchall method on your result.
Do you want me to add a working example for this workaround?
Or I think we can work on adding a support layer for AsyncSession.

@danielhasan1 danielhasan1 self-assigned this Mar 13, 2024
@danielhasan1 danielhasan1 added the question Further information is requested label Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants