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
Built Distribution
File details
Details for the file sqlalchemy_tx_context-0.4.0.tar.gz
.
File metadata
- Download URL: sqlalchemy_tx_context-0.4.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c6e3a552c8dd758d64c1ed55a749b30c362f10b4168ab05a64ee8058b168a06 |
|
MD5 | 864993717424c7ef8c53e4f4a03cceda |
|
BLAKE2b-256 | 7918b6f9b811ec8bb19d724121414d8d261ac31fbe4a87a8ed22eee45daa2f17 |
File details
Details for the file sqlalchemy_tx_context-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: sqlalchemy_tx_context-0.4.0-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d8dc401bf4f9b2d6460f06aeb14222636944f6e99a521ce53b68a9b11f52d09 |
|
MD5 | 9c3e02e0107b77dae94643c3bfb87363 |
|
BLAKE2b-256 | 6f4f65a5e99bfc2fbf22ce03d2cca8d10bf109dce6bb5ad8a5987b3111d42628 |