File size: 1,900 Bytes
1f70d93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
"""
SQLAlchemy ORM model for SessionToken entity.

This module defines the database schema for the session_tokens table.
Tracks active JWT sessions for logout and revocation functionality.
"""

from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Integer, Text
from sqlalchemy.orm import Mapped, mapped_column
from core.database import Base


class SessionToken(Base):
    """
    SQLAlchemy ORM model for SessionToken entity.

    Represents the session_tokens table in PostgreSQL database.
    Stores JWT tokens for session management and logout functionality.

    Attributes:
        id: Primary key, auto-incrementing integer
        user_id: Foreign key to users table
        token: JWT token string (unique)
        expires_at: Token expiration timestamp
        created_at: Timestamp when token was created
    """
    __tablename__ = "session_tokens"

    # Primary key with auto-increment
    id: Mapped[int] = mapped_column(
        Integer,
        primary_key=True,
        autoincrement=True
    )

    # Foreign key to users table
    user_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False,
        index=True
    )

    # JWT token string (unique for lookup)
    token: Mapped[str] = mapped_column(
        Text,
        unique=True,
        nullable=False,
        index=True
    )

    # Token expiration timestamp
    expires_at: Mapped[datetime] = mapped_column(
        DateTime,
        nullable=False,
        index=True
    )

    # Automatic timestamp on creation
    created_at: Mapped[datetime] = mapped_column(
        DateTime,
        default=datetime.utcnow,
        nullable=False
    )

    def __repr__(self) -> str:
        """String representation for debugging"""
        return f"<SessionToken(id={self.id}, user_id={self.user_id}, expires_at={self.expires_at})>"