24 lines
717 B
Python
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)
|