The best way to interact with a MongoDB database in a FastAPI application is to use an asynchronous MongoDB driver, such as motor
. It integrates seamlessly with FastAPI and allows for non-blocking database operations, which is important to maintain the responsiveness of your application.
Key Considerations
- Separation of Concerns:
- It is best practice to separate the database interaction logic into a service or utility module to ensure your code adheres to the Single Responsibility Principle.
- Avoid putting CRUD operations directly into your endpoint handlers to keep them clean and focused on handling HTTP requests/responses.
- Insert Operation Responsibility:
- For the
/query
endpoint, it makes sense to log the result to the database within the same request lifecycle, especially if this is an essential feature of the endpoint (e.g., saving query history).- However, more complex database operations (e.g., retrieving a history of queries) should be handled by separate endpoints.
MongoDB Schema Design
This schema will support the plans for the front end
User Document
Chat Message Document
Chat Window Document
Chat Configs
Manage config details for a specific chat session (eg, LLM settings)
- Design: Embed these configurations directly in the
chat_window
document.
Results
Stores details about LLM responses and metadata
- Design: Embed result data in the
chat_message
document, as it is tightly coupled with each message.
users stores user data, preferences, and linked chats
chat_messages stores individual messages and their associated results
chat_windows stores high-level chat session info
motor mongodb backend development research webapp fastapi python