简体   繁体   English

Prisma 读取数据 - 有没有更好的方法来读取嵌套数据?

[英]Prisma Reading data - is there a better way to read nested data?

I currently need to pull lots of nested data to format into a JavaScript object to send into a chart as the data, I am currently allowing ten nested deep, but there might be some cases when this just keeps on going.我目前需要提取大量嵌套数据以格式化为 JavaScript object 以作为数据发送到图表中,我目前允许十个嵌套深度,但在某些情况下这可能会继续进行。

I'm wondering if there is a better way to pull out data like this, I couldn't see anything in the docs, for pulling infinitely or dynamically nested data through the relationships.我想知道是否有更好的方法来提取这样的数据,我在文档中看不到任何内容,用于通过关系提取无限或动态嵌套的数据。

This is what I currently have for example:这就是我目前所拥有的,例如:

const [result] = await prisma.company.findMany({
  where: {
    users: {
      some: {
        email: {
          contains: session.user.email,
        },
      },
    },
  },
  select: {
    name: true,
    users: true,
    id: true,
    profiles: {
      select: {
        id: true,
        name: true,
        image: true,
        category: {
          select: { name: true },
        },
        children: {
          select: {
            id: true,
            name: true,
            image: true,
            category: {
              select: { name: true },
            },
            children: {
              select: {
                id: true,
                name: true,
                image: true,
                category: {
                  select: { name: true },
                },
                children: {
                  select: {
                    id: true,
                    name: true,
                    image: true,
                    category: {
                      select: { name: true },
                    },
                    children: {
                      select: {
                        id: true,
                        name: true,
                        image: true,
                        category: {
                          select: { name: true },
                        },
                        children: {
                          select: {
                            id: true,
                            name: true,
                            image: true,
                            category: {
                              select: { name: true },
                            },
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
});

Have you considered using the include statement instead of select ?您是否考虑过使用include语句而不是select It automatically fetches all non-relation fields, so you just have to manually specify the relation fields.它会自动获取所有非关系字段,因此您只需手动指定关系字段。 The nested reads section in the prisma docs also contains a lot of examples for this. prisma 文档中的嵌套读取部分也包含很多这方面的示例。

In general, if you don't need granular control over which fields of the model get returned from the query, include is the way to go.通常,如果您不需要精细控制从查询返回 model 的哪些字段,则include go 的方法。

Please check out my answer to a very similar question . 请查看我对一个非常相似的问题的回答 I think you might find that solution relevant to your use-case.我认为您可能会发现与您的用例相关的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM