from datetime import datetime, timezone from sqlalchemy import DateTime, Index, Text from sqlalchemy.orm import Mapped, mapped_column from fabledassistant.models import Base class User(Base): __tablename__ = "users" id: Mapped[int] = mapped_column(primary_key=True) username: Mapped[str] = mapped_column(Text, unique=True, nullable=False) email: Mapped[str | None] = mapped_column(Text, nullable=True) password_hash: Mapped[str] = mapped_column(Text, nullable=False) role: Mapped[str] = mapped_column(Text, nullable=False, default="user") created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=lambda: datetime.now(timezone.utc) ) __table_args__ = ( Index("ix_users_username", "username"), ) def to_dict(self) -> dict: return { "id": self.id, "username": self.username, "email": self.email, "role": self.role, "created_at": self.created_at.isoformat(), }