๐Ÿ“ฆ aminalaee / mongox

Familiar async Python MongoDB ODM

โ˜… 123 stars โ‘‚ 6 forks ๐Ÿ‘ 123 watching โš–๏ธ MIT License
asgiasynciomongodbmotorpydanticpythonstarlette
๐Ÿ“ฅ Clone https://github.com/aminalaee/mongox.git
HTTPS git clone https://github.com/aminalaee/mongox.git
SSH git clone git@github.com:aminalaee/mongox.git
CLI gh repo clone aminalaee/mongox
Amin Alaee Amin Alaee Version 0.1.2 (#91) 2b1786c 3 years ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ docs
๐Ÿ“ examples
๐Ÿ“ mongox
๐Ÿ“ scripts
๐Ÿ“ tests
๐Ÿ“„ .codecov.yml
๐Ÿ“„ .flake8
๐Ÿ“„ .gitignore
๐Ÿ“„ CONTRIBUTING.md
๐Ÿ“„ LICENSE
๐Ÿ“„ mkdocs.yml
๐Ÿ“„ poetry.lock
๐Ÿ“„ pyproject.toml
๐Ÿ“„ README.md
๐Ÿ“„ README.md

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.