-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbconnection.py
36 lines (27 loc) · 1.06 KB
/
dbconnection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.orm import declarative_base, sessionmaker, Session
from sqlalchemy.pool import StaticPool
import logging
logger = logging.getLogger("unsc_db_filler")
Base = declarative_base()
class DBConnection:
def __init__(self, host: str, dbname: str, user: str, password: str) -> None:
self.host: str = host
self.dbname: str = dbname
self.user: str = user
self.password: str = password
self.engine: Engine = create_engine(
f"postgresql+psycopg2://{user}:{password}@{host}/{dbname}",
echo=True,
poolclass=StaticPool,
)
self.session: Session = sessionmaker(bind=self.engine)()
Base.metadata.create_all(self.engine)
@property
def connection_string(self) -> str:
return f"host={self.host} dbname={self.dbname} user={self.user} password={self.password}"
def get_session(self) -> Session:
return self.session
def get_engine(self) -> Engine:
return self.engine