Get async session outside of route #161
-
I followed the documentation (https://github.com/jolt-org/advanced-alchemy/blob/main/examples/fastapi.py) for building an async app.
This allows me to get access to the database in my routes using with For testing purposes, I want to be able to get a session outside of my routes. I should get an async session, perform some testing creations and deletions and exit. How can I get a session without a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I'm also interested in that. Can anyone share an example of how to do it? Thanks. |
Beta Was this translation helpful? Give feedback.
-
Sorry for the delay in answering this. There is actually a method on the you can use it like so: alchemy: StarletteAdvancedAlchemy
alchemy = StarletteAdvancedAlchemy(config=sqlalchemy_config, app=app)
async with sqlalchemy_config.get_session() as db_session:
await db_session.execute(...) If you needed to use this in a test, you would just use this a fixture or monkey patch the session wherever you need it. |
Beta Was this translation helpful? Give feedback.
Sorry for the delay in answering this. There is actually a method on the
config
object to get a new session anywhere outside of a request.you can use it like so:
If you needed to use this in a test, you would just use this a fixture or monkey patch the session wherever you need it.