-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
23 lines (20 loc) · 775 Bytes
/
Copy pathdatabase.py
File metadata and controls
23 lines (20 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, sessionmaker
from typing import Generator
import os
class Database:
def __init__(self):
self.DATABASE_URL = os.getenv("DATABASE_URL", "mysql+mysqlconnector://root:password123@localhost/policy-helper")
self.engine = create_engine(self.DATABASE_URL, echo=True)
self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine)
self.Base = declarative_base()
def get_db(self) -> Generator[Session, None, None]:
db = self.SessionLocal()
try:
yield db
finally:
db.close()
database = Database()
Base = database.Base
get_db = database.get_db