Skip to main content

MongoDB ORM interface for Python

Project description

Schema-first mongoDB interface (inspired by Mongoose). Library serves as a thin wrapper around PyMongo.



Why use imongo?

  • Schema first API 😎

  • Built-In on-write validation for insertions and updates ✅

  • Last updated timestamp on every update for Slowly changing dimensions

  • Support for nested schemas 🪢



Getting Started

0. Install

pip install imongo-orm

1. Writing your first Model

from imongo import Key
from imongo import Model
from imongo import Schema
from imongo import Types

EMAIL_REGEX = """(?:[a-z...""" # trimmed because of space

user_schema = Schema(
    {
        "name": Key(type=Types.String, required=True, min_length=5, max_length=50, lowercase=True),
        "email": Key(type=Types.String, required=True, regex=EMAIL_REGEX),
        "role": Key(type=Types.String, required=True, default=None, enum=["A", "B", None]),
        "details": {
            "last_visit": Key(type=Types.Date, required=False),
            "age": Key(type=Types.String, required=False),
        }
    },
    timestamps=True,
    validate=True,
)

User = Model("users", user_schema)

3. Use imongo through pyMongo operations

connect_db("helloworld")

# Insert new user
new_user = User({"name": "Roberto", "email": "asd@google.com", "role": "A"})
res = User.insert_one(new_user)
new_user_id = res.inserted_id

# Fail to Update new user due to validation
res = User.update_one({"_id": new_user_id}, {"$set": {"name": "Rob"}})

# Find users
for user in User.find({"name": "roberto"}):
    print(user, "\n")

Validation Config for Key

required: bool = True                   #   Specify whether key is required
default: Union[Any, None] = None        #   Specify default value for key if value is not provided
enum: Union[List[Any], None] = None     #   Specify allowed values for key
immutable: bool = False                 #   Specify if value is immutable (i.e. it can't be altered after creation)
lowercase: bool = False                 #   Apply inline lowercase transformation (Types.String)
uppercase: bool = False                 #   Apply inline uppercase transformation (Types.String)
regex: str = None                       #   Regex match check against value (Types.String)
min: int = None                         #   Specify key's minimum value (Types.Integer, Types.Long, Types.Double)
max: int = None                         #   Specify key's maximum value (Types.Integer, Types.Long, Types.Double)
min_length: int = None                  #   Specify key's minimum length (Types.String)
max_length: int = None                  #   Specify key's minimum length (Types.String)

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

imongo-orm-0.0.4.tar.gz (8.3 kB view hashes)

Uploaded Source

Built Distribution

imongo_orm-0.0.4-py3-none-any.whl (8.0 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