mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-04-15 22:26:25 +02:00
* feat: rework async event system * docs: add v3 migration guide * feat: add cache * enhancements
51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
"""SQLAlchemy column mixins for common column patterns."""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, Uuid, text
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
|
|
class UUIDMixin:
|
|
"""Mixin that adds a UUID primary key auto-generated by the database."""
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid,
|
|
primary_key=True,
|
|
server_default=text("gen_random_uuid()"),
|
|
)
|
|
|
|
|
|
class UUIDv7Mixin:
|
|
"""Mixin that adds a UUIDv7 primary key auto-generated by the database."""
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid,
|
|
primary_key=True,
|
|
server_default=text("uuidv7()"),
|
|
)
|
|
|
|
|
|
class CreatedAtMixin:
|
|
"""Mixin that adds a ``created_at`` timestamp column."""
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
server_default=text("clock_timestamp()"),
|
|
)
|
|
|
|
|
|
class UpdatedAtMixin:
|
|
"""Mixin that adds an ``updated_at`` timestamp column."""
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
server_default=text("clock_timestamp()"),
|
|
onupdate=text("clock_timestamp()"),
|
|
)
|
|
|
|
|
|
class TimestampMixin(CreatedAtMixin, UpdatedAtMixin):
|
|
"""Mixin that combines ``created_at`` and ``updated_at`` timestamp columns."""
|