Pydantic Async Mongo Document
Project description
Pydantic Mongo Document
pydantic_mongo_document
is a Python library that provides a base class for creating MongoDB documents using Pydantic models.
Installation
Install the package using pip or poetry.
Using pip
pip install pydantic_mongo_document
Using poetry
poetry add pydantic_mongo_document
Usage To use pydantic_mongo_document, you need to create a Pydantic model that represents your MongoDB document and inherit from the MongoDocument class provided by the library. Here's an example:
from pydantic_mongo_document import Document
class User(Document):
__collection__ = "users"
__database__ = "production"
name: str
email: str
In this example, we define a User Pydantic Document model with two fields (name and email) and
specifies the MongoDB collection name (users) and database name (production) using the __collection__
and __database__
class attributes.
from pydantic_mongo_document import Document
# Set the MongoDB replica configuration
Document.set_replica_config({"localhost": "mongodb://localhost:27017"})
class User(Document):
__replica__ = "localhost"
__collection__ = "users"
__database__ = "production"
name: str
email: str
async def create_user():
user = User(name="John", email="john@example.com")
await user.insert()
user = await User.one(add_query={"name": "John"})
print(user) # User(id=ObjectId("64fc59cf6410868c9a40644b"), name="John", email="john@example")
In this example, we created new User in database. We then used the User.one
method to retrieve the user from the database.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
MIT
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file pydantic_mongo_document-0.2.3.7.tar.gz
.
File metadata
- Download URL: pydantic_mongo_document-0.2.3.7.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.1 Darwin/24.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcce8eb05bfa46c1d365505f2fac7556ac1d3cc0fc980eba958348eeb717edf0 |
|
MD5 | 844d2f38148d0e1d544be397fa5d7246 |
|
BLAKE2b-256 | b81e0d7a388380a8889d4caa2e737346cbc6167210ae78133094ee4243771a03 |
File details
Details for the file pydantic_mongo_document-0.2.3.7-py3-none-any.whl
.
File metadata
- Download URL: pydantic_mongo_document-0.2.3.7-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.1 Darwin/24.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7419ea4a3cc7979465c6afa84423b803d92031a7d1f5ca73dac0ea36a8979a1f |
|
MD5 | 4d8eeb3fcd0fa80722e172c4be0c1bd7 |
|
BLAKE2b-256 | 5b6a7b72700f407c253e699f9e6ba7d5c3426d7205f757b95b447dbcfd5936c8 |