Files
Solo_bot/middlewares/database.py
T
Zakhar Izmaylov 9673f10af7 Update config
2024-11-22 21:24:55 +03:00

24 lines
717 B
Python

from typing import Any, Awaitable, Callable, Dict
from aiogram import BaseMiddleware
from aiogram.types import TelegramObject
import asyncpg
from config import DATABASE_URL
class DatabaseMiddleware(BaseMiddleware):
async def __call__(
self,
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
event: TelegramObject,
data: Dict[str, Any],
) -> Any:
async with await asyncpg.create_pool(DATABASE_URL) as pool:
async with pool.acquire() as session:
data["session"] = session
try:
return await handler(event, data)
finally:
await pool.release(session)