Prisma js return 加入字段
Prisma js return join fields
我正在尝试使用 Facebook DataLoader. Let's say I have the following schema (taken from here):
实现批处理和缓存
type Post {
id: ID! @id
title: String!
published: Boolean! @default(value: false)
author: User
comments: [Comment!]!
}
type User {
id: ID! @id
name: String
posts: [Post!]!
comments: [Comment!]!
}
type Comment {
id: ID! @id
text: String!
post: Post!
writtenBy: User!
}
我正在开发一个棘手的解析器,它列出了同一用户在同一 post 下针对给定评论创建的所有评论。要检索单个条目,我会这样:
const fetchCommentsByUserForSamePost = async (commentId: string, userId: string): Promise<Comment[]> => {
const comments = await this.prisma.comment.findOne({ where: { id: commentId } })
.post()
.comments({
where: {
writtenBy: { id: userId }
}
})
return comments;
}
这适用于单个查询,但我想批量查询。在原始 SQL 中,我会在每一行中 return commentId
和 userId
,这样我就可以按这些字段对结果进行分组。但是我找不到一种方法来使用 Prisma return 原始 commentId
来概括查询以使用 commentId
- userId
对列表。
有没有办法用 Prisma 完成这个,或者我遗漏了什么?我知道发出两个请求可以解决这个问题,但这会导致涉及逻辑,而且我宁愿避免进行两次数据库往返。
Prisma 2.0 已经内置了一个专门用于此目的的数据加载器。这意味着您的解析器可能会对 findOne
进行多次调用,但这些调用将在幕后分批处理成一个大的 SQL 查询。所以应该不需要你自己实现这个优化。
我正在尝试使用 Facebook DataLoader. Let's say I have the following schema (taken from here):
实现批处理和缓存type Post {
id: ID! @id
title: String!
published: Boolean! @default(value: false)
author: User
comments: [Comment!]!
}
type User {
id: ID! @id
name: String
posts: [Post!]!
comments: [Comment!]!
}
type Comment {
id: ID! @id
text: String!
post: Post!
writtenBy: User!
}
我正在开发一个棘手的解析器,它列出了同一用户在同一 post 下针对给定评论创建的所有评论。要检索单个条目,我会这样:
const fetchCommentsByUserForSamePost = async (commentId: string, userId: string): Promise<Comment[]> => {
const comments = await this.prisma.comment.findOne({ where: { id: commentId } })
.post()
.comments({
where: {
writtenBy: { id: userId }
}
})
return comments;
}
这适用于单个查询,但我想批量查询。在原始 SQL 中,我会在每一行中 return commentId
和 userId
,这样我就可以按这些字段对结果进行分组。但是我找不到一种方法来使用 Prisma return 原始 commentId
来概括查询以使用 commentId
- userId
对列表。
有没有办法用 Prisma 完成这个,或者我遗漏了什么?我知道发出两个请求可以解决这个问题,但这会导致涉及逻辑,而且我宁愿避免进行两次数据库往返。
Prisma 2.0 已经内置了一个专门用于此目的的数据加载器。这意味着您的解析器可能会对 findOne
进行多次调用,但这些调用将在幕后分批处理成一个大的 SQL 查询。所以应该不需要你自己实现这个优化。