Files
Solo_bot/middlewares/database.py
T
Zakhar Izmaylov 0867ebdd61 Fix typing
2024-11-24 01:19:00 +03:00

23 lines
602 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:
conn = await asyncpg.connect(DATABASE_URL)
try:
data["session"] = conn
return await handler(event, data)
finally:
await conn.close()