Introduction to Sequelize
What is sequelize?
According to the docs “Sequelize is a promise-based ORM for Node.js v4 and upwards. It supports the dialects PostgreSQL, MySQL, SQLite and MSSQL and features solid transaction support, relations, read replication and more.”
A promise in Nodejs is an event which will produce a result in the future. This result can either be a success(fulfilled) or failure(rejected).
Sequelize being a promise-based ORM means that it supports NodeJS promises using the bluebirdJS library internally(which is a NodeJS promise library).
What is an ORM
An ORM is known as Object Relational Mapper. This is a tool or a level of abstraction which maps(converts) data in a relational database into programmatic objects that can be manipulated by a programmer using a programming language(usually an OOP language). ORMs solely exist to map the details between two data sources which due to a mismatch cannot coexist together.
Benefits of using ORMs
ORMs save time in writing raw SQL queries thereby reducing development time.
SQL queries are written as strings and embedded in the application logic. It is very difficult to get tools like syntax highlighting, autocorrect, or Intellisence support while writing raw queries in an application. ORMs provide a type-safe interface for interacting with databases.
ORMs helps to prevent against SQL injections.
ORMs lets you write Object-oriented code for your models and abstracts away the complete CRUD process enabling you to write declarative code in your application.
Features of Sequelize
Sequelize is packed with lots of features, below are a couple of features we will cover in this series on sequelize and more.
- Promises.
- Support for MySQL, SQLite, PostgreSQL, and Microsoft SQL server.
- Model Hooks
- Transaction support
- Database synchronization
- Database migrations
- Model Validations
- Raw queries
- Data seeding
- Scopes
Conclusion
In the series, we will be building a REST API for a book service. A basic understanding of SQL, NodeJS, and ExpressJS are good to have before reading through.
Before you proceed to the next article in this series do ensure to have the following installed and set up on your local machine.
- Node.js 6 and above
- Postman Native or Postman Chrome extension
- Postgres Database or create an Online Elephant SQL database
- Git
Sequelize is a great ORM in the NodeJS space with an active community on Github and Slack e.t.c. Check out the users of Sequelize.