ORM working with MongoDb and using Pydantic as a model.
Project description
Pydantic Mongo ORM
Pydantic Mongo ORM is a library that allows users to map Pydantic models to MongoDb.
Installation
pip install pydantic_mongo_orm
Usage
Models
PMO uses Pydantic models to represent data in database. To start using it, you need to create new model based on
pydantic_mongo_orm.BaseModel
. This class implements all the features from Pydantic but also adds some
orm-specific functionality, e.g. adds read-only field id
that represents document-id (_id
) from MongoDb:
from pydantic_mongo_orm import BaseModel
class MyModel(BaseModel):
greeting: str
my_model = MyModel(greeting='Hello World!')
print(my_model.greeting) # >> 'Hello World!'
print(my_model.id) # >> uuid.UUID4('2daaa0ac-501b-4018-9101-cc822a37c1a1')
You may also specify mongo_config
for each model class that further modifies ORM behaviour:
from pydantic_mongo_orm import BaseModel, MongoConfigDict
class MyModel(BaseModel):
greeting: str
mongo_config = MongoConfigDict(collection='my_model')
Available options are:
collection
: Name of the collection where the model will be stored. By default, it's detected from the class name.objects_class
: Class that will be used as an object storage for given model. By default,ObjectsStorage
is used. Explained later
Storage
The base of PMO is Storage
- class connected to MongoDb able to map models to objects and vice-versa.
Connect to MongoDb and save new instance of MyModel from previous example:
from pydantic_mongo_orm import Storage
storage = Storage(host='mongodb://127.0.0.1:27017', db_name='local')
storage.connect()
my_model = MyModel(greeting='Hello World!')
storage.save(my_model)
my_models = storage.find(MyModel, {'greeting': 'Hello World!'})
print(list(my_models)) # [MyModel greetings: 'Hello World!']
Bound objects
You may use Storage
object directly but PMO offers a shorthand that simplifies working with the ORM. Each
model class provides attribute objects
that provides direct access to collection storing that particular model.
To enable this feature, you must first bind the Storage
to the BaseModel
using .bind()
. This example is
equivalent with the previous one:
from pydantic_mongo_orm import Storage
storage = Storage(host='mongodb://127.0.0.1:27017', db_name='local')
storage.connect()
storage.bind()
my_model = MyModel(greeting='Hello World!')
my_model.save()
my_models = MyModel.objects.find({'greeting': 'Hello World!'})
print(list(my_models)) # [MyModel greetings: 'Hello World!']
By default, the objects
attribute contains an instance of pydantic_mongo_orm.ObjectsStorage
. It provides the
same functionality as pydantic_mongo_orm.Storage
. If you wish to customize behaviour of certain model - change
how it's stored, add custom filters etc., you may extend the ObjectsStorage
class, add new functionality and
then configure it to be used for your models:
from pydantic_mongo_orm import BaseModel, ObjectsStorage, Storage, MongoConfigDict
class RectangleStorage(ObjectsStorage):
def squares(self):
return self.find({'$where': 'this.x == this.y'})
class Rectangle(BaseModel):
mongo_config = MongoConfigDict(objects_class=RectangleStorage)
length: int
width: int
storage = Storage(host='mongodb://127.0.0.1:27017', db_name='local')
storage.connect()
storage.bind()
my_square = Rectangle(x=5, y=5)
my_square.save()
my_rectangle = Rectangle(x=5, y=10)
my_rectangle.save()
print(list(Rectangle.objects.squares())) # >> [Rectangle x=5 y=5]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic_mongo_orm-0.0.0.tar.gz
.
File metadata
- Download URL: pydantic_mongo_orm-0.0.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a236cca7d6f4d9451d7936310fd58b63bbf5167be25619b706173e1ca551d5c6 |
|
MD5 | fc392238bc7ad57ac9b85b7d066524ce |
|
BLAKE2b-256 | 3e13d4c986c16a241c58d26e029a496f5dff9e24f6ac0f3843e9c114cabc3f4e |
File details
Details for the file pydantic_mongo_orm-0.0.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_mongo_orm-0.0.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ed8287acdb67f4f972ae39e735d2b6a2bb1d8b6f70c0f9767d586ea2a2ca6f1 |
|
MD5 | cd0cc2c67d7bde251e2ac723d84ba0d3 |
|
BLAKE2b-256 | 2f93bb42d3c99bec82878089ba67bf5a0505aea51931b3d7d7f01c33673c54da |