在 Prisma 中按多列排序

Ordering by multiple columns in Prisma

我知道目前 Prisma 不支持按多个标量字段排序,(请参阅此问题:https://github.com/prisma/prisma/issues/62)。 但是,我想知道是否有人在不使用 executeRaw mutation (raw SQL) 的情况下找到了解决此问题的解决方案,因为我的代码中有很多地方我需要按多个字段排序,我不想在这么多地方使用 executeRaw。 我将不胜感激任何建议。谢谢!

我认为没有解决方案,在我的项目中,我需要随机顺序,increment/decrement,聚合...最后使用原始。

从 Prisma 2.4 开始,这基本上可以通过在 orderBy 中使用数组来实现:

const users = await prisma.user.findMany({
   select: {
      email: true,
      role: true,
   },
   orderBy: [
      {
         email: 'desc',
      },
      {
         role: 'desc',
      }
   ],
})

在文档中查找更多信息:https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#sort-user-by-multiple-fields---email-and-role