Getting Started with Sequelize for Nodejs Applications

43,056 reads

@ amanhimself Aman Mittal 👨‍💻Developer ❤️ Nodejs, ReactNative, Expo | 📖 Blogger & Author

Introduction to ORM

ORM or Object Relation Mapping is a process of mapping between objects and relation database systems. An ORM acts like an interface between two system. ORM provide advantages for developers from basic ones like saving time and effort and rather focusing on business logic. The code is robust instead of redundant. ORM helps in managing queries for multiple tables in an effective manner. Lastly, an ORM (like sequelize) is capable to connect with different databases (which comes in handy when switching from one database to another).

Getting Started with Sequelize

Sequelize is a promise-based ORM for Node.js. Sequelize is easy to learn and has dozens of cool features like synchronization, association, validation, etc. It also has support for PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL. I am assuming you have some form of SQL database service started on your machine. I am currently using MySQL.

Installation

Sequelize is available via npm.

Setting up a Connection

Sequelize does setup a connection between the rest api/application and your SQL database. To setup basic connection between the two:

How do I setup my Sequelize Connection?

For the sake brevity, I like to divide code into modules. After all, the Unix philosophy of one program/module should do one thing is major part of the philosophy behind writing code in JavaScript (and using Node.js as a server side platform) these days.

I start with config.json / config.js file in the root of my application/api folder in which I define the general constraints needed to setup the connection with database:

You can do this in your .env file if you like to follow that pattern. For more info on this see dotenv .

After defining the configuration variables, in my models/ folder or where I define schema of tables in the database at application level, I create the connection in an index.js file:

It’s important to notice that I am exposing db object which contains every model/table schema definition. From now, I just have to import the db object to apply operations on specific database tables using it.

This setup can be auto-generated with the help of Sequelize CLI tool that helps in bootstrapping a new project in an effective manner (like the above) and handle database migrations directly from the terminal.

Conclusion

Sequelize is feature rich ORM for Node.js. It has a documentation that at times may not provide direct solutions to your problems but there always Github issues for that. What I like about is its Promise based control flow. Coming from NoSQL background (and using MongoDB), understanding Sequelize really took less time. Most of the query based models are quite similar to that in MongoDB (especially the CRUD operations). I am looking for a brighter, more improved documentation and ease of support from Sequelize.

Want to receive more articles like this one? Subscribe me here. Sometimes, I send “never seen before” content to my subscribers.

Thank you for reading. If you find this post useful, please hit the 💚button so this story can reach out to more readers. If you’d like to talk about it more, ping me on Twitter.

Tags