Repository MongoDB
A robust MongoDB repository pattern implementation featuring automated transaction management, type-safe operations, and comprehensive replica set support.
Features
- Type-Safe Repository Pattern: Generic repository implementation with strong typing support
- Automated Transaction Management: Declarative transaction handling using metaclasses
- Replica Set Support: Built-in support for MongoDB replica sets with automated setup
- Robust Error Handling: Comprehensive error handling and logging throughout the stack
- Clean Architecture: Follows SOLID principles and clean architecture patterns
Prerequisites
- Docker and Docker Compose
- Python 3.10 or higher
- pip (Python package manager)
Quick Start
- Clone the repository:
git clone https://github.com/yourusername/repository_mongodb.git
cd repository_mongodb
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Setup MongoDB Replica Set:
chmod +x deployment/scripts/setup-mongodb.sh
sudo ./deployment/scripts/setup-mongodb.sh
Project Structure
repository_mongodb/
├── repository_mongodb/
│ ├── base_model.py # Base model with MongoDB integration
│ ├── base_repository.py # Generic repository implementation
│ ├── mongo_client.py # MongoDB client configuration
│ ├── mongo_config.py # MongoDB connection settings
│ ├── transaction_management.py # Transaction context managers
│ └── transaction_metaclass.py # Automated transaction handling
├── deployment/
│ ├── docker/
│ │ ├── docker-compose.yml # MongoDB replica set configuration
│ │ └── mongodb/ # MongoDB data directory
│ └── scripts/
│ └── setup-mongodb.sh # Automated setup script
├── tests/
│ ├── test_base_repository.py # Repository tests
│ └── conftest.py # Pytest fixtures
└── README.md
Architecture
Repository Pattern
The implementation uses a generic repository pattern with built-in transaction support:
class MyRepository(BaseRepository[MyModel]):
def find_by_name(self, name: str) -> Optional[MyModel]:
return self.find_by_attributes({"name": name})
Transaction Management
Transactions are automatically handled using metaclasses:
@transactional
def transfer_funds(from_account: Account, to_account: Account, amount: float):
from_account.balance -= amount
to_account.balance += amount
account_repo.update(from_account)
account_repo.update(to_account)
MongoDB Configuration
The system supports flexible MongoDB configuration through environment variables:
MONGO_HOST: MongoDB host (default: localhost)MONGO_PORT: MongoDB port (default: 27017)MONGO_DATABASE: Database name (default: test_database)MONGO_REPLICA_SET: Replica set name (default: rs0)
Development
Running Tests
pytest tests/
MongoDB Management
Start MongoDB cluster:
cd deployment/docker
docker-compose up -d
Stop MongoDB cluster:
cd deployment/docker
docker-compose down
View logs:
docker-compose logs -f
Error Handling
The implementation includes comprehensive error handling:
- Connection failures
- Transaction conflicts
- Replica set issues
- Network timeouts
All errors are properly logged with detailed information for debugging.
Best Practices
- Always use the repository pattern for database operations
- Let the transaction decorator handle transaction management
- Use type hints for better code safety
- Follow the provided error handling patterns
- Use the logging system for debugging
Contributing
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
License
MIT License
Support
For issues and feature requests, please use the GitHub issue tracker.
Metadata
Release files for repository-mongodb 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| repository_mongodb-0.1.8.tar.gz | 9.9 kB | Without provenance | Details for repository_mongodb-0.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| repository_mongodb-0.1.8-py3-none-any.whl | Python 3 | none | any | Details for repository_mongodb-0.1.8-py3-none-any.whl |
Total release size: 21.4 kB
Release files / repository_mongodb-0.1.8.tar.gz
| Download URL | repository_mongodb-0.1.8.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a5d2117a66b68be4cd29238fc11e27d5a2b10e28de13cb9088dcaaffcad46506
|
|
BLAKE2b-256 checksum How to use checksums |
c3f76b3f14bb121918ff64330d7cd11baaedbe57517f85774decb7ccb06a8650
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.18
|
Release files / repository_mongodb-0.1.8-py3-none-any.whl
| Download URL | repository_mongodb-0.1.8-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c582382f97a13cfd432d4aba8f88953585accf398abaf3132c41f5bd807704d
|
|
BLAKE2b-256 checksum How to use checksums |
254a9c0dc598b5c0ed7035c277d573b05bca46849ccdd41cd043d7f0d8e3d548
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.18
|
Security for repository-mongodb 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.