In case you missed it, check out Part 1 where I talk about my Q3 goals and explain how this series works.
Now, here’s Part 2!
During the week of July 21st, my goal was to complete the following:
✅ Completed Goal
I put in extra hours this weekend to get back on track for my plan so I completed both of the course sections.
🤔 What did I do?
I learned about validation, error handling and data persistence. I also integrated TypeORM and a PostgresSQL database.
Again, I have experience in these areas but here are some things that stuck out this time around:
A data transfer object is essentially the definition of a piece of data, which may be used across multiple layers of your application.
For instance, in the Nest.js application I’m building, there is a /tasks controller. On the controller, we have a POST endpoint on the root where you can create a task. The controller utilizes the service (where the logic actually lives). Since these both accept the same parameter when creating a task, this is perfect use case for a DTO. We create a create-task.dto.ts which looks like this:
Then, import it wherever we need it within the application. Viola!
One comment the instructor made was to define it using ES6 Classes instead of a TypeScript interface, so the shape would be preserved after the TS complication to JS. This also makes code easier to maintain in the future.
Then in our controller, we can tell Nest.js to use a Validation Pipe like so:
Below are some things that were difficult for me and that I’m still wrapping my head around.
After adding TypeORM and PostgresSQL, we added a “repository” layer. The documentation states:
Repository is supposed to work with your entity objects. Find entities, insert, update, delete, etc.
That’s all for my update! Thanks for reading and catch ya next week! 👋