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.
What is Sequelize API?
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.
What is ORM?
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.
Features of Sequelize API
- 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.
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.
Create A New Database Model
Models are the actual tables that we going to create in our databases, but in Sequelize we call it Models.
- Step 1 — install the Sequelize CLI
npm install — -save-dev sequelize-cli sequelize`
3. Step 3— Import necessary modules from the Sequelize dependency that we install in 1st step.
4. Step 4 — Implement the Customer model
5. Step 5 — Export the model to use in the controllers
Once you run your Node JS application, this model will create a new table called Customer with
firstName, lastName and
Major Database Queries
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
- 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,
firstName` of all customers
- 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
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.