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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file mongodb-odm-0.1a2.tar.gz.

File metadata

  • Download URL: mongodb-odm-0.1a2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.6

File hashes

Hashes for mongodb-odm-0.1a2.tar.gz
Algorithm Hash digest
SHA256 c1471aa6139e0e9f3a0856300110c3563eeaeee1b721c5aeb1caea491b32631f
MD5 404e2c07370f90aa2fdfade187e21282
BLAKE2b-256 b118598fa34d1b9da44ab8f7d576bc701d584d74c6d3ddddcdbc6e26423a2afe

See more details on using hashes here.

File details

Details for the file mongodb_odm-0.1a2-py3-none-any.whl.

File metadata

  • Download URL: mongodb_odm-0.1a2-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.6

File hashes

Hashes for mongodb_odm-0.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 b0c4bd8df510cb806a9ebcb9a69f18deb60477cf706a1e03b5586e24950bbf47
MD5 615fec133ca412fdafdbc5c41cb418c4
BLAKE2b-256 e8fd8dac50436073a3fbd39bd19efd5fb626c925227cd9527fbd7c2fc1f61601

See more details on using hashes here.

Supported by

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