m
r
@Markey Boi 👋 I don’t think this would be possible with Prisma. There’s a lot of raw query stuff like
EXISTS
and custom fields like difference of
COUNT
, so a raw query would be a better fit in this case.
m
Ok, if we ignore the exists stuff for a minute is there a non-raw query way to findMany of an entity and include the count of 2 things it's related to (in my case I want to findMany posts with the count of its upvotes and downvotes)?
r
Does your schema look something like this?
Copy code
model Post {
  id Int @id
  upvotes Int @default(0)
  downvotes Int @default(0)
}
m
No, I have upvotes and downvotes as separate entities. I discovered the _count key for include so I think I figured out a workable solution which looks like
Copy code
const posts = await this.prisma.post.findMany({
	include: {
		_count: {
			select: {
				upvote: true,
				downvote: true
			}
		},
		upvote: {
			where: {
				accountId: accountId
			}
		},
		downvote: {
			where: {
				accountId: accountId
			}
		}
	}
});
Thanks for the help though. Also I should ask, when passing data like this (the accountId), does prisma do any sort of input escaping. Like in TypeORM you could do .setParamater which would make sure the parameter was escaped.
r
Prisma always escapes strings by default.
The above should work fine. It will get all upvotes/downvotes of a given post and if the upvotes and downvotes match the given account.