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)
2. Connect with MongoDB and link imongo
from pymongo import MongoClient
def connect_db(db_name: str = "helloworld") -> MongoClient:
client = MongoClient("mongodb://root:example@localhost:27017")
# register schemas
User.register_client(client, db_name)
return client
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)
Built Distribution
Close
Hashes for imongo_orm-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3787b514f5aacb17444f91cfd193026718ebc449aa3bc0ff67d29a7802f0cb8a |
|
MD5 | a7984c49543a9f5a68ba5f863c8edb9f |
|
BLAKE2b-256 | 20ec7e1dd4f277684392f2bf7748c94ff203ae15ed87041de9a7c9c80c02e906 |