Overview
TheDeduplicateJoinsPlugin automatically removes duplicate join clauses from your queries. This is particularly useful when building complex queries dynamically or when using query composition patterns that might inadvertently add the same join multiple times.
Installation
Usage Example
Without the Plugin
With the Plugin
Methods
transformQuery
args.queryId- Unique identifier for the queryargs.node- The root operation node to transform
transformResult
args.queryId- Unique identifier for the queryargs.result- The query result
Real-World Use Cases
Dynamic Query Building
When building queries dynamically, you might add joins conditionally without tracking whether they’ve already been added:Query Composition with Helper Functions
Reusable Query Fragments
Complex Multi-Table Queries
How It Works
The plugin uses aDeduplicateJoinsTransformer that:
- Traverses the operation node tree
- Identifies all join nodes in the query
- Compares join nodes for equality
- Removes subsequent joins that are identical to earlier ones
- Preserves the order of the first occurrence of each unique join
When Joins Are Considered Duplicates
Two joins are considered duplicates if they have:- Same join type (INNER, LEFT, RIGHT, etc.)
- Same table being joined
- Same join condition
- Same table alias (if any)
Performance Impact
The plugin adds minimal overhead:- Only processes queries that have joins
- Comparison is done using operation node equality
- No runtime performance impact on query execution
- Prevents unnecessary database work by eliminating redundant joins