init
This commit is contained in:
24
backend/app/database.py
Normal file
24
backend/app/database.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import logging
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
from app.config import settings
|
||||
|
||||
logger = logging.getLogger("app.database")
|
||||
|
||||
logger.info("Initializing database engine: %s", settings.DATABASE_URL.split("@")[-1]) # log host only, not password
|
||||
|
||||
engine = create_async_engine(settings.DATABASE_URL, echo=False)
|
||||
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
async def get_db() -> AsyncSession:
|
||||
logger.debug("Opening database session")
|
||||
async with async_session() as session:
|
||||
yield session
|
||||
logger.debug("Database session closed")
|
||||
Reference in New Issue
Block a user