mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-17 15:13:24 +02:00
37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
from collections.abc import AsyncGenerator
|
|
|
|
from fastapi import Depends
|
|
from fastapi_users.db import (
|
|
SQLAlchemyBaseUserTableUUID,
|
|
SQLAlchemyUserDatabase,
|
|
SQLAlchemyBaseOAuthAccountTableUUID,
|
|
)
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
from sqlalchemy.orm import Mapped, relationship
|
|
|
|
from media_manager.database import Base
|
|
from media_manager.database import db_url
|
|
|
|
|
|
class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base):
|
|
pass
|
|
|
|
|
|
class User(SQLAlchemyBaseUserTableUUID, Base):
|
|
oauth_accounts: Mapped[list[OAuthAccount]] = relationship(
|
|
"OAuthAccount", lazy="joined"
|
|
)
|
|
|
|
|
|
engine = create_async_engine(db_url, echo=False)
|
|
async_session_maker = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
|
|
async with async_session_maker() as session:
|
|
yield session
|
|
|
|
|
|
async def get_user_db(session: AsyncSession = Depends(get_async_session)):
|
|
yield SQLAlchemyUserDatabase(session, User, OAuthAccount)
|