NestJSREST APIs & DTOsLesson 45 / 69

CRUD for books

Code Lab45 min

CRUD for books

Now the real thing — five endpoints over a real table:

@Post() create(@Body() dto: CreateBookDto) { … }
@Get() list(@Query('author') author?: string) { … }
@Get(':id') findOne(@Param('id') id: string) { … }
@Patch(':id') update(@Param('id') id: string, @Body() dto: UpdateBookDto) { … }
@Delete(':id') remove(@Param('id') id: string) { … }

Notice what repeats: the service does CRUD, the controller translates HTTP. Once you feel the repetition, you have understood Nest — everything else is the same pattern wearing accessories (relations, auth, transactions).

This is your first code lab: build it end to end, from SQL table to JSON.

Exercise

Build CRUD for books against this app's database. All five endpoints, validation on create/update, 404 on unknown id.

Practice this lesson in the IDE

Open the NestJS practice workspace, edit the starter code, and run your work in the browser.

Open IDE and practice