NestJS is a nice little framework for using typescript with node. It enforces opinionated design patterns that are supposed to be familiar to Java Programmers who have worked with Spring. It's built off of Node-TS and can be configured a whole shit-ton of ways. I'm personally making a postgres database using TypeORM and I'm not hating the process.
In this article we'll get into
- Who nest might be good for
- What I like about Nest so far
- What I don't like about Nest so far
- Why I may or may not stick with nest
The name's Jon! I've been programming for about 2 years, I started with Rails for the backend, have done a few node build and burns (admittedly not a whole lot) and then found out I had the opportunity to work on and design a big project. Because I'm a Masochistic idiot, constantly searching to grow my brainpower, I decided I definitely wanted to do my backend in TypeScript and further more in an opinionated Typescript framework like NestJS. That's oversimplified, let us un-oversimplify that in the next section here.
Coming from a rails background, I was convinced that I would learn better practices faster diving head first into a more OPINIONATED framework. Nest seemed to fit the bill, so I said screw it, lets go!!!!!
On first glance there was one thing that attracted me to the framework.... CLI tools!
In Rails, if you are familiar, you can effectively create a an entire API in about 10 minutes or less because of how well their CLI tools work! NestJS has them too! Just run
nest generate resource fill out the menu options that come up in your terminal, and WHAM you get a shit ton of files made for you.
for example if you create a users resource you will get a file structure something like this
- src - users - entities --user.entity.ts - dtos --create-user.dto.ts --update-user.dto.ts -- user.module.ts -- user.service.ts -- user.controller.ts
This is fucking fantastic! Less room for error, less thinking, bada boom, badabing, WHAM.
As I started to get deeper into it though, I found that my appreciation actually came more from being forced to learn some new design patterns. I was forced to learn about data transfer objects, services, (I already knew about controllers), modules, and while I definitely don't fully understand all of the code, it's nice to see a good design in practice. The code that I wind up writing truly does feel MUCH more organized than if I had not used nest.
I regularly seem to encounter things like "OHHHHH thats why they did this this way, that makes sense"
Also the structure of the Nest Docs is BEAUTIFUL. The UI is great to look at, and with the content that they do cover they do a very good job of.
in summary of the things I like we have
- CLI Tools
- Forced to learn good design patterns
- Very tidy project structure
- Well made docs
OK so I wasn't 100% honest about the CLI tools. They are great, but they are also flawed deeply in my opinion. Here is the thing, maybe I'm a spoiled brat, but in Rails you can literally set up a one to many relationship in under a minute by typing in
rails g scaffold classroom name:string rails g scaffold teacher name:string classroom:references
And BAM. Right out of the box you get
- Postgres connection
- Beautifully organized migration files
- its all done for you, you barely even have to know how to code
you can immediately spin up your server and start seeding classrooms and teachers and are good to go.
but when you type in
nest g resource classroom
here's what you get
- A controller
- A Service which is what your controller connects to that actually manages database connections
- some types that aren't filled in that allow you to move data around
- A Module that organizes it
here's what is missing though and its a BIG FUCKING THING
- TypeORM integration (Or some other library like sequeliz)
So now what you wind up with is 5-8 different files that you have to go in and do a TON of manual configuration. To give you an idea of what the steps are like. They are something like this.
- install typeorm
- install types for typeorm
- connect typeorm to your classroom entity by specifying it as a typeorm entity
- create a classroom repository in the constructor arguments for the classroom service
- use the classroom repository to update your classroom services methods
- update your imports for your classroom module to include typeorm
- update the classroom entity to include all of the data fields that you need
- update the create-classroom dto to include all of the data fields that you need to create a classroom
- update the update-classwroom dto to include all of the data fields that you need to update a classroom.
THENNNNNNNN you're good to go and start testing!!!
Ok that was a lot, and I don't know what other people's development experiences are like, but to me it seems like the beauty of an opinionated framework is the ability to NOT have to do all that. On top of this here are some other things you no longer get out of the box.
- The Ability to seed things
- Validations ( they are easy to generate in rails I think but I'm not sure)
Also another quick complaint. While the docs are fucking beautiful, and the tone is great for beginners, they need to be more thorough. I had to go down ton's of stack overflow rabbit holes just to find information on for example "What do I actually put in my post request for a many to many relationship".
It feels like all of NestJS's problems can be boiled down to this, and I totally could be wrong. NEST NEEDS STRONGER OPINIONS! From everything I read, the Node ecosystem is severely lacking a dominate "batteries-included" framework, and from what I can tell NestJS has the real potential to be the number one contender. But they CANNOT do that if the learning process, or the developing process is this tedious.
Here's some things Nest needs to do before I'd wan't to refer everybody to it (Not that I would reccomend against it now)
- The cli generators need to connect your entities to the database for you
- migrations and transactions from typeorm should be built right into nestJS, and documented directly on the nestJS website on how to use (So far I find NestJS docs 10,000 times better than typeorm's or most other libraries in how they are written).
- (Bonus) it would be great if you could specify datafeilds in the cli generators
- When I do
nest new project_name, I shouldn't have to go down the rabbit-hole and read all the docs to get everything hooked up with passportjs, class-validator, class-transformer. Make some opinions on what people will use all the time for a standard REST API, and build them into the app template, the cli-tools etc...
- Include more post requests
- Show more workflow on the passport strategy
- more tutorial sections, potentially some video walkthroughs
I genuinely think Nest is great. They do a great job with their docs, and if everything discussed above was actually improved there is no doubt I would enjoy Nest more than rails personally. Maybe I'll do a follow up article in 5 years haha!