25 lines
		
	
	
		
			824 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			25 lines
		
	
	
		
			824 B
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import Annotated
 | 
						|
 | 
						|
from fastapi import Depends
 | 
						|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
 | 
						|
from sqlalchemy.orm import sessionmaker, declarative_base
 | 
						|
 | 
						|
from settings import PG_LOGIN, PG_PASSWORD, PG_HOST, PG_DATABASE
 | 
						|
 | 
						|
DATABASE_URL = f'postgresql+asyncpg://{PG_LOGIN}:{PG_PASSWORD}@{PG_HOST}/{PG_DATABASE}'
 | 
						|
engine = create_async_engine(DATABASE_URL)
 | 
						|
session_maker = sessionmaker(engine,
 | 
						|
                             class_=AsyncSession,
 | 
						|
                             expire_on_commit=False,
 | 
						|
                             autocommit=False,
 | 
						|
                             autoflush=False)
 | 
						|
BaseModel = declarative_base()
 | 
						|
 | 
						|
 | 
						|
async def get_session() -> AsyncSession:
 | 
						|
    async with session_maker() as session:
 | 
						|
        yield session
 | 
						|
 | 
						|
 | 
						|
DatabaseDependency = Annotated[AsyncSession, Depends(get_session)]
 |