Skip to main content

Async-first MongoDB ODM for Python, powered by Pydantic v2

Project description

Pygoose

An async-first MongoDB object-document mapper (ODM) for Python, inspired by Mongoose. Pygoose provides a clean, Pydantic-native API for working with MongoDB collections as typed Python classes.

Features

  • Async-first: Built for async/await from the ground up using PyMongo's async client
  • Pydantic-native: Use Pydantic v2 models directly as your documents
  • Type-safe: Full type hints for queries, documents, and relationships
  • QuerySet: Fluent, lazy query builder inspired by Django ORM
  • References: Type-safe document references with automatic population
  • Lifecycle hooks: Pre/post validation, save, delete, and update hooks
  • Encryption: Field-level encryption for sensitive data
  • Plugins: Built-in plugins for timestamps, soft delete, and audit trails
  • FastAPI: First-class FastAPI integration with exception handlers and schema generation

Installation

pip install pygoose

Optional dependencies:

# For field-level encryption
pip install pygoose[encryption]

# For FastAPI integration
pip install pygoose[fastapi]

# For development
pip install pygoose[dev]

Quick start

Define your documents as Pydantic models:

from pygoose import Document
from typing import Optional

class User(Document):
    name: str
    email: str
    age: Optional[int] = None

Connect to MongoDB and perform CRUD operations:

import asyncio
from pygoose import connect, disconnect

async def main():
    # Connect to MongoDB
    await connect("mongodb://localhost:27017/my_database")

    # Create
    user = User(name="Alice", email="alice@example.com", age=30)
    await user.save()

    # Read
    user = await User.find_one({"email": "alice@example.com"})

    # Update
    user.age = 31
    await user.save()

    # Delete
    await user.delete()

    # Cleanup
    await disconnect()

asyncio.run(main())

Use the fluent QuerySet API:

# Find multiple documents
users = await User.find(age={"$gte": 18}).sort("name").limit(10).all()

# Count documents
count = await User.find(age={"$gte": 18}).count()

# Check existence
exists = await User.find({"email": "alice@example.com"}).exists()

FastAPI integration

Pygoose works seamlessly with FastAPI:

from fastapi import FastAPI
from pygoose import connect, disconnect, init_app

app = FastAPI()
init_app(app)

@app.on_event("startup")
async def startup():
    await connect("mongodb://localhost:27017/my_database")

@app.on_event("shutdown")
async def shutdown():
    await disconnect()

@app.post("/users")
async def create_user(user: User):
    await user.save()
    return user

@app.get("/users/{user_id}")
async def get_user(user_id: str):
    from bson import ObjectId
    user = await User.find_one({"_id": ObjectId(user_id)})
    return user

Documentation

Complete documentation is available in the /docs directory:

Requirements

  • Python 3.11 or higher
  • MongoDB 4.4 or higher
  • PyMongo 4.8 or higher (async driver)
  • Pydantic v2 or higher

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

pygoose_odm-0.2.0.tar.gz (74.1 kB view details)

Uploaded Source

Built Distribution

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

pygoose_odm-0.2.0-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

Details for the file pygoose_odm-0.2.0.tar.gz.

File metadata

  • Download URL: pygoose_odm-0.2.0.tar.gz
  • Upload date:
  • Size: 74.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for pygoose_odm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5636ad6e951662e513a1ff3c9e13441639dc5cd5e857b78fe493978fd340be90
MD5 38c8e657904476118c289f9895d8aff1
BLAKE2b-256 ae800013642518ad177f02e7de80ca97cd7efbf9f33f77e7622b9f4a5ee183bb

See more details on using hashes here.

File details

Details for the file pygoose_odm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pygoose_odm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for pygoose_odm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a56ae85d15a0e12cc4a5a2daf8e1591dc88c8d56e6ceaef004bde4167366a06a
MD5 60c27ef9af6851e77becacc0d437e85f
BLAKE2b-256 da2dfe768cf459ede662ca427b2214736e9fcb0833349d1b21b1f5976884dfa1

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