Skip to main content

Unit Of Work Abstractions

PyPI PyPI - Python Version GitLab last commit

Homepage Downloads GitLab stars

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 an async_sessionmaker[AsyncSession] and automatically calls .commit or .rollback when the context is closed.
  • uowabc.sa.SaSessionRepository – A base repository containing an AsyncSession, extending the functionality of SaSessionUnitOfWork.
  • uowabc.sa.CrudSaSessionRepository – A repository based on SaSessionRepository, designed to work with DeclarativeBase and implements basic CRUD methods.
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)

Source distribution for uow-abstractions 0.1.0
File Size Uploaded
uow_abstractions-0.1.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uow-abstractions 0.1.0
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page