book / backend /database.py
Ammar Ahmed Khan
Add Physical AI Humanoid Book Platform
e2eff86
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os
# Get database URL from environment variables
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./physical_ai_book.db")
# Create engine
engine = create_engine(DATABASE_URL)
# Create session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Base class for models
Base = declarative_base()
def get_db():
"""
Dependency to get database session
"""
db = SessionLocal()
try:
yield db
finally:
db.close()
# Import all models here to ensure they are registered with Base
import sys
import os
# Add the backend directory to the path to allow absolute imports
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__))))
from models import user, translation, book_content_chunk