45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
from sqlalchemy import Column, String, Float, Integer, DateTime, ForeignKey, Boolean
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
from app.database import Base
|
|
|
|
class Asset(Base):
|
|
__tablename__ = "assets"
|
|
|
|
id = Column(String, primary_key=True) # Slug of the asset (e.g., "bitcoin")
|
|
rank = Column(Integer)
|
|
symbol = Column(String, index=True)
|
|
name = Column(String, index=True)
|
|
supply = Column(Float)
|
|
max_supply = Column(Float, nullable=True)
|
|
market_cap_usd = Column(Float)
|
|
volume_usd_24hr = Column(Float)
|
|
price_usd = Column(Float)
|
|
change_percent_24hr = Column(Float, nullable=True)
|
|
vwap_24hr = Column(Float, nullable=True)
|
|
explorer = Column(String, nullable=True)
|
|
|
|
# Last time the data was updated
|
|
last_updated = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
|
|
|
# Relationships
|
|
price_history = relationship("AssetPriceHistory", back_populates="asset", cascade="all, delete-orphan")
|
|
|
|
def __repr__(self):
|
|
return f"<Asset {self.symbol}: {self.name}>"
|
|
|
|
|
|
class AssetPriceHistory(Base):
|
|
__tablename__ = "asset_price_history"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
asset_id = Column(String, ForeignKey("assets.id", ondelete="CASCADE"), index=True)
|
|
price_usd = Column(Float)
|
|
timestamp = Column(DateTime(timezone=True), index=True)
|
|
interval = Column(String) # m1, m5, m15, m30, h1, h2, h6, h12, d1
|
|
|
|
# Relationship
|
|
asset = relationship("Asset", back_populates="price_history")
|
|
|
|
def __repr__(self):
|
|
return f"<AssetPriceHistory {self.asset_id} @ {self.timestamp}>" |