A mongodb ODM dependant on pymongo with extensive model creation, validation, and relational model mechanism.
Project description
MongoAPI Documentation
This documentation provides an overview of the MongoAPI and Model classes, designed to simplify CRUD operations and schema enforcement when interacting with MongoDB. The classes provide a Pythonic interface for managing MongoDB connections, defining collections, and enforcing data validation.
Table of Contents
Introduction
The MongoAPI and Model classes provide an abstraction layer on top of PyMongo, enabling easier management of MongoDB collections and documents. These classes also enforce schema validation, ensuring that data inserted into the database adheres to predefined rules.
Installation
To use the MongoAPI and related classes, you need to install the following dependencies:
pip install mongodesu
MongoAPI Class
The MongoAPI class is a wrapper for CRUD operations and connection logic for MongoDB.
Methods
__init__(): Initializes the MongoAPI instance and establishes a connection to the MongoDB database.connect(): A class method for establishing a connection to the MongoDB database.connect_one(): An instance method for establishing a connection to the MongoDB database.
Model Class
The Model class is an abstraction over a MongoDB collection. It provides methods for defining the schema and interacting with the collection.
Attributes
connection: A reference to aMongoAPIinstance, used to establish the connection.collection_name: The name of the MongoDB collection. If not provided, it defaults to the pluralized class name.collection: A reference to the MongoDB collection.
Methods
__init__(): Initializes the Model instance and sets up the MongoDB collection.find(): Finds a list of documents from the collection.find_one(): Finds a single document based on the provided filter.insert_many(): Inserts multiple documents into the collection.insert_one(): Inserts a single document into the collection.update_one(): Updates a single document based on the provided filter.update_many(): Updates multiple documents based on the provided filter.delete_one(): Deletes a single document based on the provided filter.delete_many(): Deletes multiple documents based on the provided filter.aggregate(): Performs aggregation operations on the collection.save(): Saves the current instance to the MongoDB collection.construct_model_name(): Constructs the collection name based on the class name.
Field Classes
StringField
A field that stores string data.
Parameters:
size: The maximum size of the string.required: Whether the field is required.unique: Whether the field should be unique.index: Whether the field should be indexed.default: The default value of the field.
NumberField
A field that stores numeric data.
Parameters:
required: Whether the field is required.unique: Whether the field should be unique.index: Whether the field should be indexed.default: The default value of the field.
ListField
A field that stores a list of items.
Parameters:
required: Whether the field is required.item_type: The type of items in the list.default: The default value of the field.
DateField
A field that stores date or datetime data.
Parameters:
required: Whether the field is required.unique: Whether the field should be unique.index: Whether the field should be indexed.default: The default value of the field.
BooleanField
A field that stores boolean data.
Parameters:
required: Whether the field is required.unique: Whether the field should be unique.index: Whether the field should be indexed.default: The default value of the field.
ForeignField
A field that stores a reference to another model.
Parameters:
model: The model to which this field refers.parent_field: The field in the parent model to which this field refers.required: Whether the field is required.default: The default value of the field.existance_check: Whether to check the existence of the referenced document.
Examples
Connecting to MongoDB (Method I)
This will connect the db and all the operation will use this connection.
from mongodesu import MongoAPI
mongo_api = MongoAPI(uri="mongodb://localhost:27017", database="mydatabase")
Connecting to the database (Methond II)
This will connect to the default mongo instance. And whatever model you will create. All the operation will use this connection by default.
MongoAPI.connect(uri="mongodb://localhost:27017/python-db-test")
Connecting to the database (Method III)
This is the same as the above methods.
mongo = MongoAPI()
mongo.connect_one(uri="mongodb://localhost:27017/python-db-test")
Defining a Model
from mongodesu import Model
from mongodesu.fields import StringField, BooleanField, NumberField
class User(Model):
name = StringField(required=True)
age = NumberField(required=True)
email = StringField(required=True, unique=True)
is_active = BooleanField(default=True)
# Example usage
user = User(name="John Doe", age=30, email="john.doe@example.com")
user.save()
Inserting a document
user = User()
user.insert_one({"name":"John Doe", "age"28, "email":"john@example.com"})
Inserting a list of documents
user = User()
documents = [
{"name":"John Doe", "age"28, "email":"john@example.com"},
{"name":"Jack Doe", "age"28, "email":"jack@example.com"}
]
user.insert_many(documents)
Finding one Document
user = User()
result = user.find_one({"name": "John Doe"})
print(result)
Finding list of documents
user = User()
result = user.find({})
print(result)
Updating Documents
user = User()
user.update_one({"name": "John Doe"}, {"$set": {"age": 31}})
Deleting Documents
user = User()
user.delete_one({"name": "John Doe"})
Count Documents
user = User()
print(user.count_documents({"name": "John Doe"}))
Using ForeignField
class Post(Model):
title = StringField(required=True)
content = StringField(required=True)
author = ForeignField(model=User, required=True, existance_check=True)
post = Post(title="My First Post", content="Hello, world!", author="ObjectId_of_User")
post.save()
Creating multiple connection
This will create two connection and each model will be associated with the one connection and every operation will be perform for that connection over the connected database.
from mongodesu import MongoAPI
from mongodesu.fields import StringField, BooleanField, NumberField
mongo1 = MongoAPI(host="localhost", port=27017, database="flaskdb")
mongo2 = MongoAPI(uri="mongodb://localhost:27017/python-db-test")
class Todo(Model):
connection = mongo1
collection_name = 'todos'
# Fields description
task = StringField(required=True, index=True)
is_done = BooleanField(required=False)
class UserTodo(Model):
connection = mongo2
collection_name = 'user_todos'
# Field description
task = StringField(required=True, index=True)
is_done = BooleanField(required=True)
count_task = NumberField(required=False)
todo = Todo()
todo.insert_one({
"task": "Fetch the car",
"is_done": False
})
userTodo = UserTodo()
userTodo.insert_one({
"task": "Paint",
"is_done": False,
"count_task": 1
})
This documentation provides a comprehensive guide to using the MongoAPI, Model, and Field classes. The classes are designed to simplify interaction with MongoDB while enforcing data integrity through schema validation.
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 mongodesu-2.0.2.tar.gz.
File metadata
- Download URL: mongodesu-2.0.2.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2355abc1f28987542a5f4fb2f47b8bb05778ba4c4608a9ada86a86af87e5b9ca
|
|
| MD5 |
debdc7b5da878bac33d5f8f43eadc386
|
|
| BLAKE2b-256 |
82b94ec389dc866460e614f92e43f591b54a9d6cc82d45e6934aa6fb47e22b86
|
Provenance
The following attestation bundles were made for mongodesu-2.0.2.tar.gz:
Publisher:
pypi-publish.yml on AKA-Per/mongodesu
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mongodesu-2.0.2.tar.gz -
Subject digest:
2355abc1f28987542a5f4fb2f47b8bb05778ba4c4608a9ada86a86af87e5b9ca - Sigstore transparency entry: 506321178
- Sigstore integration time:
-
Permalink:
AKA-Per/mongodesu@4fed4f7a9d81a08a45d9cdaca2f65795ade64d67 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/AKA-Per
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@4fed4f7a9d81a08a45d9cdaca2f65795ade64d67 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mongodesu-2.0.2-py3-none-any.whl.
File metadata
- Download URL: mongodesu-2.0.2-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b2e476b178899d0527e2062b2ee84937de0bd1b58c7cc12a19571247c6f8211
|
|
| MD5 |
8104edd2e23e24c44d3fd89670b17741
|
|
| BLAKE2b-256 |
497c7904dd2a0cc001dbeec51956b5b61794504e0cd117ddec480d78cad4cb3f
|
Provenance
The following attestation bundles were made for mongodesu-2.0.2-py3-none-any.whl:
Publisher:
pypi-publish.yml on AKA-Per/mongodesu
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mongodesu-2.0.2-py3-none-any.whl -
Subject digest:
4b2e476b178899d0527e2062b2ee84937de0bd1b58c7cc12a19571247c6f8211 - Sigstore transparency entry: 506321207
- Sigstore integration time:
-
Permalink:
AKA-Per/mongodesu@4fed4f7a9d81a08a45d9cdaca2f65795ade64d67 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/AKA-Per
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@4fed4f7a9d81a08a45d9cdaca2f65795ade64d67 -
Trigger Event:
push
-
Statement type: