Skip to content
VibORM
Esc
navigateopen⌘Jpreview
On this page

To-One Relation Filters

Filter by singular relation slots using is and isNot

is

Match records where the related record matches conditions:

// Posts by admin author
const adminPosts = await client.post.findMany({
  where: {
    author: {
      is: { role: "ADMIN" },
    },
  },
});

// Users with verified profile
const verifiedUsers = await client.user.findMany({
  where: {
    profile: {
      is: { verified: true },
    },
  },
});

isNot

Exclude records where the related record matches conditions:

// Posts not by banned authors
const posts = await client.post.findMany({
  where: {
    author: {
      isNot: { status: "BANNED" },
    },
  },
});

Null Checks

Null checks are available only when the to-one relation is optional. Bare null is shorthand for { is: null }.

// Users with a profile
const withProfile = await client.user.findMany({
  where: {
    profile: {
      isNot: null,
    },
  },
});

// Users without a profile
const withoutProfile = await client.user.findMany({
  where: {
    profile: { is: null },
  },
});

// Equivalent shorthand
const alsoWithoutProfile = await client.user.findMany({
  where: { profile: null },
});

An empty optional relation is not an orphaned membership. An orphaned membership has non-empty stored identity whose target row is missing; that is invalid data and relation projection fails rather than returning null.

Direct polymorphic relations are variant-aware: target filters include a type, while optional-presence checks accept bare null, { is: null }, and { isNot: null }. Singular polymorphic inverse slots use the ordinary forms shown above. See Polymorphic Relations.

Nested Conditions

Filter deeply nested relations:

// Posts by authors from specific organization
const posts = await client.post.findMany({
  where: {
    author: {
      is: {
        organization: {
          is: { name: "Acme Corp" },
        },
      },
    },
  },
});

// Comments on posts by verified authors
const comments = await client.comment.findMany({
  where: {
    post: {
      is: {
        author: {
          is: { verified: true },
        },
      },
    },
  },
});

Combined Filters

// Posts by active admin authors
const posts = await client.post.findMany({
  where: {
    author: {
      is: {
        role: "ADMIN",
        active: true,
      },
    },
    published: true,
  },
});

// Users with premium subscription
const premiumUsers = await client.user.findMany({
  where: {
    subscription: {
      is: {
        plan: "PREMIUM",
        active: true,
        expiresAt: { gt: new Date() },
      },
    },
  },
});

Examples

Find Posts by Author Email

async function getPostsByAuthorEmail(email: string) {
  return client.post.findMany({
    where: {
      author: {
        is: { email },
      },
    },
    include: { author: true },
  });
}

Was this page helpful?