feat: use correct mongodb client lifecycle#69
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the backend’s MongoDB integration to use a proper application-scoped client lifecycle via FastAPI’s lifespan and propagates DB access through the request/app state, while also adding index creation for query performance.
Changes:
- Introduces MongoDB startup/shutdown hooks and stores Mongo client/database in
app.state. - Refactors route + database layers to pass
Requestso DB functions can accessrequest.app.state.mongo_database. - Adds MongoDB index creation (notably for LAF, and a setup function for Backtest).
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| server/app.py | Adds MongoDB/Valkey lifecycle management in lifespan. |
| server/database/mongo.py | New MongoDB setup/shutdown + get_database() helper using app.state. |
| server/database/valkey.py | Replaces print with structured logging and adds shutdown hook. |
| server/helpers/cache.py | Adds aiocache key builder that excludes Request from cache keys. |
| server/database/laf.py | Moves DB access to request.app.state, adds multiple indexes, updates cached helpers to accept Request. |
| server/routes/laf.py | Threads Request through LAF route handlers into DB functions. |
| server/database/loanertech.py | Moves DB access to request.app.state and updates function signatures accordingly. |
| server/routes/loanertech.py | Threads Request through LoanerTech route handlers into DB functions. |
| server/database/backtest.py | Moves DB access to request.app.state, adds caching key builder usage, introduces backtest_db_setup() for indexes. |
| server/routes/backtest.py | Threads Request through Backtest route handlers into DB functions. |
| server/database/init.py | Removes global Mongo client/database initialization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.
Checklist: