MongORM
is an ORM (object relational mapping) wrapper using async library motor for MongoDB
connection and pydantic for data definition and validation.
This module is a work in progress and API may change radically.
MongORM
uses pydantic
for data validation
and motor
for async MongoDB connection.
Using PIP:
pip install python-mongorm
Using poetry:
poetry add python-mongorm
from mongorm import MongORM
client = MongORM("mongodb://root:root@localhost:27017/", "database")
from mongorm import BaseModel, MongoIndex, MongoIndexType
class Book(BaseModel):
"""Define models the way you would define pydantic models"""
class Meta:
"""Meta contains the model's configuration and indexes"""
client = client # pass the client to the model's Meta
collection = "books"
title = MongoIndex("title", MongoIndexType.ASCENDING)
author = MongoIndex("author", MongoIndexType.ASCENDING)
# id field of type ObjectId is created automatically
title: str
author: str
year_published: int