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.7.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.7-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.1.7.tar.gz
Algorithm Hash digest
SHA256 cf6a2003f819373c7a97f717bc263cd3783ede455a519513eada0d89f5374fb8
MD5 50e033dee9d1020edd02c082b36e69b5
BLAKE2b-256 854ec464febd6d0bfda242f072dfd4ed86ce686ace6f3ed87894e8af67b7ec02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for context_async_sqlalchemy-2.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d5bc73d2a2bc168f4047678caa8da1a0d8299a07672fb97e46ede302e64f3e90
MD5 81ec606d2cc619e277afdd05ed7406cf
BLAKE2b-256 c35e4587ee11c48f4b264a197d957636bbd9a06faee3d781bd0f7f226e99f253

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