from collections.abc import AsyncGenerator from fastapi import Depends from fastapi_users.db import ( SQLAlchemyBaseOAuthAccountTableUUID, SQLAlchemyBaseUserTableUUID, SQLAlchemyUserDatabase, ) from sqlalchemy import String from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import Mapped, mapped_column, relationship from media_manager.database import Base, get_async_session class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base): access_token: Mapped[str] = mapped_column(String(length=4096), nullable=False) refresh_token: Mapped[str | None] = mapped_column( String(length=4096), nullable=True ) class User(SQLAlchemyBaseUserTableUUID, Base): oauth_accounts: Mapped[list[OAuthAccount]] = relationship( "OAuthAccount", lazy="joined" ) async def get_user_db( session: AsyncSession = Depends(get_async_session), ) -> AsyncGenerator[SQLAlchemyUserDatabase]: yield SQLAlchemyUserDatabase(session, User, OAuthAccount)