1- import uuid
21from datetime import datetime
32
4- from sqlalchemy import Boolean , DateTime , String , Index
3+ from sqlalchemy import Boolean , DateTime , Index , String
54from sqlalchemy .orm import Mapped , mapped_column , relationship
65
6+ from src .modules .user .infrastructure .models .user_model import UserModel
77from src .shared .database .mixin .timestamp import SoftDeleteMixin , TimeStampMixin
88from src .shared .database .model import Base
99
1010
1111class UserSessionModel (Base , TimeStampMixin , SoftDeleteMixin ):
1212 """User session management for tracking active sessions.
13-
13+
1414 One-to-many relationship with users table.
1515 Stores refresh tokens, device info, and login history.
1616 """
17+
1718 __tablename__ = "user_sessions"
1819 __table_args__ = (
1920 Index ("ix_user_sessions_user_id" , "user_id" ),
@@ -27,26 +28,30 @@ class UserSessionModel(Base, TimeStampMixin, SoftDeleteMixin):
2728 String (36 ), # UUID as string for FK
2829 nullable = False ,
2930 )
30-
31+
3132 # Session Token (hashed for security)
3233 refresh_token_hash : Mapped [str ] = mapped_column (String (255 ), nullable = False )
33-
34+
3435 # Session Expiry
35- expires_at : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), nullable = False )
36-
36+ expires_at : Mapped [datetime ] = mapped_column (
37+ DateTime (timezone = True ), nullable = False
38+ )
39+
3740 # Device and Location Info
3841 device_info : Mapped [str | None ] = mapped_column (String (500 ), nullable = True )
39- ip_address : Mapped [str | None ] = mapped_column (String (45 ), nullable = True ) # IPv6 compatible
42+ ip_address : Mapped [str | None ] = mapped_column (
43+ String (45 ), nullable = True
44+ ) # IPv6 compatible
4045 user_agent : Mapped [str | None ] = mapped_column (String (1000 ), nullable = True )
41-
46+
4247 # Session Status
4348 is_revoked : Mapped [bool ] = mapped_column (Boolean , default = False , nullable = False )
4449 revoked_at : Mapped [datetime | None ] = mapped_column (
4550 DateTime (timezone = True ),
4651 nullable = True ,
4752 )
4853 revoked_reason : Mapped [str | None ] = mapped_column (String (255 ), nullable = True )
49-
54+
5055 # Relationship
5156 user : Mapped ["UserModel" ] = relationship (
5257 back_populates = "sessions" ,
0 commit comments