Good evening gentlemen! Data relational question. ...
# orm-help
t
Good evening gentlemen! Data relational question. I have following structure in DB: Factory - Site - Enterprise If I want to fetch factories of certain enterprise, is this the "best" approach? For me it looks quite ugly.
Copy code
async getFactories(enterpriseId: number): Promise<Factory[]> {
    return (
      await this.prisma.enterprise.findFirst({
        where: {
          id: enterpriseId,
        },
        include: {
          site: {
            include: {
              factory: true,
            },
          },
        },
      })
    ).site
      .map((site) => {
        return site.factory;
      })
      .flat();
  }
a
Hey Topi, Would you mind sharing your Prisma schema? That’s allows helpful for these kids of questions. At first glance, you could try querying through the
Factory
model instead, but I would need to see your schema to know for sure.
t
```model Enterprise {
id Int @id @default(autoincrement()) @map("ID")
name String? @map("Name")
site Site[]
}
model Site {
id Int @id @default(autoincrement()) @map("ID")
name String? @map("Name")
enterpriseId Int @map("EnterpriseID")
enterprise Enterprise @relation(fields: [enterpriseId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "To_Enterprise")
factory Factory[]
@@index([enterpriseId], map: "fki_To_Enterprise")
}
model Factory {
id Int @id @default(autoincrement()) @map("ID")
siteId Int @map("SiteID")
name String? @map("Name")
site Site @relation(fields: [siteId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "To_Site")
}```
a
Would something like this work for you?
Copy code
let result = await prisma.factory.findMany({
    where: {
      site: {
        enterprise: {
          id: enterpriseId
        }
      }
    }
  })
t
Forgot to answer, but thanks! This works and makes life a bit easier đŸ˜‡