-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
fastapi.Depends
for dependency injection of the database (#90)
* Add engine as parameter to allow dependency injection * Add engine parameter to allow dependency injection * Move database initialization to shared setup * Use fastapi.Depends for dependency injection at the endpoint * Move old and new dataset endpoint tests to separate files * Add database dependencies * Define auto-injected parameters last There is a quirck where `None` is a valid `Engine`, which allows us to put it behind other optional parameters. In principle, I do not like that it is technical not optional (but provided by FastAPI) but I do prefer having these parameters last instead of first.
- Loading branch information
Showing
9 changed files
with
175 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from config import load_database_configuration | ||
from sqlalchemy import Engine, create_engine | ||
from sqlalchemy.engine import URL | ||
|
||
_user_engine = None | ||
_expdb_engine = None | ||
|
||
|
||
def _create_engine(database_name: str) -> Engine: | ||
database_configuration = load_database_configuration() | ||
db_url = URL.create(**database_configuration[database_name]) | ||
return create_engine( | ||
db_url, | ||
echo=True, | ||
pool_recycle=3600, | ||
) | ||
|
||
|
||
def user_database() -> Engine: | ||
global _user_engine | ||
if _user_engine is None: | ||
_user_engine = _create_engine("openml") | ||
return _user_engine | ||
|
||
|
||
def expdb_database() -> Engine: | ||
global _expdb_engine | ||
if _expdb_engine is None: | ||
_expdb_engine = _create_engine("expdb") | ||
return _expdb_engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.