Skip to main content

Minimal, explicit Repository & Unit-of-Work layer over SQLAlchemy 2.x

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

SARepo

Minimal, explicit Repository + Unit of Work layer on top of SQLAlchemy 2.x (sync & async). No magic method-names, just clean typed APIs, composable specs, pagination, and optional soft-delete.

Install (editable)

pip install -e .

Quick Start (sync)

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Mapped, mapped_column
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy import String

from SARepo.sa_repo import SARepository
from SARepo.base import PageRequest
from SARepo.specs import and_specs, ilike

class Base(DeclarativeBase): pass

class Request(Base):
    __tablename__ = "requests"
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    title: Mapped[str] = mapped_column(String(255))
    status: Mapped[str] = mapped_column(String(50))

engine = create_engine("sqlite+pysqlite:///:memory:", echo=False)
Base.metadata.create_all(engine)
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)

with SessionLocal() as session:
    repo = SARepository(Request, session)
    # create
    r = repo.add(Request(title="Hello", status="NEW"))
    session.commit()

    # search + paginate
    page = repo.page(PageRequest(0, 10), spec=ilike(Request.title, "%He%"))
    print(page.total, [i.title for i in page.items])

Async Quick Start

import asyncio
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from SARepo.sa_repo import SAAsyncRepository
from SARepo.base import PageRequest

async def main():
    engine = create_async_engine("sqlite+aiosqlite:///:memory:", echo=False)
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    SessionLocal = async_sessionmaker(bind=engine, expire_on_commit=False)
    async with SessionLocal() as session:
        repo = SAAsyncRepository(Request, session)
        await repo.add(Request(title="Hello", status="NEW"))
        await session.commit()
        page = await repo.page(PageRequest(0, 10))
        print(page.total)

asyncio.run(main())

Features

  • SARepository and SAAsyncRepository (CRUD, page() with total count)
  • Composable specs (eq, ilike, and_specs, not_deleted)
  • Optional soft-delete if the entity has is_deleted: bool
  • Minimal Unit of Work helpers (sync & async)

License

MIT

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

sarepo-0.1.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sarepo-0.1.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file sarepo-0.1.3.tar.gz.

File metadata

  • Download URL: sarepo-0.1.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sarepo-0.1.3.tar.gz
Algorithm Hash digest
SHA256 64720a899d9af3444cbb5acf710df6e31ffd1aecaf89e5691476a05776d5a016
MD5 5781bd8b171f49d532302f257f8ec8a6
BLAKE2b-256 71fe0cab0a2495382314d4366f04585f93f198d5b94014c62a0b584cff9aa5df

See more details on using hashes here.

File details

Details for the file sarepo-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: sarepo-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sarepo-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a1e655dcfd54bd3be85ea27cdb591bed1e15e8af6fbcc11d7f5ad102c7d16a89
MD5 90e0984c56ed13ef8ac45b35961fbc06
BLAKE2b-256 ace073583d1c07513ea5c54546331a15c208854494d15888a7340bac416b4c42

See more details on using hashes here.

Supported by

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