DTO projection
What you need to know
A data transfer object (DTO) is a deliberately small object returned across a boundary. A database record may contain costs, supplier notes and unpublished content that a catalog visitor should never receive. Create the public object from an allowlist of fields after applying the access policy.
By the end, you can: return only display fields; identify private fields.
Project a public record (standalone TypeScript helper)
type BookRow = { slug: string; title: string; priceCents: number; supplierNote: string; published: boolean };export function publicSummary(row: BookRow) { if (!row.published) return null; return { slug: row.slug, title: row.title, priceCents: row.priceCents };}Expected behavior (not framework-tested): A published row produces three fields. An unpublished row returns null. Neither result includes supplierNote.
How it works
Authorization answers whether a principal can access a resource; projection determines which fields may cross the boundary. Both are necessary. TypeScript annotations describe expected types, but do not delete properties from a runtime object. When a Server Component passes props to a Client Component, those values may be serialized into data delivered to the browser. Hiding a property in the UI is not a privacy control. Validate external inputs at runtime and use a real session identity for private access, not an asserted user ID from a query or header.
Pass a deliberately small prop (partial Server Component)
import { getPublicBook } from "../../lib/dal";import SaveButton from "./SaveButton";export default async function BookPreview() { const book = await getPublicBook("moss"); if (!book) return <p>Unavailable</p>; return <article><h2>{book.title}</h2><SaveButton slug={book.slug} /></article>;}Expected behavior (not framework-tested): The SaveButton receives only the public slug. SaveButton is a separately supplied client control; the snippet does not implement saving.
Common mistake
const summary: PublicBook = row does not remove extra runtime keys. Avoid spreading whole database rows into a response and avoid logging them. Explicitly construct the output and test the actual keys. A DTO is not a replacement for record-level access checks.
Practice and verification
For a synthetic row with an internal cost, unpublished note and public price, write a three-field projection. Test both a published and unpublished row, and inspect Object.keys of the result. Later inspect actual RSC payloads for forbidden fixture markers.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Data Security · Auth · Components