Skip to main content

Type safety filtering and ordering for SQLAlchemy

Project description

SQLAlchemy Filter

Docs pypi version PyPI - Downloads

Package for convenient filtering and ordering functionality in SQLAlchemy

Quite often, optional filtering functionality is required. To facilitate the implementation of filtering functionality, this package was written

See documentation

All examples are available at the following link

Simple Usage

# models.py
class Review(Base):
    __tablename__ = "review"

    id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
    content: Mapped[str]

Define filter class

# filters.py

from operator import eq
from sqla_filter import (
    BaseFilter,
    Unset,
    UNSET,
    FilterField,
)

from db.models import Review

class ReviewFilter(BaseFilter):
    ident: Annotated[UUID | Unset, FilterField(Review.id, operator=eq)] = UNSET
    content: Annotated[
        str | Unset,
        FilterField(Review.content, operator=eq),
    ] = UNSET

Use a filter in the repository (or elsewhere)

# repository.py

class ReviewRepository:
    def __init__(self, session: AsyncSession) -> None:
        self._session = session

    async def get(self, filter_: ReviewFilter) -> Review | None:
        stmt = select(ReviewFilter)
        stmt = filter_.apply(stmt)
        return (await self._session.scalars()).one_or_none()

Use repository method

# command.py

class UpdateReviewCommand:
    def __init__(self, repository: ReviewRepository) -> None:
        self._repository = repository

    async def execute(self, ident: UUID, content: str) -> Review:
        review = await self._repository.get(
            filter_=ReviewFilter(ident=ident)
        )
        ...

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

sqla_filter-0.5.7.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

sqla_filter-0.5.7-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file sqla_filter-0.5.7.tar.gz.

File metadata

  • Download URL: sqla_filter-0.5.7.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for sqla_filter-0.5.7.tar.gz
Algorithm Hash digest
SHA256 0ee4a5cc66d04259e9657f29d0b2998cd8b6bfb5c8a9c68b7acfab4ed0fe48a3
MD5 8057864b07a7b6c82f8ec65fe415c326
BLAKE2b-256 d3b09c8950d90d7d1e7524c113d0a75ce0ede8d8dcf9686684800d9ecc1d3783

See more details on using hashes here.

File details

Details for the file sqla_filter-0.5.7-py3-none-any.whl.

File metadata

File hashes

Hashes for sqla_filter-0.5.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d88441e8b55d138f9acea60428db4d1d2a60bc6a85581d25b93a93233ce224e5
MD5 658fc56effec16eb892676e3ab36a278
BLAKE2b-256 39b0d5bb102563fb90c71fa3d8d5ed269172a19c6fba099c20abed6ab8b16e39

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