mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 06:57:47 +01:00
26 lines
847 B
Python
26 lines
847 B
Python
from typing import AsyncGenerator
|
|
from fastapi import Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from fastapi_users.db import SQLAlchemyUserDatabase
|
|
|
|
from ..core.db import async_session_maker, engine
|
|
from ..core.base import Base
|
|
from ..models.user import User, OAuthAccount
|
|
|
|
_initialized = False
|
|
|
|
|
|
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
|
|
global _initialized
|
|
if not _initialized:
|
|
# Lazily ensure tables exist; helpful for test runs without migrations
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
_initialized = True
|
|
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)
|