Skip to content

Event

event

Module defining the base Event class for events.

Event

Bases: Message[RawEventType]

Base class for all events.

Events represent something significant that happened in the system.
They are immutable facts about the past that other parts of the system can react to.

Events are named in past tense (e.g., OrderCreated, CustomerRegistered,
PaymentProcessed).

Example
class OrderCreatedPayload:
    def __init__(self, order_id: str, customer_id: str, total: float) -> None:
        self.order_id = order_id
        self.customer_id = customer_id
        self.total = total


class OrderCreated(Event[OrderCreatedPayload]):
    def __init__(self, order_id: str, customer_id: str, total: float):
        super().__init__()
        self._order_id = order_id
        self._customer_id = customer_id
        self._total = total

    @property
    def _payload(self) -> OrderCreatedPayload:
        return OrderCreatedPayload(
            order_id=self._order_id,
            customer_id=self._customer_id,
            total=self._total,
        )

    @classmethod
    def from_payload_fields(
        cls, data: OrderCreatedPayload, metadata: MessageMetadata
    ) -> "OrderCreated":
        return cls(
            order_id=data.order_id,
            customer_id=data.customer_id,
            total=data.total,
        )

    @property
    def value(self) -> OrderCreatedPayload:
        return self._payload
Source code in src/forging_blocks/domain/messages/event.py
class Event[RawEventType](Message[RawEventType]):
    """Base class for all events.

    Events represent something significant that happened in the system.
    They are immutable facts about the past that other parts of the system can react to.

    Events are named in past tense (e.g., OrderCreated, CustomerRegistered,
    PaymentProcessed).


    Example:
        ```python
        class OrderCreatedPayload:
            def __init__(self, order_id: str, customer_id: str, total: float) -> None:
                self.order_id = order_id
                self.customer_id = customer_id
                self.total = total


        class OrderCreated(Event[OrderCreatedPayload]):
            def __init__(self, order_id: str, customer_id: str, total: float):
                super().__init__()
                self._order_id = order_id
                self._customer_id = customer_id
                self._total = total

            @property
            def _payload(self) -> OrderCreatedPayload:
                return OrderCreatedPayload(
                    order_id=self._order_id,
                    customer_id=self._customer_id,
                    total=self._total,
                )

            @classmethod
            def from_payload_fields(
                cls, data: OrderCreatedPayload, metadata: MessageMetadata
            ) -> "OrderCreated":
                return cls(
                    order_id=data.order_id,
                    customer_id=data.customer_id,
                    total=data.total,
                )

            @property
            def value(self) -> OrderCreatedPayload:
                return self._payload
        ```

    """

    @property
    def occurred_at(self) -> datetime:
        """Get the timestamp when this event occurred (UTC timezone)."""
        return self.created_at

    @property
    @abstractmethod
    def _payload(self) -> RawEventType:
        """Return the event-specific payload data.

        Subclasses MUST implement this property to return the event-specific
        data.
        """

occurred_at: datetime property

Get the timestamp when this event occurred (UTC timezone).