|
| 1 | +from typing import Optional, List |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from sqlalchemy import String, DateTime, ForeignKey, Integer |
| 5 | +from sqlalchemy.orm import Mapped, mapped_column, relationship |
| 6 | + |
| 7 | +from app.core.database import Base |
| 8 | + |
| 9 | +class RateLimit(Base): |
| 10 | + __tablename__ = "rate_limit" |
| 11 | + |
| 12 | + id: Mapped[int] = mapped_column( |
| 13 | + "id", autoincrement=True, nullable=False, unique=True, primary_key=True, init=False |
| 14 | + ) |
| 15 | + name: Mapped[str] = mapped_column(String, nullable=False, unique=True) |
| 16 | + path: Mapped[str] = mapped_column(String, nullable=False) |
| 17 | + limit: Mapped[int] = mapped_column(Integer, nullable=False) |
| 18 | + period: Mapped[int] = mapped_column(Integer, nullable=False) |
| 19 | + |
| 20 | + tier_rate_limits: Mapped["TierRateLimit"] = relationship(back_populates="rate_limit", cascade="all, delete", lazy="selectin", default_factory=list) |
| 21 | + |
| 22 | + |
| 23 | +class Tier(Base): |
| 24 | + __tablename__ = "tier" |
| 25 | + |
| 26 | + id: Mapped[int] = mapped_column( |
| 27 | + "id", autoincrement=True, nullable=False, unique=True, primary_key=True, init=False |
| 28 | + ) |
| 29 | + name: Mapped[str] = mapped_column(String, nullable=False, unique=True) |
| 30 | + |
| 31 | + users: Mapped[List["User"]] = relationship(back_populates="tier", cascade="save-update, merge", lazy="selectin", default_factory=list) |
| 32 | + tier_rate_limits: Mapped["TierRateLimit"] = relationship(back_populates="tier", cascade="all, delete", lazy="selectin", default_factory=list) |
| 33 | + created_at: Mapped[datetime] = mapped_column( |
| 34 | + DateTime, default_factory=datetime.utcnow |
| 35 | + ) |
| 36 | + updated_at: Mapped[Optional[datetime]] = mapped_column(default=None) |
| 37 | + |
| 38 | + |
| 39 | +class TierRateLimit(Base): |
| 40 | + __tablename__ = "tier_rate_limit" |
| 41 | + |
| 42 | + id: Mapped[int] = mapped_column( |
| 43 | + "id", autoincrement=True, nullable=False, unique=True, primary_key=True, init=False |
| 44 | + ) |
| 45 | + |
| 46 | + rate_limit_id: Mapped[int] = mapped_column(ForeignKey("rate_limit.id"), index=True) |
| 47 | + tier_id: Mapped[int] = mapped_column(ForeignKey("tier.id"), index=True) |
| 48 | + |
| 49 | + created_at: Mapped[datetime] = mapped_column( |
| 50 | + DateTime, default_factory=datetime.utcnow |
| 51 | + ) |
| 52 | + |
| 53 | + tier: Mapped[Tier] = relationship(back_populates="tier_rate_limits", init=False) |
| 54 | + rate_limit: Mapped[RateLimit] = relationship(back_populates="tier_rate_limits", init=False) |
0 commit comments