-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
33 lines (25 loc) · 1021 Bytes
/
context.py
File metadata and controls
33 lines (25 loc) · 1021 Bytes
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
from sqlalchemy import create_engine
from sqlalchemy import Engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import Session
# DB_CONNECTION_STRING = "sqlite:///./app.db"
# DB_CONNECTION_STRING = "postgresql+psycopg://user:password@localhost:5432/dbname"
# DB_CONNECTION_STRING = "mariadb+mariadbconnector://user:password@localhost:3306/dbname"
class DbContext:
def __init__(self, connection_string: str):
# sqlite engine
self._engine = create_engine(
connection_string,
connect_args={"check_same_thread": False},
)
# PostgreSQL / MySQL engine
# self._engine = create_engine(
# connection_string, echo=False, pool_pre_ping=True, pool_recycle=3600
# )
self._session_maker = sessionmaker(bind=self._engine, expire_on_commit=False)
@property
def session(self) -> sessionmaker[Session]:
return self._session_maker
@property
def engine(self) -> Engine:
return self._engine