Skip to main content

Unit Of Work Abstractions

Project description

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

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

uow_abstractions-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

uow_abstractions-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file uow_abstractions-0.1.0.tar.gz.

File metadata

  • Download URL: uow_abstractions-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.8.0-40-generic

File hashes

Hashes for uow_abstractions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ba5ddd94e12d944ce64a73dd8fb26a70b93061482d962102c962331054c9fc81
MD5 ab6e812e8c90d5a3f1b0f7e161f26c9e
BLAKE2b-256 ca4272f613103258b04a8a41f0b77d07a9bf41630666b9dfce3df88b98c3c301

See more details on using hashes here.

File details

Details for the file uow_abstractions-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: uow_abstractions-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.8.0-40-generic

File hashes

Hashes for uow_abstractions-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9729b338fda6a497ad9f586c9ee7aba3bee0efcd1174c713555023f835a386b6
MD5 587a8c621ceaae17c00a3e5c0c1fcf32
BLAKE2b-256 acd02c4e3a94fdde196a65166ffd2b4b949dd3a36f0a53a63fbaa2490b9dabf4

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