/prisma-6:ensuring-query-type-safetyfrom prisma-6
Use Prisma's generated types, `Prisma.validator`, and `GetPayload` for type-safe queries.
Ships in prisma-6. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Ensure type safety in all database queries using Prisma's generated types, Prisma.validator for custom fragments, and GetPayload for type inference.
Prisma automatically generates TypeScript types from your schema.
Basic Usage:
import { Prisma, User } from '@prisma/client';
async function getUser(id: string): Promise<User | null> {
return prisma.user.findUnique({ where: { id } });
}
With Relations:
type UserWithPosts = Prisma.UserGetPayload<{ include: { posts: true } }>;
async function getUserWithPosts(id: string): Promise<UserWithPosts | null> {
return prisma.user.findUnique({
where: { id },
include: { posts: true },
});
}
Use Prisma.validator to create reusable, type-safe query fragments.
// Query validator
const userWithProfile = Prisma.validator<Prisma.UserDefaultArgs>()({
include: {
profile: true,
posts: {
where: { published: true },
orderBy: { createdAt: 'desc' },
},
},
});
type UserWithProfile = Prisma.UserGetPayload<typeof userWithProfile>;
async function getCompleteUser(id: string): Promise<UserWithProfile | null> {
return prisma.user.findUnique({
where: { id },
...userWithProfile,
});
}
// Input validator
const createUserInput = Prisma.validator<Prisma.UserCreateInput>()({
email: '[email protected]',
name: 'John Doe',
profile: { create: { bio: 'Software developer' } },
});
async function createUser(data: typeof createUserInput) {
return prisma.user.create({ data });
}
// Where clause validator
const activeUsersWhere = Prisma.validator<Prisma.UserWhereInput>()({
isActive: true,
deletedAt: null,
posts: { some: { published: true } },
});
async;
function findActiveUsers() {
return prisma.user.findMany({ where: activeUsersWhere });
}
Infer types from query shapes using GetPayload.
// Complex selections
const postWithAuthor = Prisma.validator<Prisma.PostDefaultArgs>()({
select: {
id: true,
title: true,
content: true,
author: { select: { id: true, name: true, email: true } },
tags: { select: { id: true, name: true } },
},
});
type PostWithAuthor = Prisma.PostGetPayload<typeof postWithAuthor>;
async function getPostDetails(id: string): Promise<PostWithAuthor | null> {
return prisma.post.findUnique({
where: { id },
...postWithAuthor,
});
}
// Nested GetPayload
type UserWithPostsAndComments = Prisma.UserGetPayload<{
include: {
posts: {
include: {
comments: { include: { author: true } };
};
};
};
}>;
const userArgs = Prisma.validator<Prisma.UserDefaultArgs>()({
include: {
posts: {
include: {
comments: { include: { author: true } },
},
},
},
});
async function getUserWithActivity(id: string): Promise<UserWithPostsAndComments | null> {
return prisma.user.findUnique({
where: { id },
...userArgs,
});
}
anyLeverage TypeScript's type system instead of any.
// Type-safe partial selections
function buildUserSelect<T extends Prisma.UserSelect>(
select: T
): Prisma.UserGetPayload<{ select: T }> | null {
return prisma.user.findFirst({ select }) as any;
}
type UserBasic = Prisma.UserGetPayload<{
select: { id: true; email: true; name: true };
}>;
const userSelect = Prisma.validator<Prisma.UserSelect>()({
id: true,
email: true,
name: true,
});
async function getUserBasic(id: string): Promise<UserBasic | null> {
return prisma.user.findUnique({
where: { id },
select: userSelect,
});
}
// Type-safe query builders
class TypedQueryBuilder<T> {
constructor(private model: string) {}
async findMany<A extends Prisma.UserDefaultArgs>(args?: A): Promise<Prisma.UserGetPayload<A>[]> {
return prisma.user.findMany(args);
}
async findUnique<A extends Prisma.UserDefaultArgs>(
args: Prisma.SelectSubset<A, Prisma.UserFindUniqueArgs>
): Promise<Prisma.UserGetPayload<A> | null> {
return prisma.user.findUnique(args);
}
}
const userQuery = new TypedQueryBuilder('user');
const users = await userQuery.findMany({
where: { isActive: true },
include: { posts: true },
});
// Conditional types
type UserArgs<T extends boolean> = T extends true
? Prisma.UserGetPayload<{ include: { posts: true } }>
: Prisma.UserGetPayload<{ select: { id: true; email: true } }>;
async function getUser<T extends boolean>(
id: string,
includePosts: T
): Promise<UserArgs<T> | null> {
if (includePosts) {
return prisma.user.findUnique({
where: { id },
include: { posts: true },
}) as Promise<UserArgs<T> | null>;
}
return prisma.user.findUnique({
where: { id },
select: { id: true, email: true },
}) as Promise<UserArgs<T> | null>;
}
Reusable Type-Safe Includes:
const includes = {
userWithProfile: Prisma.validator<Prisma.UserDefaultArgs>()({
include: { profile: true },
}),
userWithPosts: Prisma.validator<Prisma.UserDefaultArgs>()({
include: { posts: { where: { published: true } } },
}),
userComplete: Prisma.validator<Prisma.UserDefaultArgs>()({
include: { profile: true, posts: true, comments: true },
}),
} as const;
type UserWithProfile = Prisma.UserGetPayload<typeof includes.userWithProfile>;
type UserWithPosts = Prisma.UserGetPayload<typeof includes.userWithPosts>;
type UserComplete = Prisma.UserGetPayload<typeof includes.userComplete>;
async function getUserVariant(
id: string,
variant: keyof typeof includes
): Promise<UserWithProfile | UserWithPosts | UserComplete | null> {
return prisma.user.findUnique({
where: { id },
...includes[variant],
});
}
Type-Safe Dynamic Queries:
type DynamicUserArgs = {
includeProfile?: boolean;
includePosts?: boolean;
includeComments?: boolean;
};
function buildUserArgs(options: DynamicUserArgs): Prisma.UserDefaultArgs {
const args: Prisma.UserDefaultArgs = {};
if (options.includeProfile || options.includePosts || options.includeComments) {
args.include = {};
if (options.includeProfile) args.include.profile = true;
if (options.includePosts) args.include.posts = true;
if (options.includeComments) args.include.comments = true;
}
return args;
}
async function getDynamicUser(id: string, options: DynamicUserArgs) {
return prisma.user.findUnique({
where: { id },
...buildUserArgs(options),
});
}
@prisma/clientPrisma.validator for reusable query fragmentsGetPayloadany; leverage TypeScript's type systemTypeScript Type Safety:
What SKILL.md sets; muted values are Claude Code's defaults
19 skills in this plugin
/configuring-connection-poolsConfigure connection pool sizing for optimal performance. Use when configuring DATABASE_URL or deploying to production./configuring-serverless-clientsConfigure PrismaClient for serverless (Next.js, Lambda, Vercel) with connection_limit=1 and global singleton pattern./configuring-transaction-isolationConfigure transaction isolation levels to prevent race conditions and handle concurrent access. Use when dealing with concurrent updates, financial operations, inventory management, or when users mention race conditions, dirty reads, phantom reads, or concurrent modifications.npx claudepluginhub djankies/claude-configs --plugin prisma-6