Skip to main content

context-async-sqlalchemy

PyPI PyPI Downloads Tests (coverage > 90%)

DOCUMENTATION

Provides a super convenient way to work with SQLAlchemy in asynchronous applications. It handles the lifecycle management of the engine, sessions, and transactions.

The main goal is to provide quick and easy access to a session, without worrying about opening or closing it when it's not necessary.

Key features:

  • Automatically manages the lifecycle of the engine, sessions, and transactions
  • Allows for user autonomy when manually opening or closing sessions and transactions
  • Framework-agnostic
  • Not a wrapper around SQLAlchemy
  • Convenient for testing
  • Runtime host switching
  • Supports multiple databases and sessions per database
  • Provides tools for running concurrent SQL queries
  • Fully lazy initialization

Example of a typical session

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  # a model for example

async def some_func() -> None:
    # Creates a session with no connection to the database yet
    session = await db_session(connection)
    
    stmt = insert(ExampleTable).values(text="example_with_db_session")

    # A connection and transaction open in the first request.
    await session.execute(stmt)
    
    # If you call db_session again, it returns the same session
    # even in child coroutines.
    session = await db_session(connection)
    
    # The second request uses the same connection and the same transaction
    await session.execute(stmt)

    # The commit and closing of the session occurs automatically

How it works

basic schema.png

  1. Before executing your code, the middleware prepares a container in which the sessions required by your code are stored. The container is saved in contextvars.
  2. Your code accesses the library to create new sessions and retrieve existing ones.
  3. The middleware automatically commits or rolls back open transactions. It also closes open sessions and clears the context.

The library provides the ability to commit, roll back, and close at any

Metadata

Release files for context-async-sqlalchemy 2.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for context-async-sqlalchemy 2.1.9
File Size Uploaded
context_async_sqlalchemy-2.1.9.tar.gz 3.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for context-async-sqlalchemy 2.1.9
File Interpreter ABI Platform
context_async_sqlalchemy-2.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 3.6 MB

Release files / context_async_sqlalchemy-2.1.9.tar.gz

Download URL context_async_sqlalchemy-2.1.9.tar.gz
Size 3.5 MB
Tags Source
SHA-256 checksum
How to use checksums
67efc9070f70c375067a4df1db3afcab3c96f659dbef3d357f96109bb3719889
BLAKE2b-256 checksum
How to use checksums
0dcb6407034a21ecc19a5d94687a937d9a8e513fe5b223d2493b2f2e62eaea0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.17

Release files / context_async_sqlalchemy-2.1.9-py3-none-any.whl

Download URL context_async_sqlalchemy-2.1.9-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27a3338be54e04f1cf15163bfc6028154bd65e914e5892ced8c4cdb89d6ac4b5
BLAKE2b-256 checksum
How to use checksums
46e68ecafc44d9ea2aab0ff397aedd1ad9f947c67ad153b85679cd99e74c0782
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.17

Release history Release notifications | RSS feed

This release

2.1.9 This release

2 release files

2.1.8

2 release files

2.1.7

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.4

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page