
Lesson Description
The "Create Habit Controller" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:
Scott explains how to create a habit controller by working with database tables, managing relations like habit tags, and using transactions to maintain integrity. He also shows how to create a habit, handle errors, validate inputs with schemas, and connect the controller to the habit route.
Transcript from the "Create Habit Controller" Lesson
[00:00:00]
>> Speaker 1: So what we want to do is go to our Controllers folder here, make a new controller We'll say HabitController.ts and we'll get to work on this
[00:00:00]
We'll say import type Response from Express, so we get that, and we're gonna use our authenticated Request from our middleware instead of Express-like
[00:00:00]
You need to get our database in here because we're gonna be doing database things—that's the whole point of the server, or the whole point of the API, I should say
[00:00:00]
Then we're going to import the habits table And because of relations, we might have the entries table because you can create entries for a habit
[00:00:00]
You might also have habit tags if someone wants to create a tag or associate a habit with a tag We're allowing them to do that through the habit resource; we might also allow that through the tag resource because it's a many-to-many relationship, so it's denormalized
[00:00:00]
You can do either on either side: you can create a tag and assign it to these habits, or create a habit and assign these tags, so either way
[00:00:00]
And then some helper methods from Drizzle ORM—we'll get the one that we used before, equals, we'll get an ascending order for sorting, we'll get an in-array for checking
[00:00:00]
OK, so the first one we want to do is the C part of CRUD, which is create We'll export a function here called createHabit It's going to take a request, but it's going to be an authenticated request
[00:00:00]
We'll take a response from Express We're going to do a try-catch block We'll destructure the payload from the body: we'll get the name, description, frequency, target count, and any tag IDs
[00:00:00]
If you have some tag IDs, you can send them up, and we'll associate this new habit with these tags We'll get the user ID using req.user.id
[00:00:00]
What's new here, if you've never worked with databases, is we're going to make a transaction on the database What is a transaction
[00:00:00]
If you have to write to the database more than once for this function, and the first write operation worked, but the second operation failed, now your database is left in a broken state because you needed both of those things to succeed
[00:00:00]
Imagine transferring money from a bank account where three database operations are happening Two succeeded—the part where it took money out of your account—but the part where it sent it to the other account failed
[00:00:00]
Now what A transaction groups database write operations into one call, and if one fails, it rolls back all the others It's essentially a Git commit, but for the database
[00:00:00]
We'll start a transaction by calling transaction and getting a TX object It does the exact same thing as the DB object—you just treat it like DB
[00:00:00]
We'll create a new habit using TX.insert on the habits table, passing in the user ID, name, description, frequency, and target count
[00:00:00]
If tag IDs were passed, we'll create associations in the habit_tag table We'll map over the tag IDs and create entries linking the new habit ID with each tag ID
[00:00:00]
The transaction ensures that if anything breaks, everything is rolled back If everything succeeds, we'll return a 201 status with a "Habit Created" message
[00:00:00]
If something fails, it's likely a server-side issue, so we'll return a 500 status We'll then add this to our habit routes, creating a validation schema that matches our database and controller expectations
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops