project structure and database
This commit is contained in:
0
backend/__init__.py
Normal file
0
backend/__init__.py
Normal file
14
backend/config.py
Normal file
14
backend/config.py
Normal file
@ -0,0 +1,14 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Database
|
||||
PG_LOGIN = os.environ.get("PG_LOGIN")
|
||||
PG_PASSWORD = os.environ.get("PG_PASSWORD")
|
||||
PG_PORT = os.environ.get("PG_PORT")
|
||||
PG_DATABASE = os.environ.get("PG_DATABASE")
|
||||
PG_HOST = os.environ.get("PG_HOST")
|
||||
|
||||
SECRET_KEY = os.environ.get("SECRET_KEY")
|
||||
11
backend/dependecies.py
Normal file
11
backend/dependecies.py
Normal file
@ -0,0 +1,11 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from backend.session import get_session
|
||||
from schemas.base import PaginationSchema
|
||||
from utils.pagination import pagination_parameters
|
||||
|
||||
SessionDependency = Annotated[AsyncSession, Depends(get_session)]
|
||||
PaginationDependency = Annotated[PaginationSchema, Depends(pagination_parameters)]
|
||||
21
backend/session.py
Normal file
21
backend/session.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from .config import PG_DATABASE, PG_HOST, PG_PASSWORD, PG_LOGIN
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with session_maker() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user