Feat: [main] baekjoon-bot-v1
This commit is contained in:
21
db.py
Normal file
21
db.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import os
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", "").strip()
|
||||
if not DATABASE_URL:
|
||||
raise RuntimeError("DATABASE_URL is required (e.g. postgresql+asyncpg://...)")
|
||||
|
||||
engine = create_async_engine(DATABASE_URL, pool_pre_ping=True)
|
||||
|
||||
SessionLocal = async_sessionmaker(
|
||||
bind=engine,
|
||||
class_=AsyncSession,
|
||||
autoflush=False,
|
||||
expire_on_commit=False,
|
||||
)
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with SessionLocal() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user