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
SARepositoryandSAAsyncRepository(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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sarepo-0.1.2.tar.gz.
File metadata
- Download URL: sarepo-0.1.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84fec8aaa67b7bc8e7430da5a2d24f0c08e5c4ef85e672f349897956e2006d87
|
|
| MD5 |
c3e0532bd8799c43ea4415a673cfe106
|
|
| BLAKE2b-256 |
a367c76773723a3831d579464f4ea5c9025e66d6f727be0113e706e95fa6f3a9
|
File details
Details for the file sarepo-0.1.2-py3-none-any.whl.
File metadata
- Download URL: sarepo-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02068385499e56f8f5041767f82f2a8eda2197005e7862c9f726e480e1db3584
|
|
| MD5 |
4da63bbaa79533d68c5320c5086e0b31
|
|
| BLAKE2b-256 |
4df796ece2ab7b06ab123e39a755c77a5b3f98a9193ef2cb7dfd5c7560f9e806
|