repository-mongodb


Namerepository-mongodb JSON
Version 0.1.8 PyPI version JSON
download
home_pagehttps://github.com/ryan-zheng-teki/repository_mongodb
SummaryA small library to simplify MongoDB usage with repository pattern
upload_time2024-11-06 05:56:05
maintainerNone
docs_urlNone
authorRyan Zheng
requires_python>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements pymongo pytest pytest-asyncio
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

1. Clone the repository:
```bash
git clone https://github.com/yourusername/repository_mongodb.git
cd repository_mongodb
```

2. Create and activate a virtual environment:
```bash
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

3. Install dependencies:
```bash
pip install -r requirements.txt
```

4. Setup MongoDB Replica Set:
```bash
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:

```python
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:

```python
@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

```bash
pytest tests/
```

### MongoDB Management

Start MongoDB cluster:
```bash
cd deployment/docker
docker-compose up -d
```

Stop MongoDB cluster:
```bash
cd deployment/docker
docker-compose down
```

View logs:
```bash
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

1. Always use the repository pattern for database operations
2. Let the transaction decorator handle transaction management
3. Use type hints for better code safety
4. Follow the provided error handling patterns
5. Use the logging system for debugging

## Contributing

1. Fork the repository
2. Create your feature branch
3. Commit your changes
4. Push to the branch
5. Create a new Pull Request

## License

MIT License

## Support

For issues and feature requests, please use the GitHub issue tracker.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ryan-zheng-teki/repository_mongodb",
    "name": "repository-mongodb",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Ryan Zheng",
    "author_email": "ryan.zheng.work@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c3/f7/6b3f14bb121918ff64330d7cd11baaedbe57517f85774decb7ccb06a8650/repository_mongodb-0.1.8.tar.gz",
    "platform": null,
    "description": "# Repository MongoDB\n\nA robust MongoDB repository pattern implementation featuring automated transaction management, type-safe operations, and comprehensive replica set support.\n\n## Features\n\n- **Type-Safe Repository Pattern**: Generic repository implementation with strong typing support\n- **Automated Transaction Management**: Declarative transaction handling using metaclasses\n- **Replica Set Support**: Built-in support for MongoDB replica sets with automated setup\n- **Robust Error Handling**: Comprehensive error handling and logging throughout the stack\n- **Clean Architecture**: Follows SOLID principles and clean architecture patterns\n\n## Prerequisites\n\n- Docker and Docker Compose\n- Python 3.10 or higher\n- pip (Python package manager)\n\n## Quick Start\n\n1. Clone the repository:\n```bash\ngit clone https://github.com/yourusername/repository_mongodb.git\ncd repository_mongodb\n```\n\n2. Create and activate a virtual environment:\n```bash\npython -m venv venv\nsource venv/bin/activate  # On Windows: venv\\Scripts\\activate\n```\n\n3. Install dependencies:\n```bash\npip install -r requirements.txt\n```\n\n4. Setup MongoDB Replica Set:\n```bash\nchmod +x deployment/scripts/setup-mongodb.sh\nsudo ./deployment/scripts/setup-mongodb.sh\n```\n\n## Project Structure\n\n```\nrepository_mongodb/\n\u251c\u2500\u2500 repository_mongodb/\n\u2502   \u251c\u2500\u2500 base_model.py          # Base model with MongoDB integration\n\u2502   \u251c\u2500\u2500 base_repository.py     # Generic repository implementation\n\u2502   \u251c\u2500\u2500 mongo_client.py        # MongoDB client configuration\n\u2502   \u251c\u2500\u2500 mongo_config.py        # MongoDB connection settings\n\u2502   \u251c\u2500\u2500 transaction_management.py  # Transaction context managers\n\u2502   \u2514\u2500\u2500 transaction_metaclass.py   # Automated transaction handling\n\u251c\u2500\u2500 deployment/\n\u2502   \u251c\u2500\u2500 docker/\n\u2502   \u2502   \u251c\u2500\u2500 docker-compose.yml    # MongoDB replica set configuration\n\u2502   \u2502   \u2514\u2500\u2500 mongodb/              # MongoDB data directory\n\u2502   \u2514\u2500\u2500 scripts/\n\u2502       \u2514\u2500\u2500 setup-mongodb.sh      # Automated setup script\n\u251c\u2500\u2500 tests/\n\u2502   \u251c\u2500\u2500 test_base_repository.py   # Repository tests\n\u2502   \u2514\u2500\u2500 conftest.py              # Pytest fixtures\n\u2514\u2500\u2500 README.md\n```\n\n## Architecture\n\n### Repository Pattern\n\nThe implementation uses a generic repository pattern with built-in transaction support:\n\n```python\nclass MyRepository(BaseRepository[MyModel]):\n    def find_by_name(self, name: str) -> Optional[MyModel]:\n        return self.find_by_attributes({\"name\": name})\n```\n\n### Transaction Management\n\nTransactions are automatically handled using metaclasses:\n\n```python\n@transactional\ndef transfer_funds(from_account: Account, to_account: Account, amount: float):\n    from_account.balance -= amount\n    to_account.balance += amount\n    account_repo.update(from_account)\n    account_repo.update(to_account)\n```\n\n### MongoDB Configuration\n\nThe system supports flexible MongoDB configuration through environment variables:\n\n- `MONGO_HOST`: MongoDB host (default: localhost)\n- `MONGO_PORT`: MongoDB port (default: 27017)\n- `MONGO_DATABASE`: Database name (default: test_database)\n- `MONGO_REPLICA_SET`: Replica set name (default: rs0)\n\n## Development\n\n### Running Tests\n\n```bash\npytest tests/\n```\n\n### MongoDB Management\n\nStart MongoDB cluster:\n```bash\ncd deployment/docker\ndocker-compose up -d\n```\n\nStop MongoDB cluster:\n```bash\ncd deployment/docker\ndocker-compose down\n```\n\nView logs:\n```bash\ndocker-compose logs -f\n```\n\n## Error Handling\n\nThe implementation includes comprehensive error handling:\n\n- Connection failures\n- Transaction conflicts\n- Replica set issues\n- Network timeouts\n\nAll errors are properly logged with detailed information for debugging.\n\n## Best Practices\n\n1. Always use the repository pattern for database operations\n2. Let the transaction decorator handle transaction management\n3. Use type hints for better code safety\n4. Follow the provided error handling patterns\n5. Use the logging system for debugging\n\n## Contributing\n\n1. Fork the repository\n2. Create your feature branch\n3. Commit your changes\n4. Push to the branch\n5. Create a new Pull Request\n\n## License\n\nMIT License\n\n## Support\n\nFor issues and feature requests, please use the GitHub issue tracker.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A small library to simplify MongoDB usage with repository pattern",
    "version": "0.1.8",
    "project_urls": {
        "Homepage": "https://github.com/ryan-zheng-teki/repository_mongodb"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "254a9c0dc598b5c0ed7035c277d573b05bca46849ccdd41cd043d7f0d8e3d548",
                "md5": "8a27ddb848efb84466f34547bd1bc925",
                "sha256": "6c582382f97a13cfd432d4aba8f88953585accf398abaf3132c41f5bd807704d"
            },
            "downloads": -1,
            "filename": "repository_mongodb-0.1.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8a27ddb848efb84466f34547bd1bc925",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 11492,
            "upload_time": "2024-11-06T05:56:04",
            "upload_time_iso_8601": "2024-11-06T05:56:04.067552Z",
            "url": "https://files.pythonhosted.org/packages/25/4a/9c0dc598b5c0ed7035c277d573b05bca46849ccdd41cd043d7f0d8e3d548/repository_mongodb-0.1.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3f76b3f14bb121918ff64330d7cd11baaedbe57517f85774decb7ccb06a8650",
                "md5": "e787990bca6af9c7b018f70aeafc7aa3",
                "sha256": "a5d2117a66b68be4cd29238fc11e27d5a2b10e28de13cb9088dcaaffcad46506"
            },
            "downloads": -1,
            "filename": "repository_mongodb-0.1.8.tar.gz",
            "has_sig": false,
            "md5_digest": "e787990bca6af9c7b018f70aeafc7aa3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 9941,
            "upload_time": "2024-11-06T05:56:05",
            "upload_time_iso_8601": "2024-11-06T05:56:05.747495Z",
            "url": "https://files.pythonhosted.org/packages/c3/f7/6b3f14bb121918ff64330d7cd11baaedbe57517f85774decb7ccb06a8650/repository_mongodb-0.1.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-06 05:56:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ryan-zheng-teki",
    "github_project": "repository_mongodb",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pymongo",
            "specs": [
                [
                    ">=",
                    "4.8.0"
                ]
            ]
        },
        {
            "name": "pytest",
            "specs": [
                [
                    ">=",
                    "8.2.2"
                ]
            ]
        },
        {
            "name": "pytest-asyncio",
            "specs": [
                [
                    ">=",
                    "0.23.7"
                ]
            ]
        }
    ],
    "lcname": "repository-mongodb"
}
        
Elapsed time: 1.13468s