As NestJS is growing faster in demand its development time of nestjs app should a be also growing faster.
Let’s imagine a real-world scenario, where we need to expose CRUD endpoints for 2 entities, let’s say Userand Product entities. Following the best practices, for each entity we would have to perform several operations, as follows:
- Generate a module (
nest g mo) to keep code organized and establish clear boundaries (grouping related components)
- Generate a controller (
nest g co) to define CRUD routes (or queries/mutations for GraphQL applications)
- Generate a service (
nest g s) to implement & isolate business logic
- Generate an entity class/interface to represent the resource data shape
- Generate Data Transfer Objects (or inputs for GraphQL applications) to define how the data will be sent over the network
That takes a hour to do it.
To help speed up this repetitive process, NestJS CLI now provides a new generator (schematic) that automatically generates all the boilerplate code to help us avoid doing all of this, and make the developer experience much simpler.
install the latest version of
once done with the installation just run this command
nest g resource modulename
This command with generate some question like this gif
Select your preference and it will create all the needs files.
What you saved an hour .Isn’t it amazing.I loved It….
one life saving hint :->Don’t say this feature to your managers haa haa…
Likewise, if you want to generate resolvers for a GraphQL application, simply select the
GraphQL (code first) (or
GraphQL (schema first)) as your transport layer. In this case, NestJS will generate a resolver class instead of a REST API controller.
Hint To avoid generating test files, you can pass the
--no-spec flag, as follows:
nest g resource users --no-spec
resource schematic helps speed up and improve the developer experience tremendously, letting us build our Nest applications that much faster.