Getting Started With Sequelize — A walkthrough and short tutorial.

Joseph Hu
4 min readDec 29, 2018

Hi there! If you’re just learning how to use Sequelize and found this article, hopefully I can get you started with a simple walkthrough! Please continue assuming you are familiar with using Node.js/Express and SQL using PostgreSQL as we will be using examples of these down further.

Now, what is Sequelize? If we grab the definition right out of the documentation… Sequelize is a promise-based ORM for Node.js v4 and up. It supports the dialects PostgreSQL, MySQL, SQLite and MSSQL and features solid transaction support, relations, read replication and more.

Now what does that mean?

Lets go over what that means! So what is a promise (if you are unfamiliar with promises, this link is a great starter) based ORM? An ORM is an object-relational-mapping technique that lets you query and manipulate data using object-oriented languages. In our case, Javascript! What an ORM essentially is a library written in your choice of language that has the code you need instead of using SQL. With all that being said, of all the database management systems, we’ll be using PostgreSQL in below examples.

Before we start, for those using PostgresQL, I highly recommend using a PostgresQL client to easily view your databases and make a few edits. I personally use Postico while PgAdmin is another free alternative for those not using Macs.

Lets start! Let’s npm init before installing our packages!

npm init

Npm install sequelize with PostgreSQL


npm install sequelize
npm install pg pg-hstore

If you are using MySQL or SQLite instead of PostgesQL:

npm install mysql2
npm install sqlite3

Let’s install nodemon so we can restart our app everytime we update it

npm install nodemon

Let’s make a directory looking like below. We would place our models in our index.js, our seed to pre-populate our tables and the server to initialize everything.

Appfolder
|-models
|-index.js
|-seed.js
|-main.js

Your package.json should look something similar to this, with the exception to the two scripts start and seed:

Before we start with any code, create a database. In our example case that would be sequelize-guide but it can be whatever you named it in your package.json. Let’s make that database in our terminal.

createDb sequelize-guide

Lets get started with our index.js file where we can put our models. Sequelize allows us to connect to the database using a connection URL. So we can input below:

For our models, we have a simple example of a table called Campus with a name, address, and description column and each of these columns has a certain type of data input.

Now we have to sync this model to our database! Time to move on to our main.js. In our main.js, we’ll just make a simple function to initialize our sync. Put a console log in your function for a prompt to let you know that your tables have been properly synced!

If we take a quick peek at our table in our PostgreSQL client, we should see something very similar at this point. If you’re getting an error, don’t forget to export your Campus model and db!

Looks very familiar to something we can do with SQL right?

This table looks pretty empty so lets populate this table with our own data!

Create your own instance of data with:

db.create({}) // place your data in here within the constraints of your model!

When populating your own data, make sure your data matches the constraints of your own model otherwise you will hit errors. It should look very similar to below.

In your terminal run below to run the seed file.

npm run seed

You should see that your table is populated now!

So what now? What can we do with the data that’s been populated? With SQL we can grab the Campus table with below

SELECT * FROM Campus

We can do the same with Sequelize using findAll().

Do you see what our console.log is showing up in our terminal? Do you see anything familiar? It should be the table data! In our case you should see these data values in your console.log!

There are many other ways to query for our data values, many of the common ones are:

Model.findOne — this finds a single instance that matches the search criteria

const findCampus = await Campus.findOne({
where: {name: Harvard}
})

Model.findById — very self explanatory, finds a single instances with the specified id.

const campusOne = await Campus.findById(1)

and Model.findAll which we have just used. You can check out the other model queries in the official documentation.

Now you have the basic setup of a Sequelize application! With this you can be more flexible in using SQL databases in Javascript applications. You don’t need to write raw SQL queries as much, manage your connections and much more. Hopefully this walkthrough is helpful if you were having trouble getting started from square one and understanding what Sequelize can do!

Look forward to some more advanced topics using Sequelize like connecting multiple schemas and implementing Sequelize with your own Javascript projects! Email me at zhhjoseph@gmail.com for any comments or questions!

--

--