DEV Community

loading...
Cover image for NestJS:  Could be amazing someday

NestJS: Could be amazing someday

Jonathan Higger
General purpose musician turned Software Developer. Hopefully this swap of passions will lead me to success!
・6 min read

Introduction

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

Some Context

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.

Why I personally chose nestJS

As an instructor at a software bootcamp, I got to do a solid amount of node debugging, and am pretty good at basic Javascript. I've been playing around with Typescript in coding challenges and haven't had too much difficulty keeping up. It seemed like for myself, my career, and my confidence in life... It was finally time to grow my Type-Chops up and become the developer I was always meant to be.

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!!!!!

What I like about the Framework so far

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

Enter fullscreen mode Exit fullscreen mode

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

But there ARE SOME PROBLEMs

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 
Enter fullscreen mode Exit fullscreen mode

And BAM. Right out of the box you get

  • Postgres connection
  • Beautifully organized migration files
  • Controllers
  • Models
  • 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
Enter fullscreen mode Exit fullscreen mode

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.

  • Migrations
  • 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".

Now that you know the backstory, here are some more fleshed out opinions

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)

Focus on TypeORM support

  • 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

More generally, build opinions into the app

  • 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...

Get them docs to be a bit more thorough

  • Include more post requests
  • Show more workflow on the passport strategy
  • more tutorial sections, potentially some video walkthroughs

Summary

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!

Discussion (1)

Collapse
guledali profile image
guledali • Edited

I agree with this post as well, I have used both django-rest and rails as api-only with jsonapi-serializer.

My opinion is this if your are large team(10+) and the team is split into frontent and backend team. I would still recommend Nest.JS even with it's rough edges as this article mentioned. The reason why is your codebase is fully typed as your application grows and you have models that are interconnected. The only way of surviving in a codebase like that, is with type-safety.

The bad part is as mentioned the fact database not configured when you start a new nest.js application, another one I would say is testing part of it. Looks really rough not as easy writing a controller spec/test with minitest/rspec or unittest/pytest same goes for writing model tests. Just to take an example the setup/before method in rails is usually 1 line or 2 lines of code. In nest.js you almost have to write 10 lines configuring before you can actually write any real testing.

My opinion if they where opinionated on the database layer(database-configured, migration, seed & test-fixtures).The could also simply the testing vastly, either abstract a way the configuring layer portion or design some helper methods around it.