Giter Site home page Giter Site logo

dymmond / mongoz Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 3.0 593 KB

ODM with Pydantic made it simple

Home Page: https://mongoz.dymmond.com

License: MIT License

Python 99.58% Shell 0.42%
async asyncio edgy esmerald esmerald-framework mongodb mongodb-atlas mongodb-database motor pydantic pydantic-v2 pymongo python python3 starlette

mongoz's Introduction

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="[email protected]")
```

=== "Alternative"

```python
user = await User(name="Mongoz", email="[email protected]").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!

mongoz's People

Contributors

tarsil avatar harshalizode avatar

Stargazers

Prashik Janardhan Dewtale  avatar Alin Floare avatar  avatar Sarvesh Dwivedi avatar Sanket Parte avatar Cristina Grande avatar  avatar Max avatar  avatar M_dev_pro avatar Hasan Sezer Taşan avatar Sultar avatar Pedro Correia avatar

Watchers

Alexander avatar Pedro Correia avatar  avatar

mongoz's Issues

With setting the indexes auto generate flag is true

Hello,

I have a Model as:

indexes = [
    Index(keys=[("year", Order.DESCENDING), ("genre", IndexType.HASHED)]),
]


class AnotherMovie(Document):
    name: str = mongoz.String()
    email: str = mongoz.Email(index=True, unique=True)
    year: int = mongoz.Integer()
    uuid: Optional[ObjectId] = mongoz.ObjectId(null=True)

    class Meta:
        registry = client
        indexes = indexes
        database = "test_db"
        autogenerate_index = True

When I create a duplicate record in this model, it allows me to do so, unless I use the await AnotherMovie.create_indexes() method.

And if any model does not have indexes, calling the await AnotherMovie.create_indexes() method returns an error as well.

Thank you.

Getting issue while storing the decimal value in the document.

Hello,

I am trying to store the decimal value in by embedded document:

Class Achived(Document):
    score_from: float = mongoz.Decimal
    score_to: float = mongoz.Decimal(max_digits=5, decimal_places=3)
    amount: float = mongoz.Decimal(
        max_digits=5, decimal_places=3)

When I try to store the decimal value it raise the exception as:
InvalidDocument("cannot encode object: Decimal('0.60'), of type: <class 'decimal.Decimal'>")
.
As I passing the string or float value for it to store in document.

Document with a table name not able to utilize

Hello,
I have tried to rename my collection as per the documentation https://mongoz.tarsild.io/documents/?h=collection#document-without-table-name, but here is an issue.
When I am trying to add the collection in the Meta class then, it is raising the issue as

    class Company(BaseDocument):
  File "/home/venv/proj.env/lib/python3.11/site-packages/mongoz/core/db/documents/metaclasses.py", line 290, in __new__
    collection_name = meta.collection.name
                      ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'name'

Getting the issue while using the Abstract class.

Hello,
I have a models as:

class BaseDocument(mongoz.Document):
    """
    The base document for all documents using the `registry` registry.
    """
    objects: ClassVar[CustomManger] = CustomManger()

    class Meta:
        """
            It contains the Meta information related the database for \
                each model level.
        """
        abstract = True
        registry = settings.client
        database = settings.db
class Employee(BaseDocument):
    month_period: int = mongoz.Integer(null=True)
    full_name: str = mongoz.String()
    employee_code: str = mongoz.String()
    date_of_joining: datetime.datetime = mongoz.DateTime(null=True)
    e_mail: str = mongoz.String(null=True)
    mobile_no: str = mongoz.String(null=True)

When I am trying to query into db, Its using the default manager i.e. "Manager" object, instead of "CustomManager".

If I again initialize the "objects" in the Employee Document and try to query as:

Employee.objects.create(full_name='Emp_Name_1326', employee_code='01326')

Then, it returns the API response as:

{
    "detail": [
        {
            "type": "missing",
            "loc": [
                "month_period"
            ],
            "msg": "Field required",
            "input": {
                "full_name": "Emp_Name_1326",
                "employee_code": "01326"
            },
            "url": "https://errors.pydantic.dev/2.6/v/missing"
        },
        {
            "type": "missing",
            "loc": [
                "date_of_joining"
            ],
            "msg": "Field required",
            "input": {
                "full_name": "Emp_Name_1326",
                "employee_code": "01326"
            },
            "url": "https://errors.pydantic.dev/2.6/v/missing"
        },
        {
            "type": "missing",
            "loc": [
                "e_mail"
            ],
            "msg": "Field required",
            "input": {
                "full_name": "Emp_Name_1326",
                "employee_code": "01326"
            },
            "url": "https://errors.pydantic.dev/2.6/v/missing"
        },
        {
            "type": "missing",
            "loc": [
                "mobile_no"
            ],
            "msg": "Field required",
            "input": {
                "full_name": "Emp_Name_1326",
                "employee_code": "01326"
            },
            "url": "https://errors.pydantic.dev/2.6/v/missing"
        }
    ]
}

Can you help me for this?
Is somthing wrong here?
Thank you.

Embedded Document can't store the ObjectId.

Hello,

I have the two relation i.e. Employee and Supervisors.
Employee document has the the Supervisor as the embedded document

class Employee(Document):
    full_name: str = mongoz.String
    employee_code: str = mongoz.String
    date_of_joining: datetime.datetime = mongoz.DateTime(null=True)
    e_mail: str = mongoz.String(null=True)
    mobile_no: str = mongoz.String(null=True)
    supervisors: List[EmployeeSupervisor] = mongoz.Array(EmployeeSupervisor)


class EmployeeSupervisor(EmbeddedDocument):
    supervisor: ObjectId = mongoz.ObjectId  # Object id of employee document.
    effective_from: datetime.datetime = mongoz.DateTime
    effective_to: datetime.datetime = mongoz.DateTime

Error as:

Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/_subprocess.py", line 78, in subprocess_started
    target(sockets=sockets)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/server.py", line 65, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/server.py", line 69, in serve
    await self._serve(sockets)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/server.py", line 76, in _serve
    config.load()
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/config.py", line 433, in load
    self.loaded_app = import_from_string(self.app)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/uvicorn/importer.py", line 19, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/main.py", line 10, in <module>
    from playmity.apps.user_management.views import get_database_collection
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/__init__.py", line 5, in <module>
    from playmity.apps import user_management
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/__init__.py", line 4, in <module>
    from .urls import *
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/urls.py", line 22, in <module>
    from .views import (
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/views/__init__.py", line 9, in <module>
    from .authz import *
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/views/authz.py", line 31, in <module>
    from playmity.apps.user_management.models import (
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/models/__init__.py", line 7, in <module>
    from .employee import Employee, EmployeeBranch, EmployeeRole, EmployeeGrade, \
  File "/home/harshaliz/Desktop/Projects/My_proj/trunk/my_apps/apps/user_management/models/employee.py", line 131, in <module>
    class EmployeeSupervisor(BaseEmbeddedDocument):
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/mongoz/core/db/documents/metaclasses.py", line 420, in __new__
    new_class = super().__new__(cls, name, bases, attrs)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 183, in __new__
    complete_model_class(
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 517, in complete_model_class
    schema = cls.__get_pydantic_core_schema__(cls, handler)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/main.py", line 584, in __get_pydantic_core_schema__
    return __handler(__source)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
    schema = self._handler(__source_type)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 490, in generate_schema
    schema = self._generate_schema(obj)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 721, in _generate_schema
    schema = self._generate_schema_inner(obj)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 742, in _generate_schema_inner
    return self._model_schema(obj)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 563, in _model_schema
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 563, in <dictcomp>
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 906, in _generate_md_field_schema
    common_field = self._common_field_schema(name, field_info, decorators)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1071, in _common_field_schema
    schema = self._apply_annotations(
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1765, in _apply_annotations
    schema = get_inner_schema(source_type)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py", line 82, in __call__
    schema = self._handler(__source_type)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 1746, in inner_handler
    schema = self._generate_schema(obj)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 721, in _generate_schema
    schema = self._generate_schema_inner(obj)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 747, in _generate_schema_inner
    return self.match_type(obj)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 834, in match_type
    return self._unknown_type_schema(obj)
  File "/home/harshaliz/Desktop/Project/venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py", line 393, in _unknown_type_schema
    raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'bson.objectid.ObjectId'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.

If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.

For further information visit https://errors.pydantic.dev/2.6/u/schema-for-unknown-type

How can we achieve this?
Thank you.

Sort Query is not returning result as expected.

Discussed in #32

Originally posted by harshalizode May 6, 2024
Hello,

I have the Permission Model as:

class Permission(mongoz.Document):
    idx: int = mongoz.Integer()
    code: str = mongoz.String()

I am trying to sort the permissions in Ascending order.
await Permission.objects.sort("idx", Order.ASCENDING).values(["idx"])

But I am getting the result as
sort issue

Can you confirm that this is an issue or a mistake that occurred while running the query?
Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.