OrderBy

Bases: QueryClause

A class representing an order by clause for SQLAlchemy queries. This class extends the QueryClause class and adds an order attribute to specify the ordering direction (ascending or descending). The field attribute can be either a string representing the field name or an InstrumentedAttribute from SQLAlchemy.

An instance of this class represents a single order by condition that can be applied to a call to the order_by parameter of any method of a SqlRepository subclass. Keep in mind that this order_by parameter is expected to be a sequence of OrderBy instances, so multiple order by conditions can be applied to a query by including multiple instances of this class in the sequence.

Attributes:
  • field (str | InstrumentedAttribute[Any]) –

    Can be a string representing the field name or an InstrumentedAttribute from SQLAlchemy.

  • order (Order) –

    An instance of the Order enumeration specifying the ordering direction (ascending or descending).

Source code in src/alpha/infra/models/order_by.py
@dataclass
class OrderBy(QueryClause):
    """A class representing an order by clause for SQLAlchemy queries. This
    class extends the `QueryClause` class and adds an `order` attribute
    to specify the ordering direction (ascending or descending). The `field`
    attribute can be either a string representing the field name or an
    `InstrumentedAttribute` from SQLAlchemy.

    An instance of this class represents a single order by condition that can
    be applied to a call to the `order_by` parameter of any method of a
    `SqlRepository` subclass. Keep in mind that this `order_by` parameter is
    expected to be a sequence of `OrderBy` instances, so multiple order by
    conditions can be applied to a query by including multiple instances of
    this class in the sequence.

    Attributes
    ----------
    field
        Can be a string representing the field name or an
        `InstrumentedAttribute` from SQLAlchemy.
    order
        An instance of the `Order` enumeration specifying the ordering
        direction (ascending or descending).
    """

    field: str | InstrumentedAttribute[Any] = ""
    order: Order = Order.ASC

    def __post_init__(self) -> None:
        """Post-initialization method to set up the order by clause. This
        method calls the parent class's post-initialization method and then
        determines the appropriate subclass based on the order attribute.
        """
        super().__post_init__()
        self.__class__ = self._get_filter_class()  # type: ignore

    def _get_filter_class(self) -> type["OrderBy"]:
        """Determine the appropriate subclass based on the order attribute."""
        match self.order:
            case Order.ASC:
                return AscendingOrder
            case Order.DESC:
                return DescendingOrder
            case _:
                return OrderBy

Methods:

__post_init__

__post_init__()

Post-initialization method to set up the order by clause. This method calls the parent class's post-initialization method and then determines the appropriate subclass based on the order attribute.

Source code in src/alpha/infra/models/order_by.py
def __post_init__(self) -> None:
    """Post-initialization method to set up the order by clause. This
    method calls the parent class's post-initialization method and then
    determines the appropriate subclass based on the order attribute.
    """
    super().__post_init__()
    self.__class__ = self._get_filter_class()  # type: ignore