Load related entities using joins in NestJS CRUD requests
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.
# Load user with specific profile fieldsGET /users/1?join=profile||firstName,lastName,city# Load post with author name and email onlyGET /posts/1?join=author||name,email# Load order with customer name onlyGET /orders/1?join=customer||name
The delimiter between relation and fields is || (double pipe), while fields are separated by , (comma).
You can join multiple relations in a single request:
# Join multiple relationsGET /users?join=profile&join=posts&join=comments# Join with field selectionGET /users?join=profile||firstName,lastName&join=posts||title,createdAt
# Load user with profile and profile's addressGET /users?join=profile&join=profile.address# Load post with author and author's profileGET /posts?join=author&join=author.profile# Load order with customer and customer's addressGET /orders?join=customer&join=customer.address||street,city,country
# One-to-One: User has one ProfileGET /users?join=profile# Many-to-One: Post belongs to one AuthorGET /posts?join=author# One-to-Many: User has many PostsGET /users?join=posts# Many-to-Many: Post has many TagsGET /posts?join=tags
# Get user with full profileGET /users/123?join=profile# Get user with selected profile fieldsGET /users/123?join=profile||firstName,lastName,city,country# Get users with profiles, filtered by cityGET /users?join=profile&filter=profile.city||eq||New York&limit=20
# Get post with author infoGET /posts/456?join=author||name,email,avatar# Get posts with comments and authorsGET /posts?join=author&join=comments&sort=createdAt,DESC&limit=10# Get published posts with author and categoryGET /posts?filter=status||eq||published&join=author||name&join=category||name&sort=publishedAt,DESC
# Get order with customer and itemsGET /orders/789?join=customer||name,email&join=items||productName,quantity,price# Get products with category and reviewsGET /products?join=category||name&join=reviews||rating,comment&filter=isActive||eq||true# Get customer with all ordersGET /customers/123?join=orders||id,total,status,createdAt
# Get posts with author, likes, and comments countGET /posts?join=author||name,avatar&fields=id,content,createdAt,likesCount,commentsCount# Get user with followers and followingGET /users/123?join=followers||name,avatar&join=following||name,avatar# Get comments with post and authorGET /comments?join=post||title&join=author||name,avatar&sort=createdAt,DESC
GET /posts?join=author||name,email&join=category||name&filter=status||eq||published&filter=author.isActive||eq||true&sort=createdAt,DESC&fields=id,title,content&limit=20&page=1
# ❌ Bad: N+1 queries (1 for users + N for each user's profile)GET /users # Then fetch each profile separately# ✅ Good: Single query with joinGET /users?join=profile