Spaces:
Running
Running
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, Text | |
from sqlalchemy.orm import relationship, Mapped, mapped_column | |
from datetime import datetime | |
from .database import Base | |
class User(Base): | |
__tablename__ = "users" | |
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) | |
email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False) | |
password_hash: Mapped[str] = mapped_column(String(255), nullable=False) | |
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) | |
api_keys = relationship("APIKey", back_populates="owner", cascade="all, delete-orphan") | |
messages = relationship("ChatMessage", back_populates="user", cascade="all, delete-orphan") | |
class APIKey(Base): | |
__tablename__ = "api_keys" | |
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) | |
key: Mapped[str] = mapped_column(String(255), unique=True, nullable=False) | |
revoked: Mapped[bool] = mapped_column(Boolean, default=False) | |
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) | |
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id", ondelete="CASCADE")) | |
owner = relationship("User", back_populates="api_keys") | |
class ChatMessage(Base): | |
__tablename__ = "chat_history" | |
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) | |
role: Mapped[str] = mapped_column(String(20), default="user") # 'user' or 'assistant' | |
content: Mapped[str] = mapped_column(Text, nullable=False) | |
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) | |
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id", ondelete="CASCADE")) | |
user = relationship("User", back_populates="messages") | |