mirror of
				https://github.com/fastapi-users/fastapi-users.git
				synced 2025-11-04 14:45:50 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			40 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import AsyncGenerator, List
 | 
						|
 | 
						|
from fastapi import Depends
 | 
						|
from fastapi_users.db import (
 | 
						|
    SQLAlchemyBaseOAuthAccountTableUUID,
 | 
						|
    SQLAlchemyBaseUserTableUUID,
 | 
						|
    SQLAlchemyUserDatabase,
 | 
						|
)
 | 
						|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
 | 
						|
from sqlalchemy.orm import DeclarativeMeta, declarative_base, relationship, sessionmaker
 | 
						|
 | 
						|
DATABASE_URL = "sqlite+aiosqlite:///./test.db"
 | 
						|
Base: DeclarativeMeta = declarative_base()
 | 
						|
 | 
						|
 | 
						|
class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base):
 | 
						|
    pass
 | 
						|
 | 
						|
 | 
						|
class User(SQLAlchemyBaseUserTableUUID, Base):
 | 
						|
    oauth_accounts: List[OAuthAccount] = relationship("OAuthAccount", lazy="joined")
 | 
						|
 | 
						|
 | 
						|
engine = create_async_engine(DATABASE_URL)
 | 
						|
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(session, User, OAuthAccount)
 |