Исходный код infrastructure.database.database

import os
from sqlalchemy.ext.asyncio import (
    AsyncSession,
    async_sessionmaker,
    create_async_engine,
)

from infrastructure.database.models import Base
from infrastructure.database.scripts import migration
from config import settings

DATABASE_URL = settings.database_url

engine = create_async_engine(
    DATABASE_URL,
    future=True,
    pool_size=25,
    max_overflow=50,
    pool_pre_ping=True,
    echo=False
)

AsyncSessionLocal = async_sessionmaker(
    bind=engine,
    class_=AsyncSession,
    expire_on_commit=False,
    autoflush=False,
)


[документация] async def init_models() -> None: async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all)
[документация] async def init_migrations() -> None: await migration.run_migrations()
[документация] async def get_db(): async with AsyncSessionLocal() as session: yield session