38 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| from sqlalchemy import create_engine
 | |
| from sqlalchemy.orm import sessionmaker
 | |
| from sqlalchemy.ext.declarative import declarative_base
 | |
| import os
 | |
| from dotenv import load_dotenv
 | |
| 
 | |
| load_dotenv()
 | |
| 
 | |
| # Get database URL from environment with SQLite fallback for development
 | |
| DATABASE_URL = os.getenv("DATABASE_URL")
 | |
| 
 | |
| if not DATABASE_URL:
 | |
|     # Default to SQLite for development if no PostgreSQL URL is provided
 | |
|     DATABASE_URL = "sqlite:///./product_tracker.db"
 | |
|     print("🔄 Using SQLite database for development")
 | |
| else:
 | |
|     print(f"🐘 Using PostgreSQL database")
 | |
| 
 | |
| # Configure engine based on database type
 | |
| if DATABASE_URL.startswith("sqlite"):
 | |
|     engine = create_engine(
 | |
|         DATABASE_URL, 
 | |
|         connect_args={"check_same_thread": False}  # Needed for SQLite
 | |
|     )
 | |
| else:
 | |
|     engine = create_engine(DATABASE_URL)
 | |
| 
 | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
 | |
| 
 | |
| Base = declarative_base()
 | |
| 
 | |
| # Dependency to get database session
 | |
| def get_db():
 | |
|     db = SessionLocal()
 | |
|     try:
 | |
|         yield db
 | |
|     finally:
 | |
|         db.close()  |