mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 03:04:27 +08:00
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
from typing import AsyncGenerator
|
|
|
|
from fastapi import Depends
|
|
from fastapi_users.db import (
|
|
SQLAlchemyBaseOAuthAccountTable,
|
|
SQLAlchemyBaseUserTable,
|
|
SQLAlchemyUserDatabase,
|
|
)
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
|
from sqlalchemy.orm import relationship, sessionmaker
|
|
|
|
from app.models import UserDB
|
|
|
|
DATABASE_URL = "sqlite+aiosqlite:///./test.db"
|
|
Base: DeclarativeMeta = declarative_base()
|
|
|
|
|
|
class UserTable(Base, SQLAlchemyBaseUserTable):
|
|
oauth_accounts = relationship("OAuthAccount")
|
|
|
|
|
|
class OAuthAccountTable(SQLAlchemyBaseOAuthAccountTable, Base):
|
|
pass
|
|
|
|
|
|
engine = create_async_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
|
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
async def create_db_and_tables():
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
|
|
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(UserDB, session, UserTable, OAuthAccountTable)
|