R
R
Roman Mirilaczvili2021-10-14 11:46:51
ORM
Roman Mirilaczvili, 2021-10-14 11:46:51

How to submit an SQL query with multiple JOINs in Prisma?

There is a request like this:

SELECT
  bp.id AS bp_id,
  streams.id AS stream_id
FROM business_profiles AS bp
INNER JOIN accounts AS acc on acc.business_profile_id = bp.id
INNER JOIN streams ON streams.stream_owner_id = acc.id
WHERE streams.id in (?);


I'm trying to present it in Prisma ORM, but something doesn't work out very well (I'm working with it for the first time). Help advice.

shortened version of schema.prisma
model Account {
  id                  BigInt    @id @default(autoincrement())
  business_profile    BusinessProfile @relation(fields: [business_profile_id], references: [id])
  business_profile_id BigInt
  ...
  streams             Stream[]
}

model Stream {
  id                BigInt   @id @default(autoincrement())
  stream_owner_id   BigInt
  account           Account @relation(fields: [stream_owner_id], references: [id])
  ...
}

model BusinessProfile {
  id          BigInt   @id @default(autoincrement())
  ...
  accounts    Account[]
}

My attempts
const bpIDs = await this.client.businessProfile.findMany({
      include: {
        accounts: {
          include: {
            streams: true,
          },
        },
      },
      where: {
        accounts: {
          
        },
      },
      // select: {
      // 	accounts: {
      // 		select: {
      // 			streams: {
      // 				where: {
      // 					id: {
      // 						in: streamIDs,
      // 					},
      // 				},
      // 			},
      // 		},
      // 	},
      // },
    });
    console.log(bpIDs);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question