Skip to main content

ODM with pydantic made it simple

Project description

MongoZ

mongoz

🔥 ODM with Pydantic made it simple 🔥

Test Suite Package version Supported Python versions


Documentation: https://mongoz.dymmond.com 📚

Source Code: https://github.com/dymmond/mongoz


Motivation

MongoZ is an async Python ODM (Object Document Mapper) for MongoDB built on top of Motor and Pydantic.

MongoZ is also inspired by the great work of Aminalee from the MongoX.

So why a MongoZ if there is a MongoX? Well, MongoZ is from the same author of Esmerald, Saffier, Mongoz and many other tools out there and they all follow a specific need and pattern of development.

Mongox implements really well some operations with MongoDB but for use cases where Signals, for example, are needed, Mongox was not aiming at it and also since the creator of Mongoz is the same as Saffier and Saffier, the friendly interface to interact is also a must.

In the end, there was a need to add Pydantic 2+ with some more extras that was not coming in the Mongox.

Mongoz

This is some sort of a fork of Mongox with a rewritten core but reusing some of its best features while adding additional ones and a common and friendly interface as well as intuitive.

This ODM is designed for async which means flexibility and compatibility with various frameworks out there such as Esmerald, FastAPI, Sanic, Starlette and many others making MongoZ framework agnostic.

Features

While adopting a familiar interface, it offers some cool and powerful features using Pydantic and Motor.

Syntax

Mongoz allows two different types of syntax to be used.

  • With a familiar interface inspired by Django.
  • With a familiar interface inspired by Mongox.

The documentation follows a more familiar interface inspired by Edgy but will also show how you could also use the other allowed syntax as well

Key features

  • Document inheritance - For those cases where you don't want to repeat yourself while maintaining integrity of the documents.
  • Abstract classes - That's right! Sometimes you simply want a document that holds common fields that doesn't need to created as a document in the database.
  • Meta classes - If you are familiar with Django, this is not new to you and Mongoz offers this in the same fashion.
  • Filters - Filter by any field you want and need.
  • Model operators - Classic operations such as update, get, get_or_none and many others.
  • Indexes - Unique indexes through meta fields.
  • Signals - Quite useful feature if you want to "listen" to what is happening with your documents.

And a lot more you can do here.

Installation

To install Mongoz, run:

$ pip install mongoz

Quickstart

The following is an example how to start with Mongoz and more details and examples can be found throughout the documentation.

Use ipython to run the following from the console, since it supports await.

import asyncio

import mongoz

database_uri = "mongodb://localhost:27017"
registry = mongoz.Registry(database_uri, event_loop=asyncio.get_running_loop)


class User(mongoz.Document):
    name: str = mongoz.String(max_length=255)
    email: str = mongoz.Email(max_length=255)
    is_verified: bool = mongoz.Boolean(default=False)

    class Meta:
        registry = registry
        database = "my_db"

Now you can generate some documents and insert them into the database.

=== "Simple"

```python
user = await User.objects.create(name="Mongoz", email="mongoz@mongoz.com")
```

=== "Alternative"

```python
user = await User(name="Mongoz", email="mongoz@mongoz.com").create()
```

This will return an instance of a User in a Pydantic model and mypy will understand this is a User instance automatically which meand the type hints and validations will work everywhere.

Fetching

Since Mongoz was built on the top of Motor, means you can also use the same pattern to query as used in PyMongo/Motor.

=== "Simple"

```python
user = await User.objects.get(name="Mongoz")
```

=== "Alternative"

```python
user = await User.query({"name": "Mongoz"}).get()
```

Or you can use the User fields instead of dictionaries (check the "Alternative" for this option).

=== "Simple"

```python
user = await User.objects.get(name="Mongoz")
```

=== "Alternative"

```python
user = await User.query({User.name: "Mongoz"}).get()
```

Or a more python similar approach (check the "Alternative" for this option).

=== "Simple"

```python
user = await User.objects.get(name="Mongoz")
```

=== "Alternative"

```python
user = await User.query(User.name == "Mongoz").get()
```

There are plenty of operations you can do with Mongoz and you can see them all throughout the documentation or in the Queries section.

Mongoz praises simplicity and there is no preference in the syntax used within the queries. You can use what we called "Mongoz" option and the "Alternative" at the same time as both work really well combined.

Both are Mongoz syntaxes but for the sake of the documentation, we classify them with different names for representation purposes only.

Note

Mongoz document declaration with typing is merely visual. The validations of the fields are not done by the typing of the attribute of the documents but from the mongoz fields.

Which means you don't need to worry about the wrong typing as long as you declare the correct field type.

So does that mean Pydantic won't work if you don't declare the type? Absolutely not. Internally Mongoz runs those validations through the declared fields and the Pydantic validations are done exactly in the same way you do a normal Pydantic model.

Nothing to worry about!

Download files

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

Source Distribution

mongoz-0.11.5.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

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

mongoz-0.11.5-py3-none-any.whl (53.3 kB view details)

Uploaded Python 3

File details

Details for the file mongoz-0.11.5.tar.gz.

File metadata

  • Download URL: mongoz-0.11.5.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for mongoz-0.11.5.tar.gz
Algorithm Hash digest
SHA256 6475398b0cadc37c77390e81f842f406d15ba8a98058ff1e2b3439e547e715ef
MD5 1d1b7695d59c4c36f6b46e667e9354a4
BLAKE2b-256 d9762d77e338e7a9ce2774a5fec20cd906c8eb66b4883f14fe7ece62ee7cf19b

See more details on using hashes here.

File details

Details for the file mongoz-0.11.5-py3-none-any.whl.

File metadata

  • Download URL: mongoz-0.11.5-py3-none-any.whl
  • Upload date:
  • Size: 53.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for mongoz-0.11.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9e4113bb294e9e5252e85bea4e68f33fcd806d8a1472a6308e6f07c37515901e
MD5 79afe11fbd12c25aafb00ff344f527be
BLAKE2b-256 b087b73340703512d201b98d71d9f12872b2f3ec8c0cf437d6897641b3d686be

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