Skip to main content

A convenient way to configure and work with an async SQLAlchemy session through context in asynchronous applications

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

context_async_sqlalchemy-2.1.8.tar.gz (3.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

context_async_sqlalchemy-2.1.8-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file context_async_sqlalchemy-2.1.8.tar.gz.

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.1.8.tar.gz
Algorithm Hash digest
SHA256 66d3e23594d95b76125a918f3ecaf334b622a85fc7d6b22131f3711878da1d55
MD5 c309e7dd2c3799284c8c7ffc1d6bf4c2
BLAKE2b-256 78109831ef42d635f38fb7515b1154920e1c62370b796e78930261da2bbf814b

See more details on using hashes here.

File details

Details for the file context_async_sqlalchemy-2.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 0dadae62a773a147bc3fea5c2bf1be075c1baa696e890ca3e600fdd9623e0b0b
MD5 a1f191d8bb99307b3a0bd234e9b82ba2
BLAKE2b-256 ae7a6c8d6fb7c00557684125486dcde945f6876c87b1f4bc8c2bddbdcde08df6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page