Skip to content

feat: use correct mongodb client lifecycle#69

Merged
RafaelCenzano merged 6 commits into
mainfrom
fix-mongo-startup
Apr 1, 2026
Merged

feat: use correct mongodb client lifecycle#69
RafaelCenzano merged 6 commits into
mainfrom
fix-mongo-startup

Conversation

@RafaelCenzano

Copy link
Copy Markdown
Member

Description

  • Use proper mongodb client lifecycle and FastAPI dependency injection.
  • Add MongoDB indexes to improve queries

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Request so DB functions can access request.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.

Comment thread server/app.py
Comment thread server/database/laf.py Outdated
Comment thread server/database/loanertech.py Outdated
@RafaelCenzano RafaelCenzano merged commit 21fbfd2 into main Apr 1, 2026
6 checks passed
@RafaelCenzano RafaelCenzano deleted the fix-mongo-startup branch April 1, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants