Skip to content

Conversation

@devin-ai-integration
Copy link

feat: Add MongoDB Connection Module (MBA-265)

Summary

Implements Task 1.1: Creates a new MongoDB data access layer (chalicelib/mongodb.py) that mirrors the existing DynamoDB implementation pattern. This PR adds MongoDB support alongside DynamoDB without replacing existing functionality.

Key Changes:

  • ✅ Created chalicelib/mongodb.py with MongoChat class implementing 4 methods: putComment, getLatestComments, getRangeComments, getAllComments
  • ✅ Added pymongo dependency to requirements.txt
  • ✅ Updated .chalice/config.json with MongoDB environment variables (MONGODB_CONNECTION_STRING, MONGODB_DATABASE)
  • ✅ Added MongoDB test fixtures and test suite in tests/conftest.py and tests/test_app.py
  • ✅ Added commented MongoDB imports in app.py for future integration
  • ✅ Lint (flake8) checks passed

MongoDB Implementation Details:

  • Supports both local MongoDB (mongodb://localhost:27017) and MongoDB Atlas via connection string
  • Uses environment variable API_ENDPOINT to distinguish between local/cloud (same pattern as DynamoDB)
  • Creates indexes automatically: unique (name, time) and compound (chat_room, time DESC)
  • Maintains interface compatibility with DynamoDB methods

Review & Testing Checklist for Human

⚠️ CRITICAL: This implementation could not be tested locally due to environment setup issues (missing AWS region, DynamoDB Local, MongoDB). CI testing is essential.

  • Verify MongoDB tests pass in CI - The test suite requires MongoDB running on localhost:27017
  • Review interface compatibility - Check that MongoDB method signatures and return formats match DynamoDB expectations (note: getLatestComments returns dict with 'Items' key, while getRangeComments/getAllComments return lists)
  • Test index creation strategy - create_connection creates indexes on every call - consider if this should be done once during deployment instead
  • End-to-end integration test - The MongoDB code is not yet integrated into app.py (imports are commented). Consider testing the actual integration by uncommenting imports and switching to MongoDB
  • Verify environment variables - Ensure MONGODB_CONNECTION_STRING and MONGODB_DATABASE are properly configured in deployment environments

Recommended Test Plan

  1. Start local MongoDB: docker run -d -p 27017:27017 mongo:latest
  2. Run tests: export API_ENDPOINT=localhost && export AWS_DEFAULT_REGION=us-east-1 && pytest -vv
  3. Verify all MongoDB tests pass
  4. Test switching to MongoDB in app.py by uncommenting imports and replacing DdbChat usage
  5. Run the app locally and verify chat functionality works with MongoDB

Notes

  • Connection Management: MongoDB clients are not explicitly closed, relying on Python's garbage collection. Consider adding proper connection pooling or context managers for production use.
  • Timestamp Collisions: Using datetime.now().timestamp() as part of unique index - extremely unlikely but theoretically possible to have collisions under high load.
  • Error Handling: DuplicateKeyError is converted to generic Exception mimicking DynamoDB's ConditionalCheckFailedException - consider using a more specific custom exception.

Jira: MBA-265
Devin Run: https://app.devin.ai/sessions/86f6f5733cfe4618aba7b9be7adecd06
Requested by: Abhay Aggarwal (@abhay-codeium)

- Create chalicelib/mongodb.py with MongoChat class
- Implement putComment, getLatestComments, getRangeComments, getAllComments methods
- Add pymongo dependency to requirements.txt
- Add MongoDB environment variables to .chalice/config.json
- Add MongoDB test fixtures and test suite
- Update app.py with commented MongoDB imports for future use

Maintains interface compatibility with existing DynamoDB implementation.
Supports both local MongoDB (localhost:27017) and MongoDB Atlas.
Works alongside existing DynamoDB functionality without replacing it.

Jira: MBA-265
Co-Authored-By: Abhay Aggarwal <abhay.aggarwal@codeium.com>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

devin-ai-integration bot and others added 2 commits October 23, 2025 22:49
Python 3.7 reached EOL in June 2023 and is no longer available in GitHub Actions.
Update to Python 3.9 (oldest supported version) to fix CI failures.

Related to MBA-265

Co-Authored-By: Abhay Aggarwal <abhay.aggarwal@codeium.com>
DynamoDB Local was starting asynchronously without a readiness check,
causing tests to fail when pytest ran before the service was ready.
Added a wait loop to verify DynamoDB Local is accepting connections
before proceeding with tests.

Related to MBA-265

Co-Authored-By: Abhay Aggarwal <abhay.aggarwal@codeium.com>
@abhay-codeium
Copy link

Let's add more robust testing

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