A convenient way to configure and interact with a async sqlalchemy session through context in asynchronous applications
Project description
context-async-sqlalchemy
Provides a super convenient way to work with sqlalchemy in asynchronous applications. It takes care of the issues of managing the lifecycle of engine, session, and transactions without being a wrapper.
The main task is to get quick and easy access to the session and not worry about when to open and when to close it.
The key features are:
- Super easy to use
- Automatically manages the lifecycle of engine, session, and transaction (autocommit/autorollback)
- It doesn't interfere with manually opening and closing sessions and transactions when needed.
- Does not depend on the web framework
- It is not a wrapper over sqlalchemy
- It is convenient to test
- Host switching in runtime
- It can manage multiple databases and multiple sessions to a single database
- Provides tools for concurrent sql queries
- Lazy initialization is everywhere
What does usage look like?
from context_async_sqlalchemy import db_session
from sqlalchemy import insert
from database import connection # your configured connection to the database
from models import ExampleTable # just some model for example
async def some_func() -> None:
# Created a session (no connection to the database yet)
session = await db_session(connection)
stmt = insert(ExampleTable).values(text="example_with_db_session")
# On the first request, a connection and transaction were opened
await session.execute(stmt)
# If you call db_session again, it will return the same session
# even in child coroutines.
session = await db_session(connection)
# The second request will use the same connection and the same transaction
await session.execute(stmt)
# The commit and closing of the session will occur automatically
How it works
Here is a very simplified diagram of how everything works:
- Before executing your code, the middleware will prepare a container in which the sessions required by your code will be stored. The container is saved in contextvars
- Your code accesses the library to create new sessions and retrieve existing ones
- After your code, middleware will automatically commit or roll back open transactions. Closes open sessions and clears the context.
The library also provides the ability to commit, rollback, and close at any time, without waiting for the end of the request, without any problems.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_async_sqlalchemy-2.0.0.tar.gz.
File metadata
- Download URL: context_async_sqlalchemy-2.0.0.tar.gz
- Upload date:
- Size: 3.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c14beae297b1b9175848d5c2a62419aa6c5b32dc890f43610b35fcce631c13d
|
|
| MD5 |
73410369a8f79deda94300d698b8c186
|
|
| BLAKE2b-256 |
d0fc4df45a235f8d3badaa687df1ff77be2b862eb806e432d34668c68b11da58
|
File details
Details for the file context_async_sqlalchemy-2.0.0-py3-none-any.whl.
File metadata
- Download URL: context_async_sqlalchemy-2.0.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55191ae92e961612093c6de3c499fa3e9e02fd8ca97aeb3998041ab29320d41f
|
|
| MD5 |
49d8fcbe9740698fd0491fe113ee7baf
|
|
| BLAKE2b-256 |
14128d704b6ed6f512a29ac4768ed8a3269f41f90af82bf247f717a4dec8078d
|