Skip to main content

Document object mapper for pydantic and pymongo

Project description

Pydantic Mongo

Build Status Maintainability Test Coverage Version Downloads

Document object mapper for pydantic and pymongo

Usage

Install:

pip install pydantic-mongo

Example Code

from bson import ObjectId
from pydantic import BaseModel
from pydantic_mongo import AbstractRepository, ObjectIdField
from pymongo import MongoClient
from typing import List
import os

class Foo(BaseModel):
   count: int
   size: float = None

class Bar(BaseModel):
   apple: str = 'x'
   banana: str = 'y'

class Spam(BaseModel):
   id: ObjectIdField = None
   foo: Foo
   bars: List[Bar]

class SpamRepository(AbstractRepository[Spam]):
   class Meta:
      collection_name = 'spams'

client = MongoClient("mongodb://localhost:27017")
database = client["example"]

spam = Spam(foo=Foo(count=1, size=1.0),bars=[Bar()])

spam_with_predefined_id = Spam(
   id=ObjectId("611827f2878b88b49ebb69fc"),
   foo=Foo(count=2, size=2.0),
   bars=[Bar()]
)

spam_repository = SpamRepository(database=database)

# Insert / Update
spam_repository.save(spam)

# Insert / Update many items
spam_repository.save_many([spam, spam_with_predefined_id])

# Delete
spam_repository.delete(spam)

# Find One By Id
result = spam_repository.find_one_by_id(spam.id)

# Find One By Id using string if the id attribute is a ObjectIdField
result = spam_repository.find_one_by_id(ObjectId('611827f2878b88b49ebb69fc'))
assert result.foo.count == 2

# Find One By Query
result = spam_repository.find_one_by({'foo.count': 1})

# Find By Query
results = spam_repository.find_by({'foo.count': {'$gte': 1}})

# Paginate using cursor based pagination
edges = spam_repository.paginate({'foo.count': {'$gte': 1}}, limit=1)
more_edges = spam_repository.paginate({'foo.count': {'$gte': 1}}, limit=1, after=list(edges)[-1].cursor)

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_mongo-2.1.2-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pydantic-mongo-2.1.2.tar.gz.

File metadata

  • Download URL: pydantic-mongo-2.1.2.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pydantic-mongo-2.1.2.tar.gz
Algorithm Hash digest
SHA256 3eb4db8f2eb5abb1e4af4d005c92a2ca0c386c3c8eff125a9174edfe4ffe6633
MD5 abe2139c4e009b9e8c0c7f1c8dde65c1
BLAKE2b-256 101e032210295fa0026272ce575fd49a3f4454b597c31e5f4f827f2682ae8cb2

See more details on using hashes here.

File details

Details for the file pydantic_mongo-2.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_mongo-2.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 44cc02484eb87e812064cf85130e87b8e5e0fa660cd7188e46f153b7eaf58617
MD5 a87c772791cf2ee82d9ce6eb8a683092
BLAKE2b-256 1ca07737b53909865b29b85c6fd91941e2a483c4bb9771e2b3611b3db2c540b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page