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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file imongo-orm-0.0.4.tar.gz.
File metadata
- Download URL: imongo-orm-0.0.4.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e59551559d461f807f3ed236afd0f49f5e09bfea4ae81f3b9c74bd350d7fe3da
|
|
| MD5 |
bfdf4b06de46e59fa05ca4d1070d0be7
|
|
| BLAKE2b-256 |
4f4691aae2449e35aa13e68c3facb45812d2637807eb890c6ac7728b8a52a158
|
File details
Details for the file imongo_orm-0.0.4-py3-none-any.whl.
File metadata
- Download URL: imongo_orm-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3787b514f5aacb17444f91cfd193026718ebc449aa3bc0ff67d29a7802f0cb8a
|
|
| MD5 |
a7984c49543a9f5a68ba5f863c8edb9f
|
|
| BLAKE2b-256 |
20ec7e1dd4f277684392f2bf7748c94ff203ae15ed87041de9a7c9c80c02e906
|