This repository has been archived on 2022-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
it-volunteers-for-peace/middlewares/db.py
2022-10-16 22:50:37 +03:00

24 lines
649 B
Python

from aiogram.dispatcher.middlewares import LifetimeControllerMiddleware
from asyncpg.pool import Pool
from services.repositories import Repos
class DbMiddleware(LifetimeControllerMiddleware):
skip_patterns = ["error", "update"]
def __init__(self, pool: Pool):
super().__init__()
self.pool = pool
async def pre_process(self, obj, data, *args):
conn = await self.pool.acquire()
data["conn"] = conn
data["repo"] = Repos(conn)
async def post_process(self, obj, data, *args):
del data["repo"]
conn = data.get("conn")
if conn:
await self.pool.release(conn)