Skip to main content

A MongoDB Python ORM, built on Pydantic and PyMongo.

Project description

Logo





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

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

mongomantic-0.3.0.tar.gz (10.4 kB view hashes)

Uploaded Source

Built Distribution

mongomantic-0.3.0-py3-none-any.whl (9.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page