-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fixes for mongo queries (new django 5 expressions, objectid fi…
…eld for PG, date with timezone) (#8) * chore: support new integer lookups fully + isNull lookup * fix: objectid field postgresql support * fix: properly handle datetime fields timezones, even if not supported by mongodb
- Loading branch information
Showing
13 changed files
with
268 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,57 @@ | ||
from typing import TypeVar | ||
from typing import Generic, Literal, TypeVar | ||
|
||
from django.db import models | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class MongoQuerySet(models.QuerySet): | ||
class MongoQuerySet(Generic[T], models.QuerySet[T]): | ||
"""QuerySet which uses MongoDB as backend""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._prefer_search = False | ||
self._aggregation_stages = [] | ||
|
||
def prefer_search(self, prefer_search=True): | ||
obj = self._chain() | ||
obj._prefer_search = prefer_search | ||
obj.query.prefer_search = prefer_search | ||
return obj | ||
|
||
def add_aggregation_stage( | ||
self, | ||
stage: dict, | ||
position: Literal["prepend", "pre-sort", "append"] = "prepend", | ||
): | ||
obj = self._chain() | ||
obj._aggregation_stages.append((position, stage)) | ||
obj.query.aggregation_stages = obj._aggregation_stages | ||
return obj | ||
|
||
def _chain(self): | ||
""" | ||
Add the _prefer_search hint to the chained query | ||
""" | ||
obj = super()._chain() | ||
if obj._prefer_search: | ||
obj.query.prefer_search = obj._prefer_search | ||
if obj._aggregation_stages: | ||
obj.query.aggregation_stages = obj._aggregation_stages | ||
return obj | ||
|
||
def _clone(self): | ||
obj = super()._clone() | ||
obj._prefer_search = self._prefer_search | ||
obj._aggregation_stages = self._aggregation_stages | ||
return obj | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class MongoManager(models.Manager[T]): | ||
class MongoManager(Generic[T], models.Manager[T]): | ||
"""Manager which uses MongoDB as backend""" | ||
|
||
def get_queryset(self): | ||
def get_queryset(self) -> MongoQuerySet[T]: | ||
return MongoQuerySet(self.model, using=self._db) | ||
|
||
def prefer_search(self, require_search=True): | ||
def prefer_search(self, require_search=True) -> MongoQuerySet[T]: | ||
return self.get_queryset().prefer_search(require_search) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.