29 lines
787 B
Python
29 lines
787 B
Python
import os
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, Session
|
|
from typing import Generator
|
|
from fastapi import Depends
|
|
|
|
DB_DATABASE = os.getenv('DB_DATABASE')
|
|
DB_USER = os.getenv('DB_USER')
|
|
DB_PASSWORD = os.getenv('DB_PASSWORD')
|
|
DB_NAME = os.getenv('DB_NAME')
|
|
DB_HOST = os.getenv('DB_HOST')
|
|
DB_PORT = os.getenv('DB_PORT')
|
|
DATABASE = os.getenv('DATABASE')
|
|
|
|
|
|
engine = create_engine(f'postgresql+psycopg2://{DB_NAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DATABASE}')
|
|
SessionLocal = sessionmaker(autocommit = False, autoflush=False, bind = engine)
|
|
|
|
def getDb() -> Generator[Session, None, None]:
|
|
db: Session = SessionLocal
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
def get_user_repo(db = Depends(getDb())):
|
|
return SQLUserRepository()
|