Skip to content

Commit

Permalink
fix: query implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ale-Cas committed Jan 21, 2024
1 parent 5e01c53 commit 48685eb
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 113 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ To add and install this package as a dependency of your project, run `poetry add
### A simple example:
```python
from pydantic import BaseModel, EmailStr
from querytyper import MongoModelMetaclass, MongoQuery
from querytyper import MongoFilterMeta, MongoQuery

class User(BaseModel):
"""User database model."""
Expand All @@ -25,7 +25,7 @@ class User(BaseModel):
age: int
email: EmailStr

class UserFilter(User, metaclass=MongoModelMetaclass):
class UserFilter(User, metaclass=MongoFilterMeta):
"""User query filter."""

query = MongoQuery(
Expand Down
129 changes: 128 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pytest-clarity = ">=1.0.1"
pytest-mock = ">=3.10.0"
pytest-xdist = ">=3.2.1"
ruff = ">=0.1.3"
pymongo = "^4.6.1"
mongomock = "^4.1.2"

[tool.poetry.group.dev.dependencies] # https://python-poetry.org/docs/master/managing-dependencies/
cruft = ">=2.14.0"
Expand Down
4 changes: 2 additions & 2 deletions src/querytyper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""querytyper package."""
from querytyper.mongo import MongoModelMetaclass, MongoQuery, exists, regex_query
from querytyper.mongo import MongoFilterMeta, MongoQuery, exists, regex_query

__all__ = [
"MongoQuery",
"MongoModelMetaclass",
"MongoFilterMeta",
"regex_query",
"exists",
]
5 changes: 3 additions & 2 deletions src/querytyper/mongo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""querytyper package."""
from querytyper.mongo.query import MongoModelMetaclass, MongoQuery, exists, regex_query
from querytyper.mongo.meta import MongoFilterMeta
from querytyper.mongo.query import MongoQuery, exists, regex_query

__all__ = [
"MongoQuery",
"MongoModelMetaclass",
"MongoFilterMeta",
"regex_query",
"exists",
]
53 changes: 53 additions & 0 deletions src/querytyper/mongo/meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""MongoFilterMeta implementation."""
from typing import Any, Dict, Tuple, Type

from pydantic import BaseModel
from pydantic.main import ModelMetaclass

from querytyper.mongo.query import QueryField

DictStrAny = Dict[str, Any]


class MongoFilterMeta(ModelMetaclass):
"""
Metaclass for query models.
Use this metaclass to define query models.
Example
-------
```python
class TransactionFilter(Transaction, metaclass=MongoFilterMeta):
pass
```
"""

def __new__(
cls,
name: str,
bases: Tuple[Type[Any]],
namespace: DictStrAny,
) -> Type["MongoFilterMeta"]:
"""Override new class creation to set query fields as class attributes."""
# check exactly one base class
if len(bases) > 1:
raise TypeError("MongoFilterMeta does not support multiple inheritance")
if len(bases) < 1:
raise TypeError(
"The class with metaclass MongoFilterMeta requires a base class that inherits from BaseModel"
)
# check the base class is a subclass of BaseModel
if not issubclass(bases[0], BaseModel):
raise TypeError(f"The base class must inherit from {BaseModel.__qualname__}")
base_model_cls: Type[BaseModel] = super().__new__(cls, name, bases, namespace)
for field_name, model_field in base_model_cls.__fields__.items():
setattr(
cls,
field_name,
QueryField[model_field.type_]( # type: ignore[name-defined]
name=field_name,
field_type=model_field.type_,
),
)
return cls
Loading

0 comments on commit 48685eb

Please sign in to comment.