Prisma特定的一对多关系



我是Prisma的新手,想看看Prisma用户如何进行这样的调用。

我有以下模型:

model ChallengeScore {
id          Int @id @default(autoincrement())
user       User @relation(fields: [username], references: [username])
username     String
challenge Challenge @relation(fields: [challengeAddress], references: [address])
challengeAddress String
isGasScore Boolean
isByteScore Boolean
score       Int
}
model Challenge {
address String @unique
id Int @id @default(autoincrement())
description String
title String
challengeScores ChallengeScore[]
}

我想要的是获取特定用户的特定地址的挑战分数,其中isGasScore为真,分数最低。我想对isByteScore为true和score为最低值做同样的事情。我还想为相同的特定地址获取5个最低分数(对于任何用户),其中isByteScore为真,其中isGasScore为真。

一次通话是否可行?如果是的话,会是什么样子呢?

也什么是最佳实践,我应该只是获取所有的challengeScores,然后过滤出来的方式,我想在前端?我猜第一种选择是更昂贵的db明智吗?

欢迎指教!

谢谢

我会在单个事务中完成,或者Promise.all()也可以

const username = 'U1';
const challengeAddress = 'C1';
const [lowestGas, lowestByte, lowestFive] = await prisma.$transaction([
prisma.challengeScore.findFirst({
where: { username, challengeAddress, isGasScore: true },
orderBy: { score: 'asc' },
}),
prisma.challengeScore.findFirst({
where: { username, challengeAddress, isByteScore: true },
orderBy: { score: 'asc' },
}),
prisma.challengeScore.findMany({
where: {
username,
challengeAddress,
isGasScore: true,
isByteScore: true,
},
orderBy: { score: 'asc' },
take: 5,
}),
]);

是否在前端/后端/查询上进行过滤取决于每个实现的数据量,过滤器的复杂性,可重用性,数据安全性等。

如果是一个小应用程序,在前端进行过滤应该是可以的,但在大型应用程序中就不能很好地扩展了。

最新更新