39 lines
938 B
Python
39 lines
938 B
Python
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.core.config import settings
|
|
|
|
# PostgreSQL 연결 URL 변환 (asyncpg 사용)
|
|
DATABASE_URL = settings.DATABASE_URL
|
|
if DATABASE_URL.startswith("postgresql://"):
|
|
DATABASE_URL = DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://")
|
|
|
|
engine = create_async_engine(
|
|
DATABASE_URL,
|
|
echo=settings.DEBUG,
|
|
future=True,
|
|
pool_pre_ping=True,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
connect_args={
|
|
"timeout": 60,
|
|
"command_timeout": 60,
|
|
}
|
|
)
|
|
|
|
AsyncSessionLocal = sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
async def get_db():
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close()
|