Skip to main content

An extension for sqlalchemy

Project description

SQLAlchemy Transaction Context

An extension for sqlalchemy to store the session object in context and simplify the retrieval of the query result.

Create instance

from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy_tx_context import SQLAlchemyTransactionContext


engine = create_async_engine(...)
db = SQLAlchemyTransactionContext(engine)

Execute query

SQLAlchemyTransactionContext contains the following methods for creating queries with execution through a context session:

  • select
  • insert
  • update
  • delete
  • union
  • union_all
  • exists

Query will use a session from the context.

async def some_repository_method():
    await db.insert(...).values(...).execute()


async def some_function():
    async with db.transaction() as tx:
        await some_repository_method()
        await tx.rollback() # Record will not be inserted

If there is no session in the context, a default session will be created to execute a single query.

async def some_repository_method():
    await db.insert(...).values(...).execute()

async def some_function():
      await some_repository_method()

Calling the transaction method inside the session will create a nested transaction.

async def some_function():
    async with db.transaction() as tx1:
        isinstance(tx1, AsyncSession) # True
        async with db.transaction() as tx2:
            isinstance(tx2, AsyncSessionTransaction) # True

Execute query with proxy methods

List of proxy methods added to the request object

Proxy methods for the AsyncSession properties:

  • execute
  • scalar
  • scalars

Example:

value = await db.select(...).execute()

The same as:

async with async_sessionmaker(engine).begin() as tx:
    result = await tx.execute(select(...))

Proxy methods for the Result properties:

  • first
  • all

Example:

value = await db.select(...).first()

The same as:

async with async_sessionmaker(engine).begin() as tx:
    result = await tx.execute(select(...))
    value = result.first()

Proxy methods for the MappingResult properties:

  • mapped_first
  • mapped_one
  • mapped_all

Example:

value = await db.select(...).mapped_first()

The same as:

async with async_sessionmaker(engine).begin() as tx:
    result = await tx.execute(select(...))
    value = result.mappings().first()

Proxy method for the CursorResult:

  • rowcount

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

sqlalchemy_tx_context-0.2.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_tx_context-0.2.2-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_tx_context-0.2.2.tar.gz.

File metadata

  • Download URL: sqlalchemy_tx_context-0.2.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.16

File hashes

Hashes for sqlalchemy_tx_context-0.2.2.tar.gz
Algorithm Hash digest
SHA256 0b2fd2591ad2f25c86d37f8dff08e0b30870a45c5580058595b5df9d2325d35c
MD5 ba441f7c0a3023f2167b0c1992e2ca24
BLAKE2b-256 582b4206e07b3a6afe25bbc602387a31bd84ed4487688bedf88e7ccf6b22a4b5

See more details on using hashes here.

File details

Details for the file sqlalchemy_tx_context-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_tx_context-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6b5c7e625c8d2bf8c5b33133afb4beca7fa67c3328226c2c69aea1edeada05fb
MD5 3b651846c9ff2f447ddd9645b292be7a
BLAKE2b-256 e98f852548df49638ecbb665e26b366863cca93a9328e29ba12b6cc530ec917a

See more details on using hashes here.

Supported by

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