Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_mongo_document-0.2.5.4.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file pydantic_mongo_document-0.2.5.4.tar.gz.

File metadata

File hashes

Hashes for pydantic_mongo_document-0.2.5.4.tar.gz
Algorithm Hash digest
SHA256 b50d63664e41f7b4fc84dd84b4107be9bdc7ba71af9f5ff16fcd7a6f9fc1d244
MD5 c0a6319fe9feab0a66744e368f910997
BLAKE2b-256 c0a3826e8e1cac7c262fe549cf2ac4bcb5fd570a0424584a8ce61acc42e6183d

See more details on using hashes here.

File details

Details for the file pydantic_mongo_document-0.2.5.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_mongo_document-0.2.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 24afc73f761b78a0c3de4303b11a9c891d44befd4f8fbdbd44bee0e24c0ad3d0
MD5 334c51bb3cba7d4d2f8bc028e568984c
BLAKE2b-256 626b5223c236148849c6d5f824fe121eb3304efad8f0c1a41eea134cfe4693b2

See more details on using hashes here.

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