| from __future__ import annotations |
|
|
| import uuid |
| from datetime import datetime |
|
|
| from sqlalchemy import DateTime, ForeignKey, String, func |
| from sqlalchemy.dialects.postgresql import UUID |
| from sqlalchemy.orm import Mapped, mapped_column, relationship |
|
|
| from app.database.database import Base |
|
|
|
|
| class Document(Base): |
| """ |
| Represents a logical document within a workspace. |
| |
| A document is the logical identity of a document. Each upload |
| creates a new DocumentVersion while preserving the document's |
| identity across revisions. |
| """ |
|
|
| __tablename__ = "documents" |
|
|
| id: Mapped[uuid.UUID] = mapped_column( |
| UUID(as_uuid=True), |
| primary_key=True, |
| default=uuid.uuid4, |
| ) |
|
|
| workspace_id: Mapped[uuid.UUID] = mapped_column( |
| UUID(as_uuid=True), |
| ForeignKey("workspaces.id", ondelete="CASCADE"), |
| nullable=False, |
| index=True, |
| ) |
|
|
| title: Mapped[str] = mapped_column( |
| String(500), |
| nullable=False, |
| index=True, |
| ) |
|
|
| document_type: Mapped[str] = mapped_column( |
| String(100), |
| nullable=False, |
| index=True, |
| ) |
|
|
| created_at: Mapped[datetime] = mapped_column( |
| DateTime(timezone=True), |
| nullable=False, |
| server_default=func.now(), |
| ) |
|
|
| updated_at: Mapped[datetime] = mapped_column( |
| DateTime(timezone=True), |
| nullable=False, |
| server_default=func.now(), |
| onupdate=func.now(), |
| ) |
|
|
| |
| |
| |
|
|
| workspace: Mapped["Workspace"] = relationship( |
| back_populates="documents", |
| ) |
|
|
| versions: Mapped[list["DocumentVersion"]] = relationship( |
| back_populates="document", |
| cascade="all, delete-orphan", |
| passive_deletes=True, |
| order_by="DocumentVersion.version_number", |
| ) |
|
|
| def __repr__(self) -> str: |
| return ( |
| f"<Document(" |
| f"title='{self.title}', " |
| f"type='{self.document_type}')>" |
| ) |