Hi Dev community!
When you start development of a new project on NestJS you have quite a big range of options: starting from scratch or taking one of multiple boilerplates developed by the community. Starting from scratch is quite a good option, if you’d like to have full control of everything in your project and you have some time to set it all up. But if you have tight deadlines (almost always =) ), you can save quite a lot of time by taking one of the pre-setup solutions: boilerplates.
In the Awesome NestJS list I found 32 repositories under the Boilerplate section by the time this article was written. Quite a lot, huh? How would you choose what to take for your next project?
I must admit here that we are the authors of one of such boilerplates. But I promise, in this article we’ll try to be as objective as we can and try to help you to choose what fits you best.
Pre-filter
Before we start, we need to filter those solutions somehow, because analyzing all 32 of them would take way too much time. We are lazy developers, so, better try to find just decent candidates first for our research.
The main criterias will be:
- Support and Maintenance: if the project is active at all?
- Production readiness: if the project is ready to be used in production now?
- Performance
- Code quality
So finally we chose 5 boilerplates that more or less satisfy our requirements and added them to the table *:
Stars | Contributors | Last Commit Date | Open Issues | Approach | |
---|---|---|---|---|---|
NestJS REST API Boilerplate | 860 | 8 contributors, most of code written by 1 developer, supported and used by Brocoders | 26-03-2023 | 3 | REST API |
Awesome NestJS Boilerplate | 1.6K | 15 contributors, most of code written by 1 developer | 18-03-2023 | 9 | REST API |
NestJS Prisma Starter | 1.7K | 22 contributors, most of code written by 1 | 23-01-2023 | 4 | GraphQL |
Squareboat NestJS Boilerplate | 469 | 10 contributors, most of code written by 1 developer | 01-03-2023 | 4 | REST API |
Nest Hackathon Starter | 279 | 6 contributors, most of code written by 1 developer | 24-01-2023 | 1 | REST API |
* the data from Apr 2023
Now we are going to compare them by features.
Features
NestJS REST API Boilerplate | Awesome NestJS Boilerplate | NestJS Prisma Starter | Squareboat NestJS Boilerplate | Nest Hackathon Starter | |
---|---|---|---|---|---|
Documentation | + | Legacy | + | + | - |
Sign in / Sign up | + | + | + | - | + |
Social sign in | + | - | - | - | - |
Roles | + | + | + | - | - |
Confirm email | + | - | - | - | + |
Forgot password | + | - | - | - | - |
Config Service | + | + | + | - | - |
Mailing | + | - | - | + | + |
Translations | + | + | - | + | - |
Database | + (PostgreSQL + TypeORM or MongoDB) | + (PostgreSQL, TypeORM) | + (PostgreSQL, Prisma) | + (Knex) | + (PostgreSQL, Prisma) |
Migrations | + | + | + | + | + |
Seeding | + | - | + | + | - |
File upload | Local, AWS S3, Can extend | AWS S3, Can not extend | - | - | - |
Tests | + | + | + | + | + |
CI | E2E tests, linter | Linter | Unit tests | - | - |
Swagger | + | + | + | - | + |
Docker | + | + | + | - | - |
Auto updating dependencies | + | - | - | - | + (But without tests. It can break build) |
Code Review
Here I will briefly take a look into the repositories and give my notes about the source code of each selected repository. I would say that I didn’t find any critical issues, just a few things that make sense to keep in mind. Hopefully this code review can help you to avoid some common mistakes in your projects.
Awesome NestJS Boilerplate
There is some performance-related issues:
-
Redundant queries to DB in jwt.strategy which will execute on each request.
const user = await this.userService.findOne({ // FIXME: issue with type casts id: args.userId as never, role: args.role, });
Code
This can reduce performance of your application. -
Tables are not optimized (indexies are not created)
@ManyToOne(() => UserEntity, (userEntity) => userEntity.posts, { onDelete: 'CASCADE', onUpdate: 'CASCADE', }) @JoinColumn({ name: 'user_id' }) user: UserEntity;
Code
On a large dataset the application will work slowly, for example forget posts by user
.
There are imperfections with file upload:
- There is no validation for file size and mime type.
- File uploading follows some bad practices: for each endpoint we need to write weird logic for handling files.
Also the Auth flow is not complete. Email confirmation and Forgot password flows are missing.
NestJS Prisma Starter
-
Performance issues:
The issue is similar to Awesome NestJS Boilerplate: redundant queries to DB.
validateUser(userId: string): Promise<User> { return this.prisma.user.findUnique({ where: { id: userId } }); }
Code
This can reduce performance of your application. Some tables are not optimized (indexies not created): Code. On a large dataset the application will work slowly.
Auth flow is not complete. Email confirmation and Forgot password flows are missing.
File uploading is missing.
Squareboat NestJS Boilerplate
There is only a NestJS + database setup, with its own solutions which will be hard to support in the future.
Nest Hackathon Starter
This boilerplate has good designed schema, with optimization, but still there is problem with performance:
-
jwt.strategy makes redundant queries to DB which will execute on each request.
const user = await this.authService.validateUser(payload);
Bad designed mailing:
-
Own template solution
const mail = confirmMail .replace(new RegExp('--PersonName--', 'g'), name) .replace(new RegExp('--ProjectName--', 'g'), config.project.name) .replace(new RegExp('--ProjectAddress--', 'g'), ...
Code
It will be hard to support. -
A part of templates are stored in a service file:
this.socials = config.project.socials .map( (social) => `<a href="${social[1]}" style="box-sizing:border-box;color:${config.project.color};font-weight:400;text-decoration:none;font-size:12px;padding:0 5px" target="_blank">${social[0]}</a>`, ) .join(''); }
Bad designed service config. Configs are stored in JS object, without env configuration. This can cause problems with security.
Brocoders NestJS Boilerplate
I wouldn’t say too much about this Boilerplate as I wrote most of it and it would be difficult to criticise it :-)
But I’d like to note that I mostly followed official documentation of REST API and NestJS techniques. I tried to focus on performance and long support along with e2e tests.
It includes all necessary features for auth (sign in, sign up, social sign up, confirm email, forgot password and roles) and file uploading (this allows one endpoint for file uploading and then attach it to any other entity). I decided to have it from the box, because all the projects we started required these features.
Read more about Brocoders NestJS Boilerplate on Dev.to
Conclusion
Actually from the code review standpoint all of the selected solutions are good. Nothing critical, any of such issues can be easily resolved in your own implementation. However, it makes sense to select the starter kit knowing all the differences between solutions and make your choice consciously.
Full credits to Vlad Shchepotin for this article and the Boilerplate!
Top comments (1)
Hi Rodion and Vlad, great article on NestJS boilerplates!
We'd be really interested to hear your thoughts on our open-source generative toolkit, Traxion. It's designed to accelerate NestJS projects while maintaining full control over your code, offering features such as data management with Prisma, instant GraphQL API, Role-Based Access Control, and official packages including Dev-Kit, Nest-Authentication, Nest-Authorization, and Nest-Utilities.
We'd appreciate it if you could give Traxion a try and share your feedback with us. Your insights can help us improve Traxion and make it an even better tool for the NestJS community. Check it out at github.com/tractr/traxion and let us know your thoughts! Cheers! 😊