summit/backend/app/core/database.py

25 lines
680 B
Python

from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from app.core.config import settings
# Construct Async Database URL (replace postgresql:// with postgresql+asyncpg://)
SQLALCHEMY_DATABASE_URL = settings.DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://")
engine = create_async_engine(
SQLALCHEMY_DATABASE_URL,
echo=True, # Set to False in production
future=True,
)
SessionLocal = sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False,
autocommit=False,
autoflush=False,
)
async def get_db():
async with SessionLocal() as session:
yield session