-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from volfpeter/feat/add-regex-operator
Add aggregation utility and tests, refs #19
- Loading branch information
Showing
11 changed files
with
319 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# ::: motorhead.aggregation | ||
|
||
options: | ||
show_root_heading: true |
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any, Literal | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Iterable | ||
from typing import TypeAlias | ||
|
||
from typing_extensions import Self | ||
|
||
from .typing import Clause | ||
|
||
AggregationStage = Literal[ | ||
"$addFields", | ||
"$bucket", | ||
"$bucketAuto", | ||
"$changeStream", | ||
"$changeStreamSplitLargeEvent", | ||
"$collStats", | ||
"$count", | ||
"$currentOp", | ||
"$densify", | ||
"$documents", | ||
"$facet", | ||
"$fill", | ||
"$geoNear", | ||
"$graphLookup", | ||
"$group", | ||
"$indexStats", | ||
"$limit", | ||
"$listLocalSessions", | ||
"$listSampledQueries", | ||
"$listSearchIndexes", | ||
"$listSessions", | ||
"$lookup", | ||
"$match", | ||
"$merge", | ||
"$out", | ||
"$planCacheStats", | ||
"$project", | ||
"$querySettings", | ||
"$redact", | ||
"$replaceRoot", | ||
"$replaceWith", | ||
"$sample", | ||
"$search", | ||
"$searchMeta", | ||
"$set", | ||
"$setWindowFields", | ||
"$shardedDataDistribution", | ||
"$skip", | ||
"$sort", | ||
"$sortByCount", | ||
"$unionWith", | ||
"$unset", | ||
"$unwind", | ||
"$vectorSearch", | ||
] | ||
"""Aggregation pipeline stage.""" | ||
|
||
|
||
AggregationData: TypeAlias = Any | ||
|
||
|
||
def make_aggregation_stage( | ||
stage: AggregationStage, value: AggregationData | Clause | ||
) -> dict[str, AggregationData]: | ||
""" | ||
Creates an aggregation pipeline stage. | ||
Arguments: | ||
stage: The stage operator. | ||
value: The stage operator's content. | ||
Returns: | ||
The aggregation pipeline stage. | ||
""" | ||
return {stage: value.to_mongo() if hasattr(value, "to_mongo") else value} | ||
|
||
|
||
class Aggregation(list[dict[str, AggregationData]]): | ||
"""Aggregation pipeline.""" | ||
|
||
def __init__(self, stages: Iterable[AggregationData] = ()) -> None: | ||
""" | ||
Initialization. | ||
Arguments: | ||
stages: The aggregation pipeline stages. | ||
""" | ||
super().__init__(stages) | ||
|
||
def stage(self, stage: AggregationStage, value: AggregationData | Clause) -> Self: | ||
""" | ||
Adds the stage to the aggregation pipeline. | ||
Arguments: | ||
stage: The stage operator. | ||
value: The stage operator's content. | ||
Returns: | ||
The aggregation pipeline. | ||
""" | ||
self.append(make_aggregation_stage(stage, value)) | ||
return self |
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
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 |
---|---|---|
|
@@ -31,7 +31,7 @@ tracker = "https://github.com/volfpeter/motorhead/issues" | |
|
||
[tool.poetry] | ||
name = "motorhead" | ||
version = "0.2501.1" | ||
version = "0.2501.2" | ||
description = "Async MongoDB with vanilla Pydantic v2+ - made easy." | ||
authors = ["Peter Volf <[email protected]>"] | ||
readme = "README.md" | ||
|
@@ -52,6 +52,7 @@ pytest = "^8.3.3" | |
pytest-asyncio = "^0.24.0" | ||
pytest-docker = "^3.1.1" | ||
pytest-random-order = "^1.1.1" | ||
typing-extensions = "^4.12.2" | ||
|
||
[tool.mypy] | ||
strict = true | ||
|
Oops, something went wrong.