agents/rules/data-dto-boundaries.md
Impact: CRITICAL
Database types should not leak to the frontend. This has become a popular shortcut in our tech stack, but it's a code smell that creates multiple problems.
Problems with leaking database types:
Incorrect (database types leaking):
// API route returning Prisma types directly
import type { User } from "@prisma/client";
export async function GET(): Promise<User> {
const user = await prisma.user.findFirst();
return user; // Leaks all database fields including sensitive ones
}
// Frontend using Prisma types
import type { User } from "@prisma/client";
function UserProfile({ user }: { user: User }) {
// Component now coupled to database schema
}
Correct (explicit DTOs):
// Define explicit DTOs
interface UserDTO {
id: number;
name: string;
email: string;
// Only fields needed by the client
}
// API route transforms to DTO
export async function GET(): Promise<UserDTO> {
const user = await userRepository.findById(id);
return UserResponseSchema.parse(user); // Validate with Zod
}
// Frontend uses DTO
function UserProfile({ user }: { user: UserDTO }) {
// Component decoupled from database
}
The standard:
Location: All DTOs go in packages/lib/dto/
Naming conventions:
{Entity}Dto (e.g., BookingDto){Entity}With{Relations}Dto (e.g., BookingWithAttendeesDto){Entity}For{Purpose}Dto (e.g., BookingForConfirmationDto){Entity}Dto2, {Entity}DtoForHandler, or other use-case-specific namesEnum/union pattern - use string literal unions to stay ORM-agnostic:
// Good - ORM-agnostic string literal union
export type BookingStatusDto = "CANCELLED" | "ACCEPTED" | "REJECTED" | "PENDING";
// Bad - importing Prisma enum
import { BookingStatus } from "@calcom/prisma/client";
Type safety - never use as any in DTO mapping functions. If types don't align, fix the mapping explicitly.
packages/prisma, repository implementations (packages/features/**/repositories/*Repository.ts), and low-level data access infrastructure.packages/features/** business logic (non-repository), packages/trpc/** handlers, apps/web/**, apps/api/v2/** services/controllers, and workflow/webhook/service layers.Yes, this requires more code. Yes, it's worth it. Explicit boundaries prevent the architectural erosion that creates long-term maintenance nightmares.
Reference: Cal.com Engineering Blog