Unit Of Work Abstractions
Classes
Instruction
SqlAlchemy
This package provides a small abstraction layer over the UOW (Unit of Work) pattern for working with sqlalchemy.
uowabc.sa.SaSessionUnitOfWork– A class that contains anasync_sessionmaker[AsyncSession]and automatically calls.commitor.rollbackwhen the context is closed.uowabc.sa.SaSessionRepository– A base repository containing anAsyncSession, extending the functionality ofSaSessionUnitOfWork.uowabc.sa.CrudSaSessionRepository– A repository based onSaSessionRepository, designed to work withDeclarativeBaseand implements basicCRUDmethods.
import uowabc.sa
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
class Row(DeclarativeBase):
__tablename__ = 'rows'
id = Column(Integer, primary_key=True, autoincrement=True)
# First step - Declaring the UOW
engine = create_async_engine('...')
session_maker = async_sessionmaker(engine, expire_on_commit=False)
class DatabaseSaUow(uowabc.sa.SaSessionUnitOfWork):
def __init__(self) -> None:
super().__init__(session_maker)
# Second step - Defining the repository
class RowsRepository(uowabc.sa.CrudSaSessionRepository[DatabaseSaUow, Row]):
UowType = DatabaseSaUow
model = Row
async def get_row_count(self) -> int:
result = await self.session.execute(select(func.count()).select_from(self.model))
count = result.scalar_one()
return count
# Step three - Execution
async def example_1():
# Sequentially create the UOW and the repository
async with DatabaseSaUow() as uow:
async with RowsRepository(uow) as rows_repository:
await rows_repository.get_row_count()
async def example_2(uow: DatabaseSaUow):
# Initialize the repository by passing an already created UOW
# The UOW must be "started" (the context manager must be invoked)
async with RowsRepository(uow) as rows_repository:
await rows_repository.get_row_count()
async def example_3():
# Initialize the repository without a pre-created UOW
# The UOW will be automatically created
async with RowsRepository() as rows_repository:
await rows_repository.get_row_count()
Dependencies
This module uses types introduced in python 3.12
Contribute
Issue Tracker: https://gitlab.com/rocshers/python/uow-abstractions/-/issues
Source Code: https://gitlab.com/rocshers/python/uow-abstractions
Before adding changes:
make install-dev
After changes:
make format test
Metadata
Release files for uow-abstractions 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| uow_abstractions-0.1.0.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| uow_abstractions-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.6 kB
Release files / uow_abstractions-0.1.0.tar.gz
| Download URL | uow_abstractions-0.1.0.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba5ddd94e12d944ce64a73dd8fb26a70b93061482d962102c962331054c9fc81
|
|
BLAKE2b-256 checksum How to use checksums |
ca4272f613103258b04a8a41f0b77d07a9bf41630666b9dfce3df88b98c3c301
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/6.8.0-40-generic
|
Release files / uow_abstractions-0.1.0-py3-none-any.whl
| Download URL | uow_abstractions-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9729b338fda6a497ad9f586c9ee7aba3bee0efcd1174c713555023f835a386b6
|
|
BLAKE2b-256 checksum How to use checksums |
acd02c4e3a94fdde196a65166ffd2b4b949dd3a36f0a53a63fbaa2490b9dabf4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/6.8.0-40-generic
|