Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongodb-odm-0.1a2.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distribution

mongodb_odm-0.1a2-py3-none-any.whl (10.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page