跳至内容

选择字段

默认情况下,当查询返回记录(而不是计数)时,结果包含**默认选择集**

  • **所有**在 Prisma 模式中定义的标量字段(包括枚举)
  • **无**在 Prisma 模式中定义的关系字段

要自定义结果

  • 使用select指定要包含在结果中的字段子集。
  • 使用include指定要包含在结果中的关系字段子集。

示例模式和提供程序

所有示例都基于以下 Prisma 模式

展开以查看模式(schema.prisma
prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "dart run orm"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id           Int      @id @default(autoincrement())
  email        String   @unique
  name         String?
  age          Int?
  role         Role     @default(USER)
  posts        Post[]
  Profile      Profile?
  country      String?
  city         String?
  profileViews Int      @default(0)
}

model Post {
  id         Int        @id @default(autoincrement())
  title      String
  content    String?
  published  Boolean    @default(false)
  author     User?      @relation(fields: [authorId], references: [id])
  authorId   Int?
  categories Category[]
  views      Int        @default(0)
  likes      Int        @default(0)
  tags       String[]   @default([])
}

model Profile {
  id     Int     @id @default(autoincrement())
  bio    String?
  user   User    @relation(fields: [userId], references: [id])
  userId Int     @unique
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String @unique
  posts Post[]
}

enum Role {
  USER
  ADMIN
}
展开以查看提供程序(prisma.dart
dart
import 'dart:async';

import 'prisma/generated_dart_client/client.dart';

/// Create a new instance of PrismaClient
final _client = PrismaClient();

/// Provide a PrismaClient instance to a function.
///
/// Wrapped in a function to ensure that the instance is diconnected
/// after the function is done.
FutureOr<T> providePrisma<T>(
    FutureOr<T> Function(PrismaClient prisma) main) async {
  try {
    return await main(_client);
  } finally {
    await _client.$disconnect();
  }
}

返回默认选择集

以下查询返回默认选择集(所有标量字段,无关系)

dart
final user = await prisma.user.findUnique(
  where: UserWhereUniqueInput(id: 1),
);
展开以查看结果
json
{
  "id": 1,
  "name": "Seven",
  "email": "seven@odroe.com",
  "role": "ADMIN"
}

选择特定字段

使用select返回字段的有限子集,而不是所有字段。以下示例仅返回emailname字段

dart
final user = await prisma.user.findUnique(
  where: UserWhereUniqueInput(email: "seven@odroe.com"),
  select: UserSelect(
    name: true,
    email: true,
  ),
);
展开以查看结果
json
{
  "name": "Seven",
  "email": "seven@odroe.com"
}

包含关系并选择关系字段

使用include返回关系字段的子集。以下示例返回User记录的name字段,以及Post关系的title字段

dart
final users = await prisma.user.findMany(
  select: UserSelect(
    name: true,
    posts: PrismaUnion.$2(
      UserPostsArgs(
        select: PostSelect(title: true),
      ),
    ),
  ),
);
展开以查看结果
json
{
  "name": "Seven",
  "posts": [
    {
      "title": "My first blog post"
    },
    {
      "title": "My second blog post"
    }
  ]
}

以下查询在include中使用select,并返回所有用户字段和每个帖子的title字段

dart
final users = await prisma.user.findMany(
  include: UserInclude(
    posts: PrismaUnion.$2(
      UserPostsArgs(
        select: PostSelect(title: true),
      ),
    ),
  ),
);
展开以查看结果
json
{
  "id": 1,
  "name": "Seven",
  "email": "seven@odroe.com",
  "role": "ADMIN",
  "posts": [
    {
      "title": "My first blog post"
    },
    {
      "title": "My second blog post"
    }
  ]
}

有关查询关系的更多信息,请参阅以下文档

关系计数

您可以includeselect关系计数以及字段 - 例如,用户的帖子计数。

在 BSD-3-Clause 许可证下发布