[英]how can I either move or calculate the sum of a deeply nested object's properties to the top level in an object array
[英]For Prisma Client join queries is it possible to move deeply nested fields to top level of result?
Prisma 是否能够将嵌套字段从另一个表连接移动到结果的顶层,如扁平化视图? 我想将结果 JSON 放入前端表中,而无需挖掘嵌套对象并构建另一个 object。
例如,我想复制这种行为,我可以从不同的表(用户和学校的列)中挑选列。 目前我使用类似 SQL 的原始查询,但是我想知道是否可以仅使用 Prisma API:
SELECT
u.id
, u.email
, s.school_name
FROM "User" AS u
JOIN "UserSchool" AS us ON us.user_id = u.id
JOIN "School" AS s ON s.id = us.school_id
id | email | school_name
123| student1@email.com | mount high
I want JSON that looks like this:
{
"id": "1",
"email": "student1@email.com",
"school_name": "mount high",
}
如果我在 Prisma 中这样做,我需要将 go 放入多层嵌套对象中,以便在另一个表上获得相同的列名,例如user[user_school][schoo][school_name]
。 这需要额外的工作来遍历我的所有结果,从嵌套的 object 中提取,并构建另一个 object。这个例子还不错,但我有更多的连接和深层嵌套的对象来解决我的实际问题(很多关联/查找表). 我已经尝试使用select
并将其include
在我的连接中,但它们的结构是嵌套的 JSON。
users = await prisma.user.findMany({
include: {
user_school: {
include: {
school: true,
},
},
},
{
"id": "1",
"email": "student1@email.com",
"user_school": [
{
"id": 1,
"user_id": "1",
"school_id": "1",
"school": {
"id": 1,
"school_name": "mountain high",
}
}
],
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.