44 lines
995 B
Python
44 lines
995 B
Python
import os
|
|
from typing import Annotated
|
|
|
|
from fastapi import Depends
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
Base = declarative_base()
|
|
|
|
# Database configuration
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./investors.db")
|
|
|
|
# Create engine
|
|
engine = create_engine(DATABASE_URL, echo=False)
|
|
|
|
# Create session factory
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
db_dependency = Annotated[Session, Depends(get_db)]
|
|
|
|
|
|
def init_database():
|
|
"""Initialize the database by creating all tables"""
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
def get_session_sync() -> Session:
|
|
"""Get a database session for synchronous operations"""
|
|
return SessionLocal()
|
|
|
|
def get_db_session():
|
|
"""Get a database session for direct use."""
|
|
return SessionLocal()
|