Skip to content
VibORM
Esc
navigateopen⌘Jpreview
On this page

Selecting Fields

Control which fields are returned from queries using select and include

select

Return only specified fields:

const users = await client.user.findMany({
  select: {
    id: true,
    email: true,
    name: true,
  },
});
// Type: { id: string; email: string; name: string }[]

include

Return all scalars plus specified relations:

const users = await client.user.findMany({
  include: {
    posts: true,
    profile: true,
  },
});
// Type: { id: string; email: string; ...; posts: Post[]; profile: Profile | null }[]

omit

omit is the inverse of select — every readable scalar except the ones you name. It cannot be combined with select, and it composes with include:

const users = await client.user.findMany({
  omit: { passwordHash: true },
  include: { posts: { omit: { draftBody: true } } },
});

See Omitting fields for the full surface, including the client-level default and how the three layers of omission rank.

select vs include

Aspect select include
Base scalars Only selected All scalars
Relations Must select Add to base
Default Nothing selected All scalars

Nested Selection

Select fields within relations:

const users = await client.user.findMany({
  select: {
    id: true,
    email: true,
    posts: {
      select: {
        id: true,
        title: true,
      },
    },
  },
});
// Type: { id: string; email: string; posts: { id: string; title: string }[] }[]

Nested Include

Include relations within relations:

const users = await client.user.findMany({
  include: {
    posts: {
      include: {
        comments: true,
      },
    },
  },
});

Mixing select and include

Use select within include:

const users = await client.user.findMany({
  include: {
    posts: {
      select: {
        id: true,
        title: true,
      },
    },
  },
});

Use include within select:

const users = await client.user.findMany({
  select: {
    id: true,
    email: true,
    posts: {
      include: {
        tags: true,
      },
    },
  },
});

Filtering Relations

Apply filters to included relations:

const users = await client.user.findMany({
  include: {
    posts: {
      where: { published: true },
      orderBy: { createdAt: "desc" },
      take: 5,
    },
  },
});

Count Relations

Get relation counts:

const users = await client.user.findMany({
  include: {
    _count: {
      select: {
        posts: true,
        followers: true,
      },
    },
  },
});
// user._count.posts: number
// user._count.followers: number

Examples

Public Profile

async function getPublicProfile(userId: string) {
  return client.user.findUnique({
    where: { id: userId },
    select: {
      id: true,
      name: true,
      bio: true,
      avatarUrl: true,
      createdAt: true,
      posts: {
        where: { published: true },
        select: {
          id: true,
          title: true,
          createdAt: true,
        },
        orderBy: { createdAt: "desc" },
        take: 10,
      },
      _count: {
        select: { posts: true, followers: true },
      },
    },
  });
}

API Response

async function getPostForAPI(postId: string) {
  const post = await client.post.findUnique({
    where: { id: postId },
    select: {
      id: true,
      title: true,
      content: true,
      createdAt: true,
      author: {
        select: {
          id: true,
          name: true,
          avatarUrl: true,
        },
      },
      tags: {
        select: { name: true },
      },
      _count: {
        select: { comments: true, likes: true },
      },
    },
  });
  
  return post;
}

Type Safety

Return types are automatically inferred:

// Full record
const user = await client.user.findUnique({ where: { id } });
// Type: User | null

// Selected fields
const user = await client.user.findUnique({
  where: { id },
  select: { id: true, email: true },
});
// Type: { id: string; email: string } | null

// With relations
const user = await client.user.findUnique({
  where: { id },
  include: { posts: true },
});
// Type: (User & { posts: Post[] }) | null

Was this page helpful?