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.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

pydantic_mongo_document-0.2.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_mongo_document-0.2.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.1 Darwin/23.6.0

File hashes

Hashes for pydantic_mongo_document-0.2.2.tar.gz
Algorithm Hash digest
SHA256 285e776e5f91497cb8253ea481ad1f0a160a6e31b1240ee9ffcd0020a3d39a7c
MD5 deb05c8c1b9bf639db08613d2f150ce8
BLAKE2b-256 2c12d722f5a05d76860b3db2c4e8a482f18a3bebfbed3cb27747419728cb7895

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_mongo_document-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 98b3c88867327e00ab01e8d0c908aa15d1b646b9f8b4003cbb52f21e99c086f5
MD5 f0c70c5d9a3e46eba15c99c504cd574d
BLAKE2b-256 f1d5e386951f02a42c4b73e1f2bd6e38c25921f54d7053496449307a3045045c

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