Skip to content

Instantly share code, notes, and snippets.

@Glukhoff
Created October 21, 2022 10:35
Show Gist options
  • Save Glukhoff/8472c25d48ad320d53ee40ab25654a1a to your computer and use it in GitHub Desktop.
Save Glukhoff/8472c25d48ad320d53ee40ab25654a1a to your computer and use it in GitHub Desktop.
from typing import Callable, Any, Awaitable, Dict
from aiogram import BaseMiddleware
from aiogram.types import CallbackQuery
from bot.data.config import Config
from bot.models.base import create_poll
class DataBaseMiddleware(BaseMiddleware):
def __init__(self, config: Config) -> None:
self.sessionmaker = create_poll(
user=config.db.user,
password=config.db.password,
host=config.db.host,
name=config.db.name
)
async def __call__(
self,
handler: Callable[[CallbackQuery, Dict[str, Any]], Awaitable[Any]],
event: CallbackQuery,
data: Dict[str, Any]
) -> Any:
async with self.sessionmaker() as session:
data['db'] = session
return await handler(event, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment