i want to display amount of comments a post has before fetching all the comments

what should I do
model Post {
id Int @id @default(autoincrement())
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// many to one
user User @relation("posts", fields: [userId], references: [id])
userId Int
comments Comment[]

// many to many
likes User[] @relation("likedPosts")
dislikes User[] @relation("dislikedPosts")
}

model Comment {
id Int @id @default(autoincrement())
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// one to many
user User @relation("comments", fields: [userId], references: [id])
userId Int
post Post? @relation(fields: [postId], references: [id])
postId Int?

// many to many
likes User[] @relation("likedComments")
dislikes User[] @relation("dislikedComments")

// one to many self-reference
childComments Comment[] @relation("replies")
// many to one self-reference
parentComment Comment? @relation("replies", fields: [parentCommentId], references: [id])
parentCommentId Int?
}
model Post {
id Int @id @default(autoincrement())
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// many to one
user User @relation("posts", fields: [userId], references: [id])
userId Int
comments Comment[]

// many to many
likes User[] @relation("likedPosts")
dislikes User[] @relation("dislikedPosts")
}

model Comment {
id Int @id @default(autoincrement())
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

// one to many
user User @relation("comments", fields: [userId], references: [id])
userId Int
post Post? @relation(fields: [postId], references: [id])
postId Int?

// many to many
likes User[] @relation("likedComments")
dislikes User[] @relation("dislikedComments")

// one to many self-reference
childComments Comment[] @relation("replies")
// many to one self-reference
parentComment Comment? @relation("replies", fields: [parentCommentId], references: [id])
parentCommentId Int?
}
Solution:
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Jump to solution
3 Replies
Diogo
Diogo2y ago
try this:
const commentsCount = await prisma.post.findMany({
include: {
_count: {
select: { comments: true },
},
},
})
const commentsCount = await prisma.post.findMany({
include: {
_count: {
select: { comments: true },
},
},
})
the output should look like these if im correct:
{ id: 1, _count: { comments: 39 } },
{ id: 2, _count: { comments: 12} },
{ id: 1, _count: { comments: 39 } },
{ id: 2, _count: { comments: 12} },
Solution
Diogo
Diogo2y ago
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
test_1
test_1OP2y ago
thx i love prisma
Want results from more Discord servers?
Add your server