过滤和排序
Prisma Dart 客户端支持使用 where 查询选项进行过滤,以及使用 orderBy 查询选项进行排序。
过滤
Prisma Client 允许您根据模型字段的任意组合(包括相关模型)过滤记录,并支持各种过滤条件。
以下查询
- 返回所有具有以下条件的 User 记录:
- 电子邮件地址以
odore.com
结尾 - 至少有一篇已发布的帖子(关联查询)
- 电子邮件地址以
- 返回所有 User 字段
- 包括所有相关的 Post 记录,其中
published
等于true
await prisma.user.findMany(
where: UserWhereInput(
email: PrismaUnion.$1(
StringFilter(endsWith: PrismaUnion.$1('@odroe.com')),
),
posts: PostListRelationFilter(
some: PostWhereInput(
published: PrismaUnion.$2(true),
),
),
),
include: UserInclude(
posts: PrismaUnion.$2(
UserPostsArgs(
where: PostWhereInput(
published: PrismaUnion.$2(true),
),
),
),
),
);
过滤空字段
以下查询返回所有 content
字段为 null
的帖子
await prisma.post.findMany(
where: PostWhereInput(
content: PrismaUnion.$2(
PrismaUnion.$2(const PrismaNull()),
),
),
);
过滤非空字段
以下查询返回所有 content
字段不为 null
的帖子
await prisma.post.findMany(
where: PostWhereInput(
content: PrismaUnion.$1(
StringNullableFilter(
not: PrismaUnion.$2(
PrismaUnion.$2(const PrismaNull()),
),
),
),
),
);
过滤关联关系
Prisma Client 支持过滤关联记录。例如,在以下模式中,一个用户可以拥有多个博客帖子
model User {
id Int @id @default(autoincrement())
name String?
email String @unique
posts Post[] // User can have many posts
}
model Post {
id Int @id @default(autoincrement())
title String
published Boolean @default(true)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
User 和 Post 之间的一对多关系允许您根据用户的帖子查询用户 - 例如,以下查询返回所有至少有一篇帖子 (some
) 浏览量超过 10 次的用户
await prisma.user.findMany(
where: UserWhereInput(
posts: PostListRelationFilter(
some: PostWhereInput(
views: PrismaUnion.$1(
IntFilter(gt: PrismaUnion.$1(10)),
),
),
),
),
);
您还可以根据作者的属性查询帖子。例如,以下查询返回所有作者的 email
包含 odroe.com
的帖子
await prisma.post.findMany(
where: PostWhereInput(
author: PrismaUnion.$2(
PrismaUnion.$1(
UserWhereInput(
email: PrismaUnion.$1(
StringFilter(contains: PrismaUnion.$1('odroe.com')),
),
),
),
),
),
);
过滤标量列表/数组
标量列表(例如 String[]
)有一组特殊的过滤条件 - 例如,以下查询返回所有 tags
数组包含 databases
的帖子
await prisma.post.findMany(
where: PostWhereInput(
tags: StringNullableListFilter(
has: PrismaUnion.$1('databases'),
),
),
);
不区分大小写的过滤
不区分大小写的过滤是 PostgreSQL 和 MongoDB 提供程序的一项功能。MySQL、MariaDB 和 Microsoft SQL Server 默认情况下不区分大小写,并且不需要 Prisma Client 功能即可实现不区分大小写的过滤。
要使用不区分大小写的过滤,请将 mode
属性添加到特定过滤器并指定 insensitive
await prisma.user.findMany(
where: UserWhereInput(
email: PrismaUnion.$1(
StringFilter(
endsWith: PrismaUnion.$1('@odroe.com'),
mode: QueryMode.insensitive,
),
),
name: PrismaUnion.$1(
StringNullableFilter(
equals:
PrismaUnion.$1('Seven Odroe'), // Default mode
),
),
),
);
更多信息请查看 👉 不区分大小写 官方文档。
排序
使用 orderBy
对记录列表或嵌套记录列表按特定字段或字段集进行排序。例如,以下查询返回所有按角色和名称排序的 User 记录,以及每个用户的帖子按标题排序。
await prisma.user.findMany(
orderBy: PrismaUnion.$1([
UserOrderByWithRelationInput(role: SortOrder.desc),
UserOrderByWithRelationInput(
name: PrismaUnion.$1(SortOrder.desc),
),
]),
include: UserInclude(
posts: PrismaUnion.$2(
UserPostsArgs(
orderBy: PrismaUnion.$2(
PostOrderByWithRelationInput(title: SortOrder.desc),
),
select: PostSelect(title: true),
),
),
),
);
按关联关系排序
您还可以按关联关系的属性排序。例如,以下查询按作者的电子邮件地址对所有帖子进行排序。
await prisma.post.findMany(
orderBy: PrismaUnion.$2(
PostOrderByWithRelationInput(
author: UserOrderByWithRelationInput(
email: SortOrder.asc,
),
),
),
);
按关联关系聚合值排序
您可以按**关联记录的数量**进行排序,例如,以下查询按相关帖子的数量对用户进行排序。
await prisma.user.findMany(
take: 10,
orderBy: PrismaUnion.$2(
UserOrderByWithRelationInput(
posts: PostOrderByRelationAggregateInput(
$count: SortOrder.desc,
),
),
),
);