A MongoDB Python ORM, built on Pydantic and PyMongo.
Project description
Mongomantic = Pymongo + Pydantic
Mongomantic is a lightweight python MongoDB ORM based on Pydantic and PyMongo, heavily inspired by Mongoengine.
API
from mongomantic import BaseRepository, MongoDBModel
class User(MongoDBModel):
first_name: str
last_name: str
class UserRepository(BaseRepository):
class Meta: # Required internal class
model = User # Define model type
collection = "user" # Define collection name
user = User(first_name="John", last_name="Smith")
user = UserRepository.save(user) # PyMongo wrapping classmethods
user.id # ObjectId that was saved
Usage
pip install mongomantic
To connect to your database, a connect function similar to mongoengine is provided.
Your Opinion is Needed
Mongomantic can be kept as a simple wrapper around PyMongo, or developed into a miniature version of Mongoengine that's built on Pydantic. The first direction would result in the following API:
# Direct pymongo wrapper
users = UserRepository.find({"$and": [{"age": {"$gt": 12}}, {"name": "John"}]})
# But matches can be done as keyword arguments
john = UserRepository.find(name="John")
On the other hand, a more complex version of Mongomantic could lead to:
# More Pythonic way of writing queries
users = UserRepository.find(User.age > 12, name="John")
# Matches still compact
john = UserRepository.find(name="John")
Please submit your vote below.
Simple PyMongo Wrapper - Prefer speed and native mongodb filters More Complex Wrapper - Pythonic Filters
🚀 TODO
- Documentation
- Basic API similar to mongoengine, without any queryset logic
- Built on Pydantic models, no other schema required
- BaseRepository responsible for all operations (instead of the model itself)
- ProductionRepository derived from BaseRepository with all errors handled
- Repository/model plugin framework (ex. SyncablePlugin, TimestampedPlugin, etc.)
- Wrapper for aggregation pipelines
- Mongomock tests
- Flexible connect() function wrapper around PyMongo client (aliases, replica sets, retry writes, etc.)
- Clean up imports and expose essentials in main file
🛡 License
This project is licensed under the terms of the MIT
license. See LICENSE for more details.
📃 Citation
@misc{mongomantic,
author = {mongomantic},
title = {A MongoDB Python ORM, built on Pydantic and PyMongo.},
year = {2021},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/RamiAwar/mongomantic}}
}
Credits
This project was generated with python-package-template
.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mongomantic-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bf2cf83c6c3122779923423654d7511383113cbd38c00442a1e66f6daeb4b20 |
|
MD5 | 3fab0062a96b35b353fd7493ebe1a83c |
|
BLAKE2b-256 | 081cb4f6c3732d7aaeb97dfa4d55158c0a2e64922190acf75837e35a4cb4ff5b |