Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aminalaee/mongox
Familiar async Python MongoDB ODM
https://github.com/aminalaee/mongox
asgi asyncio mongodb motor pydantic python starlette
Last synced: 5 days ago
JSON representation
Familiar async Python MongoDB ODM
- Host: GitHub
- URL: https://github.com/aminalaee/mongox
- Owner: aminalaee
- License: mit
- Created: 2021-11-12T09:49:07.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-01T09:00:21.000Z (almost 2 years ago)
- Last Synced: 2025-01-10T14:16:36.845Z (12 days ago)
- Topics: asgi, asyncio, mongodb, motor, pydantic, python, starlette
- Language: Python
- Homepage: https://aminalaee.github.io/mongox
- Size: 703 KB
- Stars: 122
- Watchers: 6
- Forks: 7
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-pydantic - Mongox - Familiar async MongoDB ODM based on Pydantic and Motor. (Object Mapping)
- awesome-pydantic - Mongox - Familiar async MongoDB ODM based on Pydantic and Motor. (Object Mapping)
README
---
# MongoX
MongoX is an async python ODM (Object Document Mapper) for MongoDB
which is built on top of [Motor][motor] and [Pydantic][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 coverageMongoX 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](https://aminalaee.github.io/mongox)
---
## Installation
```shell
$ 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:```python
import asyncioimport 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:
```python
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:
```python
movie = await Movie.query({"name": "Forrest Gump"}).get()
```Or you can use `Movie` fields instead of dictionaries in the query (less room for bugs):
```python
movie = await Movie.query({Movie.name: "Forrest Gump"}).get()
```And finally you can use a more intuitive query (limited yet):
```python
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](https://aminalaee.github.io/mongox) or the full examples [here](https://github.com/aminalaee/mongox/tree/main/examples).
---
[motor]: https://github.com/mongodb/motor
[pydantic]: https://github.com/samuelcolvin/pydantic