
Master Sequelize ORM with Express.js and Postgres by building a simple authentication system with register, login, logout, and tokens, and learn testing with Supertest and web tokens.
Discover essential tools for Windows users, including Windows Subsystem for Linux 2, Windows Terminal, Docker, and optional Ohmy to achieve Linux-like development.
Manage multiple node versions with Node version manager (nvm), installing long-term support or latest releases and switching between them for different projects using commands like nvm install and nvm use.
Install docker and docker compose using docker desktop on Windows, Mac, or Linux, then manage containers, images, and volumes to build dev environments for postgres databases with docker-compose.
Learn to install and use Postman and DBeaver to test your application and manage the database, connect to tables, and perform operations like deleting records across MySQL and other databases.
Install Visual Studio Code and enable remote development with Windows Subsystem for Linux. Connect the editor to WSL using the extension shown for seamless editing from the terminal.
Initialize a new project in Linux via WSL, set the node version with nvm, create package.json, configure prettier, and add a .gitignore for node_modules and env files.
Install and explain essential dependencies for the project, including modules, authentication tokens, postgres driver, express, dotenv, morgan, and a transaction management library to ensure database consistency.
Configure babel and jest for node, set up configs and scripts for test, watch, and coverage, and establish a source-to-dist build with Server Dargis as the entry, enabling inspect debugging.
Compare real, in-memory, and SQLite testing databases, highlighting reliability and setup trade-offs. Then show configuring two Postgres databases with Docker Compose for local testing and straightforward connections.
Master the basics of json web tokens (jwt) for authentication and authorization by understanding header, payload, signature, signed vs encrypted tokens, and using them in authorization headers with stateless verification.
Understand why passwords must be hashed with salt using bcrypt, how salts prevent rainbow table attacks, and how adaptive, slow hashing protects against brute-force attacks as hardware evolves.
Create a config directory with a database config and an environment file to centralize settings for a sequelize express postgres, development and test profiles, port, dialect, JWT secret, password hashing.
Create a jwt utilities module that generates and verifies access and refresh tokens, using a jwt library and environment secrets, with separate utils and tests to ensure token validity.
Create a database class to manage a Sequelize connection (connect, disconnect, sync) for an Express app, using environment-based config, a transactions namespace, and model registration.
Learn to implement the register models function in a Sequelize setup by building a models object and wiring associations via each model's associate method.
Initialize an express server by importing config, loading environment variables, and connecting to the database, then listen on the configured port within an immediately invoked function expression.
Create test helpers for a Sequelize project by building a test db utility class that starts and stops the database and resets it before each test with a forced sync.
Learn to build sequelize models for user, refresh token, and roles, defining one-to-many and one-to-one associations with properties like email, password, username, first name, last name, and token.
Define the user model in Sequelize within an Express.js and Postgres setup. Implement static methods for hashing passwords and creating users, and establish associations with refresh tokens and roles.
Hash passwords in a before save hook, add a compare passwords instance method, and set a default scope that excludes the password while a passwords scope includes it.
Create a new user with roles and a refresh token inside a managed transaction to ensure atomic insertions across user, refresh token, and role tables.
Create the role model in sequelize by defining a Role class that extends Model, initializing a role property as a string, and connecting the role to a user.
Add a refresh token model in Sequelize, duplicating the role model, renaming the class across files, and updating the token to refresh token for an Express.js and Postgres setup.
Inspect the newly synced Sequelize tables in Postgres with DBeaver, noting a unique email, a unique username, and the primary key. Explain why migrations provide a history of database changes.
Generate the first Sequelize migration to create the users table, defining id, email, password, username, first name, last name, and created_at updated_at fields, with up and down migrations for rollback.
Create and test a roles table migration for Sequelize, defining fields and a user_id foreign key with cascade on update and delete, then compare, revert, and reapply migrations.
Add a refresh tokens table via migration, change the token field to text, and apply on delete cascade to align with the public schema using Sequelize and Postgres.
apply and manage migrations by resetting the development database and reapplying them, ensure migrations match the models, and prepare tests for the user model across development and production.
Improve the user creation flow in sequelize by returning the newly created user from a transaction and using an after-create hook to hide the password before returning.
Learn to test the user model in Sequelize with Express and Postgres, including hashing passwords, creating a user via static method, and validating email, names, roles, and tokens.
Write and run focused tests for the create user method, validating email and username with sequelize validations and unique database constraints, and verify single-error responses.
Add a static async create user test helper to build and save users via the Sequelize models. Verify the default scope hides passwords and the with passwords scope reveals them.
Test the user model's password comparison instance method in a Sequelize with Express.js and Postgres setup, validating true for correct passwords and false for incorrect ones.
Fix a bug in the user model where password hashing runs if no password is provided. Add a test to ensure updating user without a password does not hash it.
Create an express app with an object-oriented Up class, configure Morgan logging, json and urlencoded parsing, register routes and middleware, and provide a static getUp helper for tests.
Implement an Express.js error handling middleware that catches controller errors and returns a 500 internal server error with the error message, wired into the app as error middleware.
Create a run async wrapper utility to handle errors in async Express controllers, exporting a wrapper that calls next on failures and facilitates testing with mocks.
Create and implement an authentication middleware that verifies bearer access or refresh tokens from the authorization header using jwt utilities, handles errors, and passes control to the next middleware.
Add a controllers folder with v1 routes, set up an Express router, export default, and wire register, login, and token controllers for a versioned API.
Explore testing the register controller in a Sequelize and Express.js app by simulating user registration, validating success responses, database changes, and duplicate user errors.
Design and implement the login endpoint: validate email and password, generate an access token and a refresh token with JWT utilities, and manage refresh tokens in the database.
Introduce a test helper named registering user to call the registration endpoint with supertest, sending email and password and returning access and refresh tokens for subsequent tests.
Discover how to add login controller tests in a Sequelize with Express and Postgres project, covering success and failure login cases, refresh token handling, and logout validation.
Create a token controller that returns a new access token when a valid refresh token exists, loading the refresh token model and validating the authorization header with JWT.
Explore adding tests for the token controller middleware, covering invalid and missing authorization headers, bearer tokens, and 500 errors, with Jest spies on JWT utils.
Write and verify token controller tests, asserting a 200 response with a valid access token, and handle cases with no refresh token by returning 401 and a login reminder.
Add logout controller and register in router. Retrieve the JWT from the request body, locate the user by email with the refresh token model, and return 200 with success true.
Add tests for the logout controller by issuing a post /logout with an access token, and verify 200 status, the message 'successfully locked out', and a nullified refresh token.
Assess test coverage for the controllers and models in an Express app using Sequelize, review lcov reports in coverage folder, and understand why some code remains uncovered in test mode.
Connect to the database, set up a new app, import the default module to wire up controllers and models, and start listening on port 80.
Learn to test a Sequelize ORM app with Express.js and Postgres using Postman, including registering users, managing refresh and access tokens, and validating login and logout flows.
Learn to use the debug script to attach the debugger, inspect variables, step through code, and diagnose issues in a sequelize orm app with express and postgres.
Conclude the course by testing code, applying migrations, and configuring models with Express.js and Sequelize ORM. Explore the basics of web tokens and best practices to keep improving your code.
Hi and welcome to this course! Here, you will learn A LOT about how to build a simple backend with production-level code. We are going to learn how to set up a local database with Docker, how to use Express.js with Sequelize as the ORM and manipulate the database, how to test our code with Jest and more importantly, how to structure our code to test it easily. We will also learn:
Sequelize best practices (how to register models, how to register associations between models, how to add scopes, how to make simple queries, how to write migrations, etc.)
JWT and Bcrypt to handle authentication and
Testing with Jest (remember that untested code is broken code)
Express middlewares and how to test them
Docker and Docker Compose to set up a local database and avoid installing databases locally
Best practices in general (separating the Express app from the server for easier testing, separating model logic from the controller, hiding passwords from the response, etc.)
This course is different as I am looking for you to learn how production-level code looks like and build the habit to test every line of code you write! Most courses skip testing, but the reality is that testing is so important on software development that it is unbelievable that most courses out there just skip them.