Setting up Sequelize associations

Aditya Naik
Nomads Of Code
Published in
3 min readJun 17, 2020

Between User (as author) and Post models in our ExpressJS API

Photo by davide ragusa on Unsplash

This is part 3 of multi-part series on express.

You can find Part 1 here. It talks about building a basic express api and setting up mocha for testing.

You can find Part 2 here. It talks about setting up PostgreSQL, sequelize for connecting express api with databases.

First things first. Let’s update our test.

Update test to check for Author in response

We update our expectation in response.

So apart from the post, now we are also looking for an author, which is an object and has firstName and lastName .

Sounds good so far.

Add User factory

We update our /factories/index.js and add a User factory

So User has a first and last name.

We also update helpers/index.js to create a mock user and associate Posts with it.

We created an Author and added author to each post via author.addAuthor(post) every time we create a post.

So now we are looking for 4 posts, each with author object that has first and last name in the response.

Excellent. Let’s code!

Add a user model

We will add another model User in our App.

sequelize model:generate --name User --attributes firstName:string,lastName:string

Associate with Post as author

Update the User model with the following code in associations section -

We basically tell sequelize that user has many posts as author. as:'Written' is called as an alias of the association. Foreign key is the unique identification id connecting the tables.

We also update Post model and add corresponding association

So, post belongs to user who is the author. Makes sense.

If you notice, we can use as:'Written' in User model and as:'Author' in Post model. This works perfectly fine, since we have specified the foreign key. This change will reflect in the response we get from querying either User or Post model.

Time to get these changes mapped to the database and tables.

sequelize db:migrate

Update seed file

We will add seeds for user, and connected posts with created user

So we create two authors using different libraries and update posts creation loops with author1.addAuthor(post) and author2.addAuthor(post) .

Update sequelize query

We go to our controller, and update the sequelize query to include information from user model

So we include an array that contains User model and attributes we want to send in the response.

Test the response

Now if we run npm run start and visit http://localhost:3000/posts , we get to see the author information in the response.

and of course the test is passing as well!

In the next part of this series, we will handle authentication and protecting routes.

Here is Part 4.

Craft Academy is a Tech Education Provider that aims to bring new talent to the market and help to solve the shortage of tech workers. We are founded on the belief that modern development standards, agile methodologies, and business skills are fundamental for IT professionals.

Our primary service is a 12-week coding bootcamp designed to provide individuals with a foundation of skills that allows them to enter the industry as junior developers.

With that foundation, our learners find employment in various industries or start their own businesses that bring new innovations to the market.

Would you like to know more about what we do? Follow us here on Medium, Facebook, Twitter or visit our website.

--

--