Skip to content

feat: add AND-mode to-many filtering via per-value EXISTS subqueries#1141

Merged
ppetzold merged 4 commits intoppetzold:masterfrom
SIMPL-RouvenKruse:feat/and-mode-to-many-filtering
Apr 28, 2026
Merged

feat: add AND-mode to-many filtering via per-value EXISTS subqueries#1141
ppetzold merged 4 commits intoppetzold:masterfrom
SIMPL-RouvenKruse:feat/and-mode-to-many-filtering

Conversation

@SIMPL-RouvenKruse
Copy link
Copy Markdown
Contributor

Adds support for $and comparator on to-many filtered columns, allowing callers to express 'entity must have ALL of these related values' semantics.

Key changes:

  • addToManySubFilters: detect $and comparator values on sub-columns and emit one correlated EXISTS subquery per value (ANDed on outer query), instead of a single shared EXISTS where AND conditions on the same column would always be false on a single row
  • buildExistsQb: accepts existsIndex to make aliases unique per EXISTS (prevents TypeORM alias deduplication collapsing multiple subqueries)
  • Parameter renaming: recursively renames :paramName references in WHERE conditions of non-first EXISTS subqueries to avoid parameter collision when TypeORM merges subquery parameters into the outer query
  • FilterComparator exported from paginate.ts for consumer use
  • ManyToMany junction table support added (cherry-pick of commit 66404d8)
  • 5 new tests covering OneToMany AND-mode, ManyToMany AND-mode, negative cases, and single-value $and (should behave like OR-mode)

@ppetzold
Copy link
Copy Markdown
Owner

LGTM. Can you rebase?

Adds support for $and comparator on to-many filtered columns, allowing
callers to express 'entity must have ALL of these related values' semantics.

Key changes:
- addToManySubFilters: detect $and comparator values on sub-columns and
  emit one correlated EXISTS subquery per value (ANDed on outer query),
  instead of a single shared EXISTS where AND conditions on the same column
  would always be false on a single row
- buildExistsQb: accepts existsIndex to make aliases unique per EXISTS
  (prevents TypeORM alias deduplication collapsing multiple subqueries)
- Parameter renaming: recursively renames :paramName references in WHERE
  conditions of non-first EXISTS subqueries to avoid parameter collision
  when TypeORM merges subquery parameters into the outer query
- FilterComparator exported from paginate.ts for consumer use
- ManyToMany junction table support added (cherry-pick of commit 66404d8)
- 5 new tests covering OneToMany AND-mode, ManyToMany AND-mode, negative
  cases, and single-value $and (should behave like OR-mode)
…fe aliases, and validation

Address review feedback on the AND-mode to-many filtering feature:
- Extract hasExplicitAndComparator helper to reliably detect user-written $and vs default comparator
- Export TYPEORM_PARAM_REGEX (handles dots and spread params, skips :: casts) for testability
- Scope EXISTS alias/parameter suffixes with pathSlug to prevent collisions across independent to-many paths
- Add AddFilterOptions interface (maxAndValues, validateAndComparator) and thread it through addFilter/addToManySubFilters
- Guard inverse ManyToMany (parentMeta.inverseRelation) and document column-role swap
- Throw on mixed $and + non-$and values, bare $and, $and on scalar columns, and $and + $none/$all
- Extract buildSubqueryJoinChain, correlateManyToManyOrFk, renameSubqueryParameters as named inner functions
- Add JSDoc to addToManySubFilters and AND-mode section to README with maxAndValues example
- Add 11 unit tests (regex + hasExplicitAndComparator) and 8 integration tests (two-path collision, inverse M2M, error conditions)
@SIMPL-RouvenKruse SIMPL-RouvenKruse force-pushed the feat/and-mode-to-many-filtering branch from 0dca20e to 5646680 Compare April 28, 2026 08:42
@SIMPL-RouvenKruse
Copy link
Copy Markdown
Contributor Author

Done!

@ppetzold ppetzold merged commit 04037b1 into ppetzold:master Apr 28, 2026
2 checks passed
@ppetzold
Copy link
Copy Markdown
Owner

🎉 This PR is included in version 13.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@SIMPL-RouvenKruse SIMPL-RouvenKruse deleted the feat/and-mode-to-many-filtering branch April 28, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants