from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy.orm import declarative_base from ..core.config import settings import contextlib # Create async engine with pgbouncer configuration async_engine = create_async_engine( "postgresql+asyncpg://postgres.mqyrkmsdgugdhxiucukb:Lovyelias5584.@aws-0-eu-central-1.pooler.supabase.com:6543/postgres", echo=True, future=True, pool_pre_ping=True, connect_args={ "statement_cache_size": 0, # Required for pgbouncer "prepared_statement_cache_size": 0 # Disable prepared statement cache } ) AsyncSessionLocal = async_sessionmaker( bind=async_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() class Database: def __init__(self): self._session_factory = AsyncSessionLocal @contextlib.asynccontextmanager async def session(self): """Get a database session with automatic commit/rollback""" session = self._session_factory() try: yield session await session.commit() except: await session.rollback() raise finally: await session.close() async def get_session(self): """Get a session for manual management""" return self._session_factory() db = Database()