Skip to main content

A typed, generic repository base for SQLAlchemy 2.0. Full CRUD with no per-table boilerplate.

Project description

repositron

A typed, generic repository base for SQLAlchemy 2.0. Full CRUD, zero per-table boilerplate.

Test Release Package version Supported Python versions License

Documentation · Filtering · Projection · Hooks · API


Every SQLAlchemy project rewrites the same repository layer: a class per table wrapping select(...) / session.scalars(...), the same get / list / count, the same pagination math, the same "turn the ORM row into something light to return". It is mechanical, easy to get subtly wrong, and you write it again for the next table.

repositron writes that layer once, generically. Declare a model (and optionally a DTO and write payloads), inherit one class, and get a fully typed repository, every method checked against the types you declared.

from dataclasses import dataclass
from repositron import Repository, UNSET, UnsetType


@dataclass(frozen=True, slots=True)
class TaskDTO:                 # light, detached, serializes straight to JSON
    id: int
    title: str
    status: str
    assignee_id: int | None


@dataclass
class TaskCreate:
    workspace_id: int
    title: str


@dataclass
class TaskUpdate:
    title: str | UnsetType = UNSET            # absent = leave alone
    status: str | UnsetType = UNSET
    assignee_id: int | None | UnsetType = UNSET   # None = unassign (SET NULL)


class TaskRepository(Repository[Task, TaskDTO, TaskCreate, TaskUpdate]):
    pass                                       # the whole repository

That is the whole repository. get / first / list / list_paginated / count / exists / create / update / delete all come for free, typed against TaskDTO:

repo = TaskRepository(session)

repo.get(1)                                              # -> TaskDTO | None
repo.list(workspace_id=42, status="open", order_by=Task.created_at.desc())  # -> list[TaskDTO]
repo.list_paginated(0, 20, order_by=Task.created_at.desc())  # -> PaginatedResult[TaskDTO]
repo.create(TaskCreate(workspace_id=42, title="Ship the docs"))  # -> int (new id)
repo.update(1, TaskUpdate(status="done"))               # only status; title untouched

Why repositron

It cuts the layer you keep rewriting. One generic base replaces the per-table CRUD class, and every method is typed off the generic parameters, so your editor knows repo.list() is list[TaskDTO] and repo.get(id) is checked against the key type you declared (int, str, uuid.UUID).

Two ways to filter, in one call. Equality is keyed by attribute name, anything else is a plain SQLAlchemy expression, and they combine. A None value means IS NULL; UNSET skips the filter, so optional query params pass straight through without branching.

repo.list(workspace_id=42, extra_filters=[Task.archived_at.is_(None)], order_by=Task.id)
# WHERE workspace_id = 42 AND archived_at IS NULL ORDER BY id  (open, non-archived)

Updates that can actually write NULL. UNSET means "leave this column alone", None means "set it to NULL", the distinction the hand-written if x is not None pattern silently loses. TaskUpdate(assignee_id=None) unassigns a task; TaskUpdate(status="done") leaves the assignee untouched.

Projection that is real column selection. Index the repo with a narrow shape and it narrows the SELECT itself, it does not fetch the row and drop fields. The injected repository is untouched, the projection lasts only for the call.

@dataclass(frozen=True, slots=True)
class TaskCard:
    id: int
    title: str
    status: str

repo[TaskCard].list(workspace_id=42, status="open")
# SELECT tasks.id, tasks.title, tasks.status FROM tasks
#   WHERE workspace_id = 42 AND status = 'open'
#   -> list[TaskCard]   (only those three columns ever leave the database)

Extend without overriding. Hooks layer a derived column, an enriched DTO, or an audit row onto the base, and @writes gives a custom write the same flush/commit/rollback the built-ins get, no self.session plumbing.

Your choice of DTO. A dataclass that serializes straight to JSON (so the same object is your repository return value and your FastAPI response_model), the model itself, or a Pydantic schema you already have.

Install

uv add repositron        # or: pip install repositron

Requires Python 3.13+ and sqlalchemy>=2.0, the only dependency.

Documentation

Full guides and API reference at repositron.fa.dev.br.

License

MIT. See LICENSE.

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

repositron-0.5.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

repositron-0.5.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file repositron-0.5.0.tar.gz.

File metadata

  • Download URL: repositron-0.5.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for repositron-0.5.0.tar.gz
Algorithm Hash digest
SHA256 cfe4a5ac8a9f8bb9ca00c01904ab97c97a55820b4d3abe4f0a095b545716d1c6
MD5 4998cc1247478a85972b4189e7d37581
BLAKE2b-256 a9134ac9d490b36484b679cb5713ea4a92aa30952319909a7f990f30cd79d0cd

See more details on using hashes here.

File details

Details for the file repositron-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: repositron-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for repositron-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98dfabf12c4a5673a5d3af3d27b630ce73a11af04f15b7574001a1efdb45b7dd
MD5 05ae848ecae9b4edd424ee13b10e3383
BLAKE2b-256 f115ca8d8e4763bac7ba9e47b6fe1d32be02ed1148a2e29d1a9af70ad0c09342

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