cosmo_backend/app/models/db/resource.py

53 lines
1.9 KiB
Python

"""
Resource ORM model - File management
"""
from sqlalchemy import Column, String, Integer, TIMESTAMP, ForeignKey, CheckConstraint, Index
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.database import Base
class Resource(Base):
"""Resource files (textures, models, icons, etc.)"""
__tablename__ = "resources"
id = Column(Integer, primary_key=True, autoincrement=True)
body_id = Column(
String(50),
ForeignKey("celestial_bodies.id", ondelete="CASCADE"),
nullable=True,
comment="Reference to celestial_bodies.id (optional)",
)
resource_type = Column(
String(50), nullable=False, comment="Resource type"
)
file_path = Column(
String(500),
nullable=False,
comment="Relative path from upload directory",
)
file_size = Column(Integer, nullable=True, comment="File size in bytes")
mime_type = Column(String(100), nullable=True, comment="MIME type")
extra_data = Column(JSONB, nullable=True, comment="Extended metadata (JSON)")
created_at = Column(TIMESTAMP, server_default=func.now())
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
# Relationship
body = relationship("CelestialBody", back_populates="resources")
# Constraints and indexes
__table_args__ = (
CheckConstraint(
"resource_type IN ('texture', 'model', 'icon', 'thumbnail', 'data')",
name="chk_resource_type",
),
Index("idx_resources_body_id", "body_id"),
Index("idx_resources_type", "resource_type"),
Index("idx_resources_unique", "body_id", "resource_type", "file_path", unique=True),
)
def __repr__(self):
return f"<Resource(id={self.id}, body_id='{self.body_id}', type='{self.resource_type}', path='{self.file_path}')>"