Sequelize API for Node JS

When we implement applications with Node JS, Mongo DB is not always the best data storage option. Most of the production level applications use relational databases. But how we can build applications using relational databases in Node JS with the following best practices. In this post, I will give one of the most popular and powerful API namely Sequelize for Node JS to implement applications with relational databases.

Sequelize is a promise-based Node JS Object Relational Mapping (ORM) for databases like MySQL, MSSQL, Postgres and SQLite. Sequelize follows Semantic Versioning and support Node JS version 10 and above.

An ORM is also known as Object Relational Mapper. This is a level of abstraction that converts the data in a relational database into programmatic objects that can be handled by a programmer using a programming language. ORMs solely exist to map the details between two data sources which due to a mismatch cannot coexist together.

  • Support for multiple databases. For instance, MySQL, MSSQL, PostgreSQL and SQLite.
  • Transaction support — According to Sequelize documentation, a transaction is an object that used to identify an already running transaction. It is created by calling Sequelize.transaction() function. And also, if you want to run a query under a transaction you should pass the transaction in the options object.
  • Database synchronization
  • Database migrations
  • Model validation — Sequelize validations are checked performed in the Sequelize level. Validations can be custom made or Sequelize in-built validations. If validation fails, no SQL query will be sent to the database.
  • Scopes

Okay, now we know what is Seqeulize, what is ORM and what are the features provided by Sequelize API. Let’s take a look at how we can create database models and major queries using Sequelize API.

Models are the actual tables that we going to create in our databases, but in Sequelize we call it Models.

  1. Step 1 — install the Sequelize CLI

npm install — -save-dev sequelize-cli sequelize`

2. Step 2— Create a normal JavaScript file and gave any name that you want. For now, I called it as Customer.model.js`

3. Step 3— Import necessary modules from the Sequelize dependency that we install in 1st step.

Import modules from Sequelize dependency

4. Step 4 — Implement the Customer model

Implement customer database model

5. Step 5 — Export the model to use in the controllers

Export the customer model

Once you run your Node JS application, this model will create a new table called Customer with firstName, lastName and email

Now you can create another file called Customer.controller.js to implements the necessary queries. In that file, you have to import Customer ` model that we implemented in Customer.model.js`.

Import Customer model
  • Create a new customer

This code inserts a new row into the Customer` table. But behind the scene, Sequelize runs this raw query.

  • Get all customers

Actual query that runs under the Sequelize API,

  • Get firstName` of all customers

Actual query,

  • Update Customer’s last name where the ID is equal to 3

Actual query that runs in the database,

  • Delete a customer where the ID is equal to 3

Actual query,

These are a set of major queries that Sequelize API provide. Okay, now we know how to create a database model and how to run major queries using Sequelize API.

In conclusion, Sequelize is a great and optimized ORM in the Node JS space with an active community on GitHub.

I hope you learn something new today. Thank you.

Problem solver, Developer, Undergraduate student, YouTube content creator

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