37 lines
1003 B
Python
37 lines
1003 B
Python
"""Reusable mapped-column mixins for ORM models."""
|
|
|
|
import datetime as dt
|
|
import uuid
|
|
|
|
from sqlalchemy import DateTime, func
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
|
|
class UUIDPrimaryKeyMixin:
|
|
"""``id`` UUID primary key, generated application-side.
|
|
|
|
Generating in Python (not a DB default) keeps ids available before flush —
|
|
important because ``track.id`` is the client-facing ``content_id``.
|
|
"""
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
|
|
|
|
|
class TimestampMixin:
|
|
"""``created_at`` / ``updated_at``, server-managed.
|
|
|
|
Present on every user-mutable entity for future delta-sync (plan §4).
|
|
"""
|
|
|
|
created_at: Mapped[dt.datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
server_default=func.now(),
|
|
nullable=False,
|
|
)
|
|
updated_at: Mapped[dt.datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
server_default=func.now(),
|
|
onupdate=func.now(),
|
|
nullable=False,
|
|
)
|