Spaces:
Running
Running
| """ | |
| Database Model Base Classes | |
| Provides the declarative base and reusable mixins for all ORM models. | |
| """ | |
| import uuid | |
| from datetime import datetime | |
| from typing import Optional | |
| from sqlalchemy import DateTime, Boolean, func | |
| from sqlalchemy.dialects.postgresql import UUID | |
| from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column | |
| class Base(DeclarativeBase): | |
| """Declarative base for all ORM models.""" | |
| pass | |
| class TimestampMixin: | |
| """Adds created_at and updated_at columns to any model.""" | |
| created_at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), | |
| server_default=func.now(), | |
| nullable=False, | |
| ) | |
| updated_at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), | |
| server_default=func.now(), | |
| onupdate=func.now(), | |
| nullable=False, | |
| ) | |
| class SoftDeleteMixin: | |
| """Adds soft delete capability to any model.""" | |
| is_deleted: Mapped[bool] = mapped_column( | |
| Boolean, default=False, index=True, nullable=False | |
| ) | |
| deleted_at: Mapped[Optional[datetime]] = mapped_column( | |
| DateTime(timezone=True), nullable=True | |
| ) | |
| class UUIDPrimaryKeyMixin: | |
| """UUID primary key mixin — prevents enumeration attacks.""" | |
| id: Mapped[uuid.UUID] = mapped_column( | |
| UUID(as_uuid=True), | |
| primary_key=True, | |
| default=uuid.uuid4, | |
| nullable=False, | |
| ) | |