Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
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.

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 },
                            },
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Have you considered using the include statement instead of 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.

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.

Please check out my answer to a very similar question. I think you might find that solution relevant to your use-case.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!