Skip to main content
Relations (joins) allow you to load associated entities in a single request, reducing the need for multiple API calls. This is essential for efficient data fetching in relational databases.

Basic Syntax

The join parameter uses the following format:
  • relation: The name of the relation field defined in your entity

Example

This retrieves users with their associated profile data included.

Simple Joins

Load a related entity without specifying which fields to select:
This returns all fields from both the main entity and the related entity. 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

This returns posts with:
  • 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

When filtering by relation fields, make sure to include the corresponding join parameter.

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

This request:
  1. Joins author (with name and email)
  2. Joins category (with name)
  3. Filters published posts
  4. Filters posts by active authors
  5. Sorts by creation date (newest first)
  6. Returns specific fields
  7. Returns 20 posts per page
  8. 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:
Only explicitly allowed relations and fields can be accessed via the API. Configure the join option in your CRUD decorator to control access.

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 a RequestQueryException:

Best Practices

  1. Whitelist relations: Always configure allowed relations in your controller
  2. Select specific fields: Use field selection to reduce payload size
  3. Use pagination: Always paginate when joining one-to-many relations
  4. Index foreign keys: Add database indexes to foreign key columns
  5. Limit nesting depth: Don’t allow too many levels of nested joins
  6. Eager vs lazy loading: Use eager: false and load on demand
  7. Document relations: Clearly document available relations in your API docs
  8. 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