DEV Community

Cover image for NestJS Boilerplate Update: New Features and Improvements
Rodion
Rodion

Posted on

NestJS Boilerplate Update: New Features and Improvements

We are excited to share the latest news about our NestJS Boilerplate! Recently, we’ve received many requests from you and the community regarding the following improvements:

  • Automatic removal of unnecessary functionality during project installation.
  • The ability to add resources (controllers, services, entities, etc.) through the CLI, as this process was previously quite labor-intensive, especially considering the use of hexagonal architecture.

These improvements have now been implemented😊 So let's take a closer look at how users of the boilerplate can take advantage of them.

New CLI for Generating Resources

We’ve added a CLI that allows you to create resources and their properties automatically. To demonstrate its use, let's look at an example of creating endpoints for a blog. We’ll start the backend using the NestJS Boilerplate (in this example, we’ll use MongoDB). If you have any questions or difficulties with deploying the boilerplate itself, we recommend watching the video below, where all the steps are shown step by step. The video also covers the use of the CLI with a relational database.

After launching the boilerplate, we already have routes for Users, Files, Auth, and Home, which can be seen in Swagger. To generate a new resource, we’ll use the following command:

npm run generate:resource:document -- --name=Publication
Enter fullscreen mode Exit fullscreen mode

After executing the command, a new module with the files publication.controller.ts, publication.module.ts, publication.service.ts, as well as nested folders domain and infrastructure, where entities and other necessary files will be stored, will appear in the src/publication folder.

a new module with the files in IDE
The newly created resource will also be added to Swagger, where you can see new endpoints for creating, retrieving all publications with pagination, retrieving one by id, updating, and deleting publications.

a new endpoints for creating in Swagger
At the time of generation, the model contains only the fields id, createdAt, and updatedAt.

the fields of a new model
In our example, each blog publication is expected to have a title and body. To add the "title" field, you need to use the following command:

npm run add:property:to-document
Enter fullscreen mode Exit fullscreen mode

In interactive mode, specify the name of the entity (Entity name) where the property should be added (in our example, it's Publication), enter the Property name, choose the type as string, and add to DTO for further modification. As a result, we see the added property "title" in the dto, domain, schema, and mapper. We then update Swagger and see that the Schema Example Value in both Parameters and Responses now includes the created field {"title": "string"}.

The next field we intend to add is "body". The steps to add it are similar to the previous ones and can be seen in the screenshot.

the steps to add a field
That's it, a new resource and its properties are created, and you can use it. Try working with the new API routes in Swagger.

We’d like to emphasize once again that nestjs-boilerplate uses a hexagonal architectural approach (ports and adapters), which makes it easy to change the database or other infrastructure without affecting the business logic. You can read more about this in the documentation. Therefore, the commands for working with resources will differ when working with a relational database (e.g., PostgreSQL with TypeORM) or with two types of databases simultaneously.

Thus, we created five CRUD endpoints and added the necessary fields without writing a single line of code manually!

Future Plans and Improvements

We continue to work on improving the bc boilerplates. For example, we are currently working on expanding the data types for fields, as only string, number, and boolean are available at the moment. In the future, users will be able to choose not only between primitive types but also references to other entities with the ability to create one-to-one, one-to-many, and many-to-many relations.

We are always open to dialogue and would love to hear your ideas on how we can enhance our boilerplates. And your stars on GitHub inspire us to keep working!

Top comments (0)