2022-03-28 20:58:40 +00:00
|
|
|
"""
|
2022-03-28 20:59:27 +00:00
|
|
|
Database connection management.
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-03-27 01:17:48 +00:00
|
|
|
from sqlmodel import Session, SQLModel, create_engine
|
|
|
|
|
|
|
|
|
|
|
|
class Connection:
|
|
|
|
"""
|
2022-03-28 20:58:40 +00:00
|
|
|
Namespace for the database connection
|
2022-03-27 01:17:48 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
engine = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def connect(cls, connection_url: str) -> None:
|
|
|
|
"""
|
|
|
|
Connect ORM to a database engine.
|
|
|
|
"""
|
|
|
|
|
|
|
|
cls.engine = create_engine(connection_url)
|
|
|
|
SQLModel.metadata.create_all(cls.engine)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@property
|
|
|
|
def session(cls) -> Session | None:
|
|
|
|
"""
|
|
|
|
Create an ORM session using a context manager.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if cls.engine is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return Session(cls.engine)
|