Test, inspect, and ship

Infer select and insert types

Derive database row and insert types from the schema without confusing those types with validated request input or public responses.

8 minute lesson

~~~

The schema already knows its row shapes. Rewriting the same TypeScript interfaces by hand creates another source of truth.

Use a table’s inferred select and insert types for internal database boundaries. Select types include returned columns. Insert types reflect generated and defaulted fields. They still do not validate JSON at runtime.

export type Note = typeof notes.$inferSelect
export type NewNote = typeof notes.$inferInsert

A public API response may deliberately omit authorId or add a computed URL. Define that contract separately instead of returning database rows by accident.

Likewise, an HTTP create-note input should not include generated IDs or trusted ownership fields. Validate the request schema, then map it into NewNote with the server-owned author ID.

Create Note, NewNote, CreateNoteInput, and PublicNote types for the project. Add one private database field and prove it does not silently appear in the public mapping.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →