# MongoPyORM
MongoPyORM is a lightweight Python Object-Relational Mapper (ORM)-like implementation for MongoDB. It provides a familiar and simple interface for defining and interacting with MongoDB documents using Python classes. This library is designed to help developers manage MongoDB data in an object-oriented way, similar to how traditional ORMs work with SQL databases.
## Features
- Define document models using Python classes.
- Built-in support for common field types such as:
- `CharField`, `IntegerField`, `FloatField`, `BooleanField`, `ListField`, `JSONField`, `UUIDField`, `DateField`, `DateTimeField`.
- Simple CRUD operations with `MongoManager`.
- Field validation with custom data types.
- Supports relationships with embedded or referenced documents.
## Installation
To install MongoPyORM, use pip:
```bash
pip install MongoPyORM
```
## Quick Start
### 1. Configuration Setup
To configure your Django project for MongoDB:
1. **Update the `__init__.py` File**
In your Django project directory, locate the `__init__.py` file and add the following snippet to configure your database:
```python
from <project_name> import settings
from mongo_client.client import MongoDBConfig
DATABASE_NAME = settings.DATABASE_NAME
DATABASE_USERNAME = settings.DATABASE_USERNAME
CLUSTER_NAME = settings.CLUSTER_NAME
DATABASE_PASSWORD = settings.DATABASE_PASSWORD
config = MongoDBConfig()
config.set_config(
db_name=DATABASE_NAME,
username=DATABASE_USERNAME,
password=DATABASE_PASSWORD,
cluster_name=CLUSTER_NAME
)
```
This snippet initializes the MongoDB configuration using the settings defined in your `settings.py`.
### 2. Comment Out the Relational Database Configuration
In the `settings.py` file of your Django project, comment out the default relational database configuration, as it won't be used with MongoDB:
```python
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
```
### 3. Defining Models with MongoPyORM
Once your project is configured, you can start creating models using the package's custom fields. Here's an example of defining a model:
- **Create a Model File**
In your Django app, create a new file, such as `models.py`.
- **Import the Required Classes**
Import the field types and base model class from the `orm.mongo_orm` module:
```python
from orm.mongo_orm import CharField, FloatField, MongoModel, UUIDField
```
- **Define Your Model**
Create a Python class that inherits from `MongoModel` and define your fields:
```python
from mongopyorm import MongoModel, CharField, IntegerField, BooleanField
class User(MongoModel):
username = CharField(max_length=150, required=True)
email = CharField(max_length=255)
age = IntegerField(default=0)
is_active = BooleanField(default=True)
class Meta:
collection_name = "users"
```
- **Initialize the Manager**
Call `_initialize_manager()` on the model to set up the manager for database operations:
```python
User._initialize_manager()
```
### 4. Using the Model
- **Create a New User:**
```python
user = User(username="john_doe", email="john@example.com", age=25)
user.save() # Save to the database
```
- **Query Users:**
- **Get all users:**
```python
users = User.objects.all()
```
- **Filter users:**
```python
active_users = User.objects.filter(is_active=True)
```
- **Get a single user:**
```python
john = User.objects.get(username="john_doe")
```
- **Update a User:**
```python
john.age = 26
john.save() # Updates the existing document
```
- **Delete a User:**
```python
john.delete()
```
## Field Types
The following field types are supported:
- `CharField`: For storing strings with optional `max_length`.
- `IntegerField`: For storing integers.
- `FloatField`: For storing floating-point numbers.
- `BooleanField`: For storing boolean values.
- `ListField`: For storing lists.
- `JSONField`: For storing JSON-compatible data (list or dict).
- `UUIDField`: For storing UUID values.
- `DateField`: For storing dates.
- `DateTimeField`: For storing datetime values.
### Customization
Each field has the following optional attributes:
- `required`: Whether the field is mandatory.
- `default`: A default value for the field if none is provided.
- `blank`: Whether the field can be left blank.
## Configuration
MongoPyORM uses MongoDB credentials (e.g., username, password, database name, and cluster name) for establishing the connection. You can configure your credentials by creating a `MongoDBConfig` class, as demonstrated below:
```python
from mongo_client.client import MongoDBConfig
# Set up MongoDB credentials globally
config = MongoDBConfig()
config.set_config(db_name="your_db", username="your_username", password="your_password", cluster_name="your_cluster")
# Now you can use MongoPyORM models with these credentials
```
Once credentials are configured, models will automatically use the provided settings for MongoDB interactions.
## Contributing
Contributions are welcome! Please follow the standard GitHub workflow (fork, feature branch, pull request workflow):
- Fork the repository.
- Create a feature branch (`git checkout -b feature/your-feature`).
- Commit your changes (`git commit -m 'Add your feature'`).
- Push to the branch (`git push origin feature/your-feature`).
- Open a pull request.
## License
This project is licensed under the MIT License.
## Future Enhancements
- Support for relationship fields (e.g., ForeignKey-like references).
- Query optimizations for large datasets.
- More advanced data validation.
## Issues
If you encounter any bugs or issues, feel free to open an issue on GitHub.
## Acknowledgments
This project was inspired by the simplicity of traditional ORMs and the power of MongoDB, aiming to bring them together seamlessly.
## Contact
For any inquiries or support, please contact [brannstrom9911@gmail.com].
Raw data
{
"_id": null,
"home_page": null,
"name": "MongoPyORM",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "mongodb, ORM, database, MongoDB ORM, Django, Django ORM",
"author": "Ambar Rizvi",
"author_email": "<brannstrom9911@gmail.com>",
"download_url": null,
"platform": null,
"description": "# MongoPyORM\n\nMongoPyORM is a lightweight Python Object-Relational Mapper (ORM)-like implementation for MongoDB. It provides a familiar and simple interface for defining and interacting with MongoDB documents using Python classes. This library is designed to help developers manage MongoDB data in an object-oriented way, similar to how traditional ORMs work with SQL databases.\n\n## Features\n\n- Define document models using Python classes.\n- Built-in support for common field types such as:\n - `CharField`, `IntegerField`, `FloatField`, `BooleanField`, `ListField`, `JSONField`, `UUIDField`, `DateField`, `DateTimeField`.\n- Simple CRUD operations with `MongoManager`.\n- Field validation with custom data types.\n- Supports relationships with embedded or referenced documents.\n\n## Installation\n\nTo install MongoPyORM, use pip:\n\n```bash\npip install MongoPyORM\n```\n\n## Quick Start\n\n### 1. Configuration Setup\n\nTo configure your Django project for MongoDB:\n\n1. **Update the `__init__.py` File** \n In your Django project directory, locate the `__init__.py` file and add the following snippet to configure your database:\n\n ```python\n from <project_name> import settings\n from mongo_client.client import MongoDBConfig\n\n DATABASE_NAME = settings.DATABASE_NAME\n DATABASE_USERNAME = settings.DATABASE_USERNAME\n CLUSTER_NAME = settings.CLUSTER_NAME\n DATABASE_PASSWORD = settings.DATABASE_PASSWORD\n\n config = MongoDBConfig()\n config.set_config(\n db_name=DATABASE_NAME, \n username=DATABASE_USERNAME, \n password=DATABASE_PASSWORD, \n cluster_name=CLUSTER_NAME\n )\n ```\n\nThis snippet initializes the MongoDB configuration using the settings defined in your `settings.py`.\n\n### 2. Comment Out the Relational Database Configuration\n\nIn the `settings.py` file of your Django project, comment out the default relational database configuration, as it won't be used with MongoDB:\n\n```python\n# DATABASES = {\n# 'default': {\n# 'ENGINE': 'django.db.backends.sqlite3',\n# 'NAME': BASE_DIR / 'db.sqlite3',\n# }\n# }\n```\n\n### 3. Defining Models with MongoPyORM\n\nOnce your project is configured, you can start creating models using the package's custom fields. Here's an example of defining a model:\n\n- **Create a Model File** \n In your Django app, create a new file, such as `models.py`.\n\n- **Import the Required Classes** \n Import the field types and base model class from the `orm.mongo_orm` module:\n\n ```python\n from orm.mongo_orm import CharField, FloatField, MongoModel, UUIDField\n ```\n\n- **Define Your Model** \n Create a Python class that inherits from `MongoModel` and define your fields:\n\n ```python\n from mongopyorm import MongoModel, CharField, IntegerField, BooleanField\n\n class User(MongoModel):\n username = CharField(max_length=150, required=True)\n email = CharField(max_length=255)\n age = IntegerField(default=0)\n is_active = BooleanField(default=True)\n\n class Meta:\n collection_name = \"users\"\n ```\n\n- **Initialize the Manager** \n Call `_initialize_manager()` on the model to set up the manager for database operations:\n\n ```python\n User._initialize_manager()\n ```\n\n### 4. Using the Model\n\n- **Create a New User:**\n\n ```python\n user = User(username=\"john_doe\", email=\"john@example.com\", age=25)\n user.save() # Save to the database\n ```\n\n- **Query Users:**\n\n - **Get all users:**\n\n ```python\n users = User.objects.all()\n ```\n\n - **Filter users:**\n\n ```python\n active_users = User.objects.filter(is_active=True)\n ```\n\n - **Get a single user:**\n\n ```python\n john = User.objects.get(username=\"john_doe\")\n ```\n\n- **Update a User:**\n\n ```python\n john.age = 26\n john.save() # Updates the existing document\n ```\n\n- **Delete a User:**\n\n ```python\n john.delete()\n ```\n\n## Field Types\n\nThe following field types are supported:\n\n- `CharField`: For storing strings with optional `max_length`.\n- `IntegerField`: For storing integers.\n- `FloatField`: For storing floating-point numbers.\n- `BooleanField`: For storing boolean values.\n- `ListField`: For storing lists.\n- `JSONField`: For storing JSON-compatible data (list or dict).\n- `UUIDField`: For storing UUID values.\n- `DateField`: For storing dates.\n- `DateTimeField`: For storing datetime values.\n\n### Customization\n\nEach field has the following optional attributes:\n\n- `required`: Whether the field is mandatory.\n- `default`: A default value for the field if none is provided.\n- `blank`: Whether the field can be left blank.\n\n## Configuration\n\nMongoPyORM uses MongoDB credentials (e.g., username, password, database name, and cluster name) for establishing the connection. You can configure your credentials by creating a `MongoDBConfig` class, as demonstrated below:\n\n```python\nfrom mongo_client.client import MongoDBConfig\n\n# Set up MongoDB credentials globally\nconfig = MongoDBConfig()\nconfig.set_config(db_name=\"your_db\", username=\"your_username\", password=\"your_password\", cluster_name=\"your_cluster\")\n\n# Now you can use MongoPyORM models with these credentials\n```\n\nOnce credentials are configured, models will automatically use the provided settings for MongoDB interactions.\n\n## Contributing\n\nContributions are welcome! Please follow the standard GitHub workflow (fork, feature branch, pull request workflow):\n\n- Fork the repository.\n- Create a feature branch (`git checkout -b feature/your-feature`).\n- Commit your changes (`git commit -m 'Add your feature'`).\n- Push to the branch (`git push origin feature/your-feature`).\n- Open a pull request.\n\n## License\n\nThis project is licensed under the MIT License.\n\n## Future Enhancements\n\n- Support for relationship fields (e.g., ForeignKey-like references).\n- Query optimizations for large datasets.\n- More advanced data validation.\n\n## Issues\n\nIf you encounter any bugs or issues, feel free to open an issue on GitHub.\n\n## Acknowledgments\n\nThis project was inspired by the simplicity of traditional ORMs and the power of MongoDB, aiming to bring them together seamlessly.\n\n## Contact\n\nFor any inquiries or support, please contact [brannstrom9911@gmail.com].\n",
"bugtrack_url": null,
"license": null,
"summary": "A lightweight Python Object-Relational Mapper (ORM)-like implementation for MongoDB",
"version": "0.2.6",
"project_urls": null,
"split_keywords": [
"mongodb",
" orm",
" database",
" mongodb orm",
" django",
" django orm"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "eff5735c4865d63acf9fe4020aa62acc42673aca3b2fabf14ef5d8abd558ba7d",
"md5": "ea0ed24c4f03cf6d18013c655fe41020",
"sha256": "13ee0e81e8a968e5067f287abfb2c245c36df3b9951b47e3ae31a2d1213281be"
},
"downloads": -1,
"filename": "MongoPyORM-0.2.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ea0ed24c4f03cf6d18013c655fe41020",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 9253,
"upload_time": "2024-12-04T19:35:36",
"upload_time_iso_8601": "2024-12-04T19:35:36.474651Z",
"url": "https://files.pythonhosted.org/packages/ef/f5/735c4865d63acf9fe4020aa62acc42673aca3b2fabf14ef5d8abd558ba7d/MongoPyORM-0.2.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-04 19:35:36",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "mongopyorm"
}