Skip to main content

Python Mongodb ODM using Motor and Pydantic

Project description

Build Status Publish Status Coverage Package version Supported Python versions


MongoX

MongoX is an async python ODM (Object Document Mapper) for MongoDB which is built on top of Motor and Pydantic.

The main features include:

  • Fully type annotated
  • Async support Python 3.7+ (since it's built on top of Motor)
  • Elegant editor support (since it's built on top of Pydantic)
  • Autocompletion everywhere, from object creation to query results
  • Custom query builder which is more intuitive and pythonic
  • 100% test coverage

MongoX models are at the same time Pydantic models and have the same functionalitties, so you can use them with your existing Pydantic models.


Documentation: https://aminalaee.github.io/mongox


Installation

$ pip install mongox

Quickstart

You can define mongox models the same way you define Pydantic models. The difference is they should inherit from mongox.Model now:

import asyncio

import mongox

client = mongox.Client("mongodb://localhost:27017")
db = client.get_database("test_db")


class Movie(mongox.Model, db=db, collection="movies"):
    name: str
    year: int

Now you can create some instances and insert them into the database:

movie = await Movie(name="Forrest Gump", year=1994).insert()

The returned result will be a Movie instance, and mypy will understand that this is a Movie instance. So you will have type hints and validations everywhere.

Now you can fetch some data from the database.

You can use the same pattern as PyMongo/Motor:

movie = await Movie.query({"name": "Forrest Gump"}).get()

Or you can use Movie fields instead of dictionaries in the query (less room for bugs):

movie = await Movie.query({Movie.name: "Forrest Gump"}).get()

And finally you can use a more intuitive query (limited yet):

movie = await Movie.query(Movie.name == "Forrest Gump").get()

Notice how we omitted the dictionary and passed the Movie fields in comparison.


Please refer to the documentation here or the full examples here.


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

mongox-0.1.2.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

mongox-0.1.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file mongox-0.1.2.tar.gz.

File metadata

  • Download URL: mongox-0.1.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1022-azure

File hashes

Hashes for mongox-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bcb0d43fb2bfcdffbf9eeb02f57d0e7fdf6769622f9aa0fdaa0d540cacac4114
MD5 237352547eaed1f98fdba4e50679a94d
BLAKE2b-256 993f770b7de49e7b733067d392d29f773e381e454622c761a0f96bb6917c6634

See more details on using hashes here.

File details

Details for the file mongox-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mongox-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1022-azure

File hashes

Hashes for mongox-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 89043e1c7c4aefa859d325ec4edb8917fc866e0f382ce1ee8770414adb3fe0ec
MD5 fe402e87f690931cee50f59a0fa81780
BLAKE2b-256 0572717d652f52d37aab47540facc02ac8093fe670b7f2ac4c7ff868a135e4d0

See more details on using hashes here.

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