Sequelize Database Migrations and Models With Nodejs

Koray Göçmen
Nov 21 · 4 min read
Image for post
Image for post
Nodejs + Sequelize

After using Sequelize in my Express applications for the past 6 years, this is how I write my migrations and models for all my Nodejs applications.

Install sequelize with npm first and initialize the project with “sequelize init”. That should create the “models”, “migrations” and “seeders” folders as well as create the “config/config.json” file.

In our application, we’ll have users and cards. Each user can have multiple cards.

We need a users table and a cards table in our database and users will have a one-to-many relationship to cards.

You can find how those two models are associated using the Sequelize way here:

Create the users table migration by running:

You should now see the “migrations/20190610033956-create-users.js”

Also, create the model file under “models/User.js”

I usually add the purge function on the model itself, since we can then call purge on a created user instance before sending it back to the frontend. Otherwise, we might send back user data that should have not been sent back such as the password hash.

Create cards table migration:

You should now see the “migrations/20190610033980-create-cards.js”

Also, create the model file under “models/Card.js”

Create the users and cards tables and then the model files. We create the association in our “lib/database.js” file. You can find that here:

That’s pretty much it. The above code is actually from Violetnorth’s API. If you need to secure your website and your corporate email, you can check out Violetnorth.

Smart web and email firewall to secure your entire infrastructure:

Thanks for stopping by!

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Koray Göçmen

Written by

University of Toronto, Computer Engineering, architected and implemented reliable infrastructures and worked as the lead developer for multiple startups.

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Koray Göçmen

Written by

University of Toronto, Computer Engineering, architected and implemented reliable infrastructures and worked as the lead developer for multiple startups.

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store