Phase 1: initial codes
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, Session
|
||||
from db.models import Base
|
||||
|
||||
_DB_PATH = Path(__file__).parent.parent.parent / "stockmind.db"
|
||||
_ENGINE = None
|
||||
_SessionLocal = None
|
||||
|
||||
|
||||
def _get_engine():
|
||||
global _ENGINE
|
||||
if _ENGINE is None:
|
||||
_ENGINE = create_engine(f"sqlite:///{_DB_PATH}", connect_args={"check_same_thread": False})
|
||||
return _ENGINE
|
||||
|
||||
|
||||
def init_db():
|
||||
engine = _get_engine()
|
||||
Base.metadata.create_all(engine)
|
||||
global _SessionLocal
|
||||
_SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def get_session() -> Session:
|
||||
global _SessionLocal
|
||||
if _SessionLocal is None:
|
||||
init_db()
|
||||
session = _SessionLocal()
|
||||
try:
|
||||
yield session
|
||||
except Exception:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
@@ -0,0 +1,74 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, Float, Integer, Boolean, DateTime, Text
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class WatchlistItem(Base):
|
||||
__tablename__ = "watchlist_items"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
symbol: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(100), default="")
|
||||
group_name: Mapped[str] = mapped_column(String(50), default="Default")
|
||||
added_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
|
||||
class PortfolioPosition(Base):
|
||||
__tablename__ = "portfolio_positions"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
symbol: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
shares: Mapped[float] = mapped_column(Float, default=0.0)
|
||||
avg_cost: Mapped[float] = mapped_column(Float, default=0.0)
|
||||
broker: Mapped[str] = mapped_column(String(30), default="manual")
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
|
||||
class Alert(Base):
|
||||
__tablename__ = "alerts"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
symbol: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
alert_type: Mapped[str] = mapped_column(String(20), nullable=False) # price_above, price_below, volume_spike
|
||||
target_value: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
triggered_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
|
||||
class Setting(Base):
|
||||
__tablename__ = "settings"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
key: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
|
||||
value: Mapped[str] = mapped_column(Text, default="")
|
||||
|
||||
|
||||
class NewsCache(Base):
|
||||
__tablename__ = "news_cache"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
symbol: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
title: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
description: Mapped[str] = mapped_column(Text, default="")
|
||||
url: Mapped[str] = mapped_column(Text, default="")
|
||||
source: Mapped[str] = mapped_column(String(100), default="")
|
||||
published_at: Mapped[str] = mapped_column(String(50), default="")
|
||||
sentiment: Mapped[str] = mapped_column(String(20), default="")
|
||||
fetched_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
|
||||
class QuoteCache(Base):
|
||||
__tablename__ = "quote_cache"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
symbol: Mapped[str] = mapped_column(String(20), unique=True, nullable=False)
|
||||
price: Mapped[float] = mapped_column(Float, default=0.0)
|
||||
change: Mapped[float] = mapped_column(Float, default=0.0)
|
||||
change_pct: Mapped[float] = mapped_column(Float, default=0.0)
|
||||
volume: Mapped[int] = mapped_column(Integer, default=0)
|
||||
fetched_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
Reference in New Issue
Block a user