2025-12-18 21:48:41 +01:00
|
|
|
import os
|
2025-12-21 21:54:26 +01:00
|
|
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
2025-12-18 21:48:41 +01:00
|
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
|
|
|
|
2025-12-21 21:54:26 +01:00
|
|
|
DATABASE_URL = os.getenv(
|
|
|
|
|
"DATABASE_URL", "postgresql+asyncpg://postgres:postgres@localhost:5432/arbret"
|
|
|
|
|
)
|
2025-12-18 21:48:41 +01:00
|
|
|
|
2025-12-21 23:16:29 +01:00
|
|
|
# asyncpg needs postgresql:// instead of postgresql+asyncpg://
|
|
|
|
|
ASYNCPG_DATABASE_URL = DATABASE_URL.replace("postgresql+asyncpg://", "postgresql://")
|
|
|
|
|
|
2025-12-18 21:48:41 +01:00
|
|
|
engine = create_async_engine(DATABASE_URL)
|
|
|
|
|
async_session = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_db():
|
|
|
|
|
async with async_session() as session:
|
|
|
|
|
yield session
|