{"id":18818280,"url":"https://github.com/majidraimi/tymongo","last_synced_at":"2025-04-13T23:32:40.168Z","repository":{"id":210554423,"uuid":"726862701","full_name":"MajidRaimi/tymongo","owner":"MajidRaimi","description":"Interacting with MongoDB using Python has never been more straightforward and empowering! 💪","archived":false,"fork":false,"pushed_at":"2023-12-05T21:05:42.000Z","size":46,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T13:45:45.154Z","etag":null,"topics":["mongodb","pydantic","pymongo","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MajidRaimi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-12-03T16:13:39.000Z","updated_at":"2023-12-10T07:31:17.000Z","dependencies_parsed_at":"2023-12-03T16:39:47.770Z","dependency_job_id":null,"html_url":"https://github.com/MajidRaimi/tymongo","commit_stats":null,"previous_names":["majidraimi/tymongo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MajidRaimi%2Ftymongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MajidRaimi%2Ftymongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MajidRaimi%2Ftymongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MajidRaimi%2Ftymongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MajidRaimi","download_url":"https://codeload.github.com/MajidRaimi/tymongo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248796858,"owners_count":21163048,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongodb","pydantic","pymongo","python"],"created_at":"2024-11-08T00:15:56.791Z","updated_at":"2025-04-13T23:32:39.893Z","avatar_url":"https://github.com/MajidRaimi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003ccenter\u003e\n    \u003cimg src=\"https://i.imgur.com/DWyckJS.jpg\"/\u003e\n\u003c/center\u003e\n\n# TyMongo 🐍📦\n## PyMongo + Pydantic = TyMongo 🤩\nTyMongo is a Python package designed to simplify your interaction with MongoDB by leveraging the power of [pymongo](https://pymongo.readthedocs.io/) for database connectivity and [pydantic](https://pydantic-docs.helpmanual.io/) for efficient modeling and type creation through classes.\n\n## Features\n- Seamless connection to MongoDB using pymongo. 🌐\n- Data modeling and type creation with pydantic classes. 🧱\n- Automatic data validation and type conversion. ✅\n- Automatic data serialization and deserialization. 📦\n\n## Installation 📥\nTo install TyMongo, simply use pip:\n```bash\npip install tymongo\n```\n\n## Setup 🛠️\nAll you really need to do is to create `.env` file in your project root directory and add the following variables:\n```bash\nDATABASE_URL=your_database_url\nDATABASE_NAME=your_database_name\n```\nAnd now you're ready to go! 🚀\n\n## Usage 📝\n### Creating a Model\nTo create a model, simply inherit from `TyMongoModel` and define your fields:\n```python\nclass User(TyMongoModel):\n    name: str\n    age: int\n\n    class Config:\n        collection = \"users\"\n```\n- Will work 100% like `BaseModel` from pydantic, but with some extra features. 🤩\n- Make sure you define inner class `Config` and set `collection` to the name of the collection you want to use in your database. 📁\n- You don't have to define `_id` field, it will be automatically created for you. 🆔\n\n### Creating a User\n```python\nuser = User(name=\"Majid\", age=21)\n# or\nuser_dict = {\"name\": \"Majid\", \"age\": 21}\nuser = User(**user_dict)\n```\n- You can pass any value to any field, it will be automatically converted to the type you defined. 🔄\n- You can also pass a dictionary to the model, it will be automatically converted to the model. 🔄\n\n### Saving a User\n```python\nuser.save()\n```\nNow you have a new user in your database! 🎉\n\n### Editing a User\n```python\nuser.age = 22\nuser.save()\n```\nNow your user's age is 22 in your database! 🎉\n\n### Deleting a User\n```python\nuser.delete()\n```\nNow your user is deleted from your database! 🎉\n\n## Extra Features 🤩\n### Querying \u0026 Fields Validation\n```python\nquery = {\"age\": 22}\nusers = User.find(query)\n```\nJust like pymongo, you can pass a query to `find` method and it will return a list of all the users that match the query as a list of models, just make sure the dictionary keys match the model fields. 🔑\n\n```python\nquery = {\n    'x': 'some value',\n    'age': 21,\n}\nusers = User.find(query)\n```\nThis will raise an error because `x` is not a field in the model. ❌\n\n### Get a Single Document by ID\n```python\nuser = User.get(\"xxxxxxxxxxxxxxxxxxxxxxxx\")\n```\n\n### Get a Single Document by Query\n```python\nq = {\n    'name' : 'Majid'\n}\nuser = User.find_one(q)\n```\n\n### What if I want to use pymongo directly? 🤔\nYou can access the collection object directly from the model:\n```python\ncollection = User.__collection__()\nusers = collection.find({\"age\": 22})\n```\n\n## License\nTyMongo is licensed under the terms of the [MIT License](LICENSE).\n\n\n## Contributing 🤝\n- [Majid Saleh Al-Raimi](https://www.majidraimi.com/)\n\n## Contact 📧\n- [Email](mailto:majidsraimi@gmail.com)\n\n## Acknowledgements 🙏\n- [pymongo](https://pymongo.readthedocs.io/)\n- [pydantic](https://pydantic-docs.helpmanual.io/)\n\n\nThank you for using TyMongo! 🙌 happy coding! 🚀\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmajidraimi%2Ftymongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmajidraimi%2Ftymongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmajidraimi%2Ftymongo/lists"}