This is a minimal ODM for MongoDB. Build top of pymongo and pydantic.
Project description
ODM
Introduction
The purpose of this module is to do provide easy access to the database with the python object feature with MongoDB and pymongo. With pymongo that was very easy to make spelling mistakes of a collection name when you are doing database operation. This module provides you minimal ODM with a modeling feature so that you don’t have to look up the MongoDB dashboard(Mongo Compass) to know about field names or data types.
Requirement
Since this module is build top of pymongo and pydantic it's good have knowledge about those packages.
Example models
from datetime import datetime
from pymongo import ASCENDING, IndexModel
from pydantic import EmailStr, Field
from typing import Optional
from app.odm.models import Document
class User(Document):
email: EmailStr = Field(...)
name: Optional[str] = Field(default=None)
photo_link: str = Field(default=None)
password: Optional[str] = Field(default=None)
last_login: datetime = Field(default_factory=datetime.utcnow)
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
class Config:
collection_name = "user"
indexes = [
IndexModel([("email", ASCENDING)], unique=True),
]
Creation
To create an object and call create method. Create method accept one boolean optional argument get_obj
. If you want to pull full data and assign to the existing model then call obj.create(get_obj=True)
It will recall database with returnd id and update value of existing model.
user = User(
email="demo@example.com",
name="Example",
password="hash-password"
)
user.create()
Retrive Data
users = User.find()
for user in users:
print(user)
To retrive data there is a function call find. It's return python Iterator. Unless you accessing the value the function will not execute any DB call.
Paramiter of find.
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
Hashes for mongodb_odm-0.1a2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0c4bd8df510cb806a9ebcb9a69f18deb60477cf706a1e03b5586e24950bbf47 |
|
MD5 | 615fec133ca412fdafdbc5c41cb418c4 |
|
BLAKE2b-256 | e8fd8dac50436073a3fbd39bd19efd5fb626c925227cd9527fbd7c2fc1f61601 |