Implement custom config class; get SQLAlchemy database URI from config
This commit is contained in:
parent
31df889dcb
commit
04d434b4a3
7 changed files with 68 additions and 13 deletions
|
|
@ -5,6 +5,7 @@ from sqlalchemy import MetaData, create_engine
|
|||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session, scoped_session, sessionmaker
|
||||
|
||||
from tofu_api.common.config import Config
|
||||
from .metadata import metadata_obj
|
||||
|
||||
__all__ = [
|
||||
|
|
@ -23,19 +24,22 @@ class SQLAlchemy:
|
|||
"""
|
||||
Initializes the SQLAlchemy engine.
|
||||
"""
|
||||
self._engine = self._create_engine()
|
||||
self._engine = self._create_engine(app.config)
|
||||
self._scoped_session = self._create_scoped_session()
|
||||
|
||||
@app.teardown_appcontext
|
||||
def shutdown_session(_exception=None):
|
||||
self._scoped_session.remove()
|
||||
|
||||
def _create_engine(self) -> Engine:
|
||||
@staticmethod
|
||||
def _create_engine(config: Config) -> Engine:
|
||||
"""
|
||||
Create the database engine using the app configuration.
|
||||
"""
|
||||
# TODO: Use config
|
||||
return create_engine('sqlite:////tmp/test.db')
|
||||
return create_engine(
|
||||
config.sqlalchemy_database_uri,
|
||||
echo=config.sqlalchemy_echo,
|
||||
)
|
||||
|
||||
def _create_scoped_session(self) -> scoped_session:
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue