Basic Syntax
The join parameter uses the following format:- relation: The name of the relation field defined in your entity
Example
Simple Joins
Load a related entity without specifying which fields to select:Selecting Related Fields
You can specify which fields to return from the related entity:Syntax
- relation: The relation name
- fields: Comma-separated list of fields from the related entity
Examples
The delimiter between relation and fields is
|| (double pipe), while fields are separated by , (comma).Multiple Joins
You can join multiple relations in a single request:Example: Blog Post with Author and Comments
- Author’s name and email
- Comments text and creation date
- Category name
Nested Relations
You can load nested relations using dot notation:Example: Multi-Level Nesting
Join Types
By default, joins use LEFT JOIN behavior. The join is applied based on your entity relationships:- One-to-One: Loads the single related entity
- Many-to-One: Loads the related parent entity
- One-to-Many: Loads all related child entities
- Many-to-Many: Loads all related entities through junction table
Examples by Relationship Type
Filtering on Relations
You can filter by fields in related entities:Multiple Relation Filters
Sorting by Relations
Sort results by fields in related entities:Multiple Sorts with Relations
Complete Examples
User Profile API
Blog API
E-commerce API
Social Media API
Combining All Features
- Joins author (with name and email)
- Joins category (with name)
- Filters published posts
- Filters posts by active authors
- Sorts by creation date (newest first)
- Returns specific fields
- Returns 20 posts per page
- Returns the first page
Entity Configuration
For joins to work, your entities must define the relationships:TypeORM Example
Controller Configuration
Enable relations in your CRUD controller:Security Considerations
Whitelist Relations
Always whitelist allowed relations:Limit Nested Joins
Prevent excessive nesting to avoid performance issues:Performance Considerations
N+1 Query Problem
Joins help avoid the N+1 query problem:Selective Field Loading
Only load fields you need:Limit Joined Results
Always use pagination with joins:Index Foreign Keys
Ensure foreign keys are indexed for efficient joins:Error Handling
Invalid join parameters will throw aRequestQueryException:
Best Practices
- Whitelist relations: Always configure allowed relations in your controller
- Select specific fields: Use field selection to reduce payload size
- Use pagination: Always paginate when joining one-to-many relations
- Index foreign keys: Add database indexes to foreign key columns
- Limit nesting depth: Don’t allow too many levels of nested joins
- Eager vs lazy loading: Use
eager: falseand load on demand - Document relations: Clearly document available relations in your API docs
- Monitor performance: Track query performance and optimize slow joins
Common Patterns
Loading User Profile Data
Blog Post with Full Context
E-commerce Product Details
Social Feed
Next Steps
Filtering
Filter by fields in related entities
Sorting
Sort by fields in related entities
Field Selection
Select specific fields from relations
Query Parameters
Overview of all query parameters