cosmo_backend/app/models/db/nasa_cache.py

43 lines
1.5 KiB
Python

"""
NasaCache ORM model - NASA Horizons API cache
"""
from sqlalchemy import Column, String, TIMESTAMP, CheckConstraint, Index
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func
from app.database import Base
class NasaCache(Base):
"""NASA Horizons API response cache"""
__tablename__ = "nasa_cache"
cache_key = Column(
String(500),
primary_key=True,
comment="Cache key: {body_id}:{start}:{end}:{step}",
)
body_id = Column(String(50), nullable=True, comment="Body ID")
start_time = Column(TIMESTAMP, nullable=True, comment="Query start time")
end_time = Column(TIMESTAMP, nullable=True, comment="Query end time")
step = Column(String(10), nullable=True, comment="Time step (e.g., '1d')")
data = Column(JSONB, nullable=False, comment="Complete API response (JSON)")
expires_at = Column(
TIMESTAMP, nullable=False, comment="Cache expiration time"
)
created_at = Column(TIMESTAMP, server_default=func.now())
# Constraints and indexes
__table_args__ = (
CheckConstraint(
"end_time >= start_time",
name="chk_time_range",
),
Index("idx_nasa_cache_body_id", "body_id"),
Index("idx_nasa_cache_expires", "expires_at"),
Index("idx_nasa_cache_time_range", "body_id", "start_time", "end_time"),
)
def __repr__(self):
return f"<NasaCache(cache_key='{self.cache_key}', body_id='{self.body_id}', expires_at='{self.expires_at}')>"