DEV Community

efi shtain
efi shtain

Posted on • Originally published at itnext.io on

CRUD API in 3 lines using NestJS


Photo by Kier In Sight on Unsplash

If it sounds like a clickbait, looks like a clickbait and smells like a clickbait, it must be… well it’s not.

Here are the 3 lines to create a webserver with a crud api for a candy store which sell chocolate

I admit it’s not a full application, but If you look closely, I said an API.

Why did I write this article ?

Working with NestJS is super nice. It has an opinionated structure (that you can like or dislike), but you have to agree, it reduces a lot of the time wasted tinkering on directories structure, setting up tests, routes, database connections and so on, everything is solved for the average use case.

It even comes with a great extendable CLI to facilitate everything using generators.

But even with the CLI, every time I needed some kind of boiler plate code for a crud API I had to do the same stuff. Create a module, controller and a service.

nest g module chocolate

nest g controller chocolate

nest g service chocolate

Then I had to add the crud APIs which are the same for each resource. And finally, If I’m really committed, I had to have some DTOs and Entities definitions.

Even before I wrote any actual logic related to my application, I had to do all of that. So the generators are amazing, but not enough.

But I was wrong! there are more generators, mainly the resource generator

Using the following line of bash

nest g resource chocolate

I get EVERYTHING done for me in one command!


All resources created using generators

References:

Documentation | NestJS - A progressive Node.js framework


Top comments (0)