File size: 1,835 Bytes
67f8819
 
 
 
 
 
 
 
 
 
 
2920ce7
 
67f8819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
User model representing authenticated users managed by Better Auth.

Per @specs/001-auth-api-bridge/data-model.md
"""
from sqlmodel import SQLModel, Field, Relationship
from typing import TYPE_CHECKING, List
from datetime import datetime
from uuid import UUID, uuid4

if TYPE_CHECKING:
    from models.task import TaskTable
    from models.conversation import ConversationTable


class UserTable(SQLModel, table=True):
    """
    User account managed by Better Auth.

    The id field (UUID) MUST match the 'sub' claim in JWT tokens issued by Better Auth.
    """
    __tablename__ = "users"

    # Primary key - matches the 'sub' claim in JWT tokens
    id: UUID = Field(
        default_factory=uuid4,
        primary_key=True,
        index=True,
        description="Unique user identifier that matches JWT 'sub' claim"
    )

    # User profile information
    email: str = Field(
        unique=True,
        index=True,
        max_length=255,
        description="User's email address (unique)"
    )

    name: str | None = Field(
        default=None,
        max_length=255,
        description="User's display name"
    )

    # Timestamps
    created_at: datetime = Field(
        default_factory=datetime.utcnow,
        description="Timestamp when user account was created"
    )

    updated_at: datetime = Field(
        default_factory=datetime.utcnow,
        sa_column_kwargs={"onupdate": datetime.utcnow},
        description="Timestamp when user record was last updated"
    )

    # Relationships
    tasks: List["TaskTable"] = Relationship(
        back_populates="user",
        sa_relationship_kwargs={"cascade": "all, delete-orphan"}
    )
    conversations: List["ConversationTable"] = Relationship(
        back_populates="user",
        sa_relationship_kwargs={"cascade": "all, delete-orphan"}
    )