30 lines
734 B
Python
30 lines
734 B
Python
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
|
ENVIRONMENT = "dev"
|
|
DATABASE_URL = ""
|
|
|
|
# 비동기 엔진 생성
|
|
engine = create_async_engine(
|
|
DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://"),
|
|
echo=True if ENVIRONMENT == "dev" else False,
|
|
future=True
|
|
)
|
|
|
|
# 세션 팩토리 생성
|
|
AsyncSessionLocal = sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False
|
|
)
|
|
|
|
# Base 클래스 생성
|
|
Base = declarative_base()
|
|
|
|
# 데이터베이스 세션 의존성
|
|
async def get_db():
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close()
|