better-drizzle

Relation filters

Filter rows by their related rows with is, isNot, some, every, and none.

Use relation filters when the rows you want depend on their related rows: users with at least one published post, posts whose author is active. They compile to EXISTS subqueries, so no rows are joined into the result. Loading related rows is covered in relations.

where can constrain a row by its relations. The operators depend on the relation's cardinality.

To-one relations: is / isNot

For a "belongs to" / "has one" relation, use is (and isNot) with a nested filter:

const posts = await client.posts.findMany({
	where: {
		author: {
			is: { active: true },
		},
	},
	include: { author: true },
});

To-many relations: some / every / none

For a "has many" relation, choose the quantifier:

// Users who have at least one published post
const authors = await client.users.findMany({
	where: {
		posts: { some: { published: true } },
	},
});

// Users with no unpublished posts
const fullyPublished = await client.users.findMany({
	where: {
		posts: { none: { published: false } },
	},
});
OperatorTrue when…
someat least one related row matches
everyall related rows match
noneno related row matches
isthe single related row matches
isNotthe single related row does not match

Combining relation and scalar filters

Relation filters compose with scalar filters and logical operators like anything else:

const posts = await client.posts.findMany({
	where: {
		AND: [
			{ published: true },
			{
				author: {
					is: { email: { endsWith: '@company.com' } },
				},
			},
		],
	},
	select: {
		id: true,
		title: true,
		author: { select: { id: true, email: true } },
	},
});

On this page