mongodb-typesafe-aggregation 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. mongodb_typesafe_aggregation-0.1.0/.gitignore +14 -0
  2. mongodb_typesafe_aggregation-0.1.0/.gitmodules +3 -0
  3. mongodb_typesafe_aggregation-0.1.0/.python-version +1 -0
  4. mongodb_typesafe_aggregation-0.1.0/CHANGELOG.md +77 -0
  5. mongodb_typesafe_aggregation-0.1.0/CONTRIBUTING.md +108 -0
  6. mongodb_typesafe_aggregation-0.1.0/LICENSE +21 -0
  7. mongodb_typesafe_aggregation-0.1.0/PKG-INFO +465 -0
  8. mongodb_typesafe_aggregation-0.1.0/README.md +438 -0
  9. mongodb_typesafe_aggregation-0.1.0/SUBMODULE_README.md +172 -0
  10. mongodb_typesafe_aggregation-0.1.0/pyproject.toml +85 -0
  11. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/__init__.py +59 -0
  12. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/builder.py +538 -0
  13. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/constants.py +5 -0
  14. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/errors.py +30 -0
  15. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/py.typed +0 -0
  16. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/schema.py +128 -0
  17. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/__init__.py +47 -0
  18. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/_utils.py +21 -0
  19. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/add_fields.py +25 -0
  20. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/bucket.py +55 -0
  21. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/bucket_auto.py +47 -0
  22. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/count.py +25 -0
  23. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/facet.py +25 -0
  24. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/graph_lookup.py +65 -0
  25. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/group.py +29 -0
  26. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/limit.py +24 -0
  27. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/lookup.py +59 -0
  28. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/match.py +24 -0
  29. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/merge.py +57 -0
  30. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/project.py +55 -0
  31. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/redact.py +24 -0
  32. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/replace_root.py +24 -0
  33. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/set_stage.py +27 -0
  34. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/skip.py +24 -0
  35. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/sort.py +36 -0
  36. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/union_with.py +26 -0
  37. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/unset_stage.py +26 -0
  38. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/stages/unwind.py +47 -0
  39. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/types/__init__.py +11 -0
  40. mongodb_typesafe_aggregation-0.1.0/src/mongodb_typesafe_aggregation/types/filter.py +203 -0
  41. mongodb_typesafe_aggregation-0.1.0/tests/__init__.py +0 -0
  42. mongodb_typesafe_aggregation-0.1.0/tests/test_builder.py +709 -0
  43. mongodb_typesafe_aggregation-0.1.0/tests/test_filter.py +151 -0
  44. mongodb_typesafe_aggregation-0.1.0/tests/test_integration.py +342 -0
  45. mongodb_typesafe_aggregation-0.1.0/tests/test_schema.py +188 -0
  46. mongodb_typesafe_aggregation-0.1.0/tests/test_stages.py +396 -0
  47. mongodb_typesafe_aggregation-0.1.0/uv.lock +1149 -0
@@ -0,0 +1,14 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ build/
5
+ dist/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ coverage.xml
12
+ htmlcov/
13
+ mongodb-pipeline-demo-py
14
+ .DS_Store
@@ -0,0 +1,3 @@
1
+ [submodule "mongodb-pipeline-demo-py"]
2
+ path = mongodb-pipeline-demo-py
3
+ url = https://github.com/zuruoke/mongodb-pipeline-demo-py.git
@@ -0,0 +1,77 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2026-11-03
9
+
10
+ First release of the Python version of
11
+ [mongodb-typesafe-aggregation](https://github.com/zuruoke/mongodb-typesafe-aggregation).
12
+
13
+ ### Added
14
+
15
+ #### Pipeline Builder
16
+
17
+ - `PipelineBuilder[T]`: fluent, immutable builder for MongoDB aggregation pipelines. Every
18
+ method returns a new builder.
19
+ - `.build()`: returns a deep copy of the pipeline as `list[dict]`
20
+ - `.raw(stage)`: escape hatch for adding any stage without checks
21
+ - `.cast(schema)`: declares the document shape after a stage that changes it
22
+
23
+ #### Pipeline Stages
24
+
25
+ | Method | MongoDB Stage | Description |
26
+ | --------------------- | -------------- | ------------------------------------- |
27
+ | `.match(query)` | `$match` | Filter documents |
28
+ | `.lookup(from_, ...)` | `$lookup` | Join from another collection |
29
+ | `.group(spec)` | `$group` | Group and accumulate |
30
+ | `.add_fields(spec)` | `$addFields` | Add computed fields |
31
+ | `.set(spec)` | `$set` | Alias for `$addFields` (MongoDB 4.2+) |
32
+ | `.project(spec)` | `$project` | Include/exclude/reshape fields |
33
+ | `.unset(fields)` | `$unset` | Remove fields |
34
+ | `.sort(spec)` | `$sort` | Sort documents |
35
+ | `.skip(n)` | `$skip` | Skip N documents |
36
+ | `.limit(n)` | `$limit` | Limit to N documents |
37
+ | `.unwind(path)` | `$unwind` | Deconstruct an array field |
38
+ | `.replace_root(root)` | `$replaceRoot` | Replace root document |
39
+ | `.count(field)` | `$count` | Count matching documents |
40
+ | `.facet(spec)` | `$facet` | Multi-faceted aggregation |
41
+ | `.union_with(coll)` | `$unionWith` | Union with another collection |
42
+ | `.graph_lookup(...)` | `$graphLookup` | Recursive graph traversal |
43
+ | `.bucket(...)` | `$bucket` | Categorise into buckets |
44
+ | `.bucket_auto(...)` | `$bucketAuto` | Auto-distribute into buckets |
45
+ | `.redact(expression)` | `$redact` | Conditionally include/exclude content |
46
+ | `.merge(into, ...)` | `$merge` | Write results to another collection |
47
+
48
+ Each stage is also available as a standalone function (`match_stage()`, `lookup_stage()`, ...).
49
+
50
+ #### FilterOperator
51
+
52
+ `FilterOperator[T]`: typed builder for MongoDB query and expression operators, with the same
53
+ methods as the TypeScript version: `eq`, `ne`, `gt`, `gte`, `lt`, `lte`, `in_`, `nin`, `all`,
54
+ `size`, `exists`, `regex`, `field_eq`, `field_ne`, `cond`.
55
+
56
+ Also exports a `filter(type)` factory from `mongodb_typesafe_aggregation.types.filter` as a
57
+ concise alternative to `FilterOperator[T]()`. Stages build `FilterOperator` values
58
+ automatically, so `.build()` is optional.
59
+
60
+ #### Schema checks
61
+
62
+ - Field names are checked against a `TypedDict`, dataclass, Pydantic (v1/v2, by alias) or
63
+ annotated class, including `"$field"` paths inside expressions.
64
+ - The known fields are tracked through every stage, so checks stay correct after `group`,
65
+ `project`, `facet`, `lookup` and other shape-changing stages.
66
+ - `UnknownFieldError` (a `ValueError`) names the stage, suggests the closest field, and lists
67
+ the known fields.
68
+ - `strict=False`, or no schema, skips the runtime checks.
69
+
70
+ #### Validation
71
+
72
+ `ValueError` for invalid sort orders, negative `skip` or non-positive `limit`, `$project`
73
+ mixing inclusion and exclusion, `$group` without `_id`, `$unwind` paths without `$`,
74
+ `$lookup` without a join condition, unordered `$bucket` boundaries, and stages added after
75
+ `$merge`.
76
+
77
+ [0.1.0]: https://github.com/zuruoke/mongodb-typesafe-aggregation-py/releases/tag/v0.1.0
@@ -0,0 +1,108 @@
1
+ # Contributing
2
+
3
+ Thank you for taking the time to contribute.
4
+
5
+ ## Local Setup
6
+
7
+ Requires [uv](https://docs.astral.sh/uv/).
8
+
9
+ ```bash
10
+ git clone --recursive https://github.com/zuruoke/mongodb-typesafe-aggregation-py.git
11
+ cd mongodb-typesafe-aggregation-py
12
+ uv sync
13
+ ```
14
+
15
+ ## Development Workflow
16
+
17
+ ```bash
18
+ # Run tests
19
+ uv run pytest
20
+
21
+ # Run tests with coverage
22
+ uv run pytest --cov
23
+
24
+ # Check types
25
+ uv run mypy
26
+
27
+ # Lint
28
+ uv run ruff check
29
+
30
+ # Format
31
+ uv run ruff format
32
+
33
+ # Build
34
+ uv build
35
+ ```
36
+
37
+ ### Integration tests
38
+
39
+ Integration tests run pipelines against a real MongoDB. They are skipped unless `MONGODB_URI`
40
+ is set, and they create and drop a throwaway database, so they never touch your data.
41
+
42
+ ```bash
43
+ # Start a MongoDB, e.g. the demo container (see SUBMODULE_README.md)
44
+ cd mongodb-pipeline-demo-py && MONGO_PORT=27018 docker compose up -d && cd ..
45
+
46
+ MONGODB_URI=mongodb://localhost:27018 uv run pytest -m integration
47
+ ```
48
+
49
+ ## Branch Convention
50
+
51
+ | Branch prefix | When to use |
52
+ | ------------- | ------------------------------------- |
53
+ | `feat/` | New stage or operator |
54
+ | `fix/` | Bug fix |
55
+ | `docs/` | Documentation only |
56
+ | `refactor/` | Code restructure, no behaviour change |
57
+ | `test/` | New or improved tests |
58
+ | `chore/` | Tooling, deps, CI |
59
+
60
+ Branch off `main` and target `main` with your PR.
61
+
62
+ ## Adding a New Pipeline Stage
63
+
64
+ 1. Create `src/mongodb_typesafe_aggregation/stages/my_stage.py` following the existing
65
+ pattern: a `my_stage_stage()` function that validates its input, builds `FilterOperator`
66
+ values with `resolve_filters()`, and returns the stage dict.
67
+ 2. Export it from `stages/__init__.py` and from the package `__init__.py`.
68
+ 3. Add the method to `PipelineBuilder` in `builder.py`. Check every field the stage reads
69
+ against the current schema (`check_field`, `check_query` or `check_expression`), and pass
70
+ the new set of known fields to `_add()` if the stage changes the document shape.
71
+ 4. Write test cases in `tests/test_stages.py` and `tests/test_builder.py`, including the field
72
+ checks and the shape afterwards.
73
+ 5. Add an example to the demo (`mongodb-pipeline-demo-py`) and check it against MongoDB.
74
+ 6. Document it in `README.md`, and in `CHANGELOG.md` under an `[Unreleased]` section.
75
+
76
+ ## Commit Message Format
77
+
78
+ Follow [Conventional Commits](https://www.conventionalcommits.org/):
79
+
80
+ ```
81
+ feat(stages): add $sortByCount stage
82
+ fix(filter): handle None in exists operator
83
+ docs: update API reference table
84
+ ```
85
+
86
+ ## Pull Request Checklist
87
+
88
+ - [ ] Tests pass (`uv run pytest`)
89
+ - [ ] Types check (`uv run mypy`)
90
+ - [ ] No lint or format errors (`uv run ruff check && uv run ruff format --check`)
91
+ - [ ] New behaviour is covered by a test
92
+ - [ ] `CHANGELOG.md` updated under `[Unreleased]`
93
+
94
+ ## Releasing (Maintainers)
95
+
96
+ One-time setup: on PyPI, add a [trusted publisher](https://docs.pypi.org/trusted-publishers/)
97
+ for the project `mongodb-typesafe-aggregation` with owner `zuruoke`, repository
98
+ `mongodb-typesafe-aggregation-py`, workflow `release.yml` and environment `pypi`. Create the
99
+ `pypi` environment in the GitHub repository settings.
100
+
101
+ For each release:
102
+
103
+ 1. Update `CHANGELOG.md`: move `[Unreleased]` to the new version with today's date.
104
+ 2. Bump `__version__` in `src/mongodb_typesafe_aggregation/__init__.py`.
105
+ 3. Commit, then tag the commit with the same version: `git tag vX.Y.Z`
106
+ 4. Push the commit and the tag: `git push && git push --tags`
107
+ 5. The `release.yml` GitHub Action checks that the tag matches `__version__`, runs the checks,
108
+ and publishes to PyPI.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Chimzuruoke Okafor Augustine Best
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.