Spaces:
Paused
Paused
File size: 1,593 Bytes
de829b3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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()
|