Prisma Find with Relations
Query related records using Prisma ORM include and select for efficient nested data loading.
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export async function getUserWithPosts(userId: string) {
return prisma.user.findUnique({
where: { id: userId },
include: {
posts: {
where: { published: true },
orderBy: { createdAt: 'desc' },
take: 10,
select: {
id: true,
title: true,
slug: true,
createdAt: true,
_count: { select: { comments: true } },
},
},
profile: true,
},
});
}Sponsored
Try Prisma Accelerate — Global Database Cache
Use Cases
- Loading user profiles with posts
- Querying orders with line items
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
PostgreSQL Docker Setup with Init Script
Docker Compose for PostgreSQL with volume persistence, init scripts, and connection pooling.
Database Sync Script in Python
Sync data between two databases with upsert logic, batch processing, and change detection.
JWT Verify Middleware
Express middleware that verifies JWT tokens from the Authorization header and attaches the decoded payload to the request.
In-Memory Rate Limiter for Express
Token bucket rate limiter middleware for Express with configurable window and max requests per IP.