Skip to content

Get async session outside of route #161

Answered by cofin
breno-aumo asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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.

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@cofin
Comment options

Answer selected by cofin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants