Labsco
prisma logo

prisma-client-api-relations

✓ Official8

by prisma · part of prisma/cursor-plugin

Relation Queries. Reference when using this Prisma feature.

🔥🔥🔥✓ VerifiedFreeQuick setup
🧩 One of 7 skills in the prisma/cursor-plugin package — works on its own, and pairs well with its siblings.

Relation Queries. Reference when using this Prisma feature.

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.

by prisma

Relation Queries. Reference when using this Prisma feature. npx skills add https://github.com/prisma/cursor-plugin --skill prisma-client-api-relations Download ZIPGitHub8

Relation Queries

Query and modify related records.

Include Relations

Load related records:

Copy & paste — that's it
const user = await prisma.user.findUnique({
 where: { id: 1 },
 include: {
 posts: true,
 profile: true
 }
})

Filtered include

Copy & paste — that's it
const user = await prisma.user.findUnique({
 where: { id: 1 },
 include: {
 posts: {
 where: { published: true },
 orderBy: { createdAt: 'desc' },
 take: 5,
 select: { id: true, title: true }
 }
 }
})

Nested include

Copy & paste — that's it
const user = await prisma.user.findUnique({
 where: { id: 1 },
 include: {
 posts: {
 include: {
 comments: {
 include: { author: true }
 }
 }
 }
 }
})

Select Relations

Copy & paste — that's it
const user = await prisma.user.findUnique({
 where: { id: 1 },
 select: {
 name: true,
 posts: {
 select: { title: true }
 }
 }
})

Nested Writes

Create with relations

Copy & paste — that's it
const user = await prisma.user.create({
 data: {
 email: '[email protected]',
 posts: {
 create: [
 { title: 'Post 1' },
 { title: 'Post 2' }
 ]
 },
 profile: {
 create: { bio: 'Hello!' }
 }
 }
})

Create or connect

Copy & paste — that's it
const post = await prisma.post.create({
 data: {
 title: 'New Post',
 author: {
 connectOrCreate: {
 where: { email: '[email protected]' },
 create: { email: '[email protected]', name: 'Alice' }
 }
 }
 }
})

Connect existing

Copy & paste — that's it
const post = await prisma.post.create({
 data: {
 title: 'New Post',
 author: {
 connect: { id: 1 }
 }
 }
})

// Shorthand for foreign key
const post = await prisma.post.create({
 data: {
 title: 'New Post',
 authorId: 1
 }
})

Update Relations

Update related records

Copy & paste — that's it
const user = await prisma.user.update({
 where: { id: 1 },
 data: {
 posts: {
 update: {
 where: { id: 1 },
 data: { title: 'Updated Title' }
 }
 }
 }
})

Update many related

Copy & paste — that's it
const user = await prisma.user.update({
 where: { id: 1 },
 data: {
 posts: {
 updateMany: {
 where: { published: false },
 data: { published: true }
 }
 }
 }
})

Upsert related

Copy & paste — that's it
const user = await prisma.user.update({
 where: { id: 1 },
 data: {
 profile: {
 upsert: {
 create: { bio: 'New bio' },
 update: { bio: 'Updated bio' }
 }
 }
 }
})

Disconnect

Copy & paste — that's it
// 1-to-1 optional
const user = await prisma.user.update({
 where: { id: 1 },
 data: {
 profile: { disconnect: true }
 }
})

// Many-to-many
const post = await prisma.post.update({
 where: { id: 1 },
 data: {
 tags: {
 disconnect: [{ id: 1 }, { id: 2 }]
 }
 }
})

Delete related

Copy & paste — that's it
const user = await prisma.user.update({
 where: { id: 1 },
 data: {
 posts: {
 delete: { id: 1 }
 }
 }
})

// Delete many
const user = await prisma.user.update({
 where: { id: 1 },
 data: {
 posts: {
 deleteMany: { published: false }
 }
 }
})

Set (replace all)

Copy & paste — that's it
// Replace all related records
const post = await prisma.post.update({
 where: { id: 1 },
 data: {
 tags: {
 set: [{ id: 1 }, { id: 2 }]
 }
 }
})

Relation Filters

some

At least one matches:

Copy & paste — that's it
const users = await prisma.user.findMany({
 where: {
 posts: { some: { published: true } }
 }
})

every

All match:

Copy & paste — that's it
const users = await prisma.user.findMany({
 where: {
 posts: { every: { published: true } }
 }
})

none

None match:

Copy & paste — that's it
const users = await prisma.user.findMany({
 where: {
 posts: { none: { published: true } }
 }
})

is / isNot (1-to-1)

Copy & paste — that's it
const users = await prisma.user.findMany({
 where: {
 profile: { is: { country: 'USA' } }
 }
})

Count Relations

Copy & paste — that's it
const users = await prisma.user.findMany({
 select: {
 name: true,
 _count: {
 select: { posts: true, followers: true }
 }
 }
})
// { name: 'Alice', _count: { posts: 5, followers: 100 } }

Filter counted relations

Copy & paste — that's it
const users = await prisma.user.findMany({
 select: {
 name: true,
 _count: {
 select: {
 posts: { where: { published: true } }
 }
 }
 }
})