Book Image

Supercharging Node.js Applications with Sequelize

By : Daniel Durante
4 (1)
Book Image

Supercharging Node.js Applications with Sequelize

4 (1)
By: Daniel Durante

Overview of this book

Continuous changes in business requirements can make it difficult for programmers to organize business logic into database models, which turns out to be an expensive operation as changes to the database may result in errors and incongruity within applications. Supercharging Node.js Applications with Sequelize helps you get to grips with Sequelize, a reliable ORM that enables you to alleviate these issues in your database and applications. With Sequelize, you'll no longer need to store information in flat files or memory. This book takes a hands-on approach to implementation and associated methodologies for your database that will have you up and running in no time. You'll learn how to configure Sequelize for your Node.js application properly, develop a better sense of understanding of how this ORM works, and find out how to manage your database from Node.js using Sequelize. Finally, you'll be able to use Sequelize as the database driver for building your application from scratch. By the end of this Node.js book, you'll be able to configure, build, store, retrieve, validate, and associate your data from a database to a Node.js application.
Table of Contents (16 chapters)
1
Part 1 – Installation, Configuration, and the Basics
4
Part 2 – Validating, Customizing, and Associating Your Data
10
Part 3 – Advanced Queries, Using Adapters, and Logging Queries

Introducing Sequelize

Sequelize (also known as SequelizeJS) is an ORM framework that helps connect and correspond your Node.js application to a database. Sequelize has been in development since 2010 by Sascha Depold and is used extensively within Fortune 100 companies. Throughout the years, the framework has grown to nearly 25,000 stargazers on GitHub, with over 900 contributors, and is used by over 300,000 open sourced projects. Sequelize has been battle-tested for performance and security for over a decade and has performed without issues for major retail stores and web agencies (such as Walmart and Bitnami) even during their highest traffic times of the year.

What started out as a master’s thesis turned into a major integral building block of Node.js’ ecosystem.

Note

An ORM is a methodology of associating database structures and information using object-oriented (OO) decorations and patterns. An ORM’s purpose is to help alleviate the differences between DBMSs and to offer some form of abstraction for querying and manipulating data more ergonomically. Typically, an ORM will also come with helper functions to help manage the state of connections, pre-validation of data, and workflows.

The framework follows a promise-based approach, which allows programmers to invoke data asynchronously. The promise-based approach offers a more convenient way of managing returned values, or errors, within your application without waiting for the result(s) to return immediately. To learn more about promises and how to program with them, refer to the following link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise.

What is asynchronous?

Think of asynchronous as a way to perform tasks without having to wait for a response before continuing on with another task. When you text message someone, you do not have to wait for their response in order to continue with your day. After you send a message, you usually would not warrant any attention to the correspondence until you receive a signal that there was a response or that the message had failed to send.

Currently, Sequelize supports the following DBMSs: MySQL, MariaDB, Postgres, Microsoft SQL Server (MSSQL), Snowflake, Database 2 (DB2), and SQLite. An ORM offers more than just a connector to your database. ORMs often offer features such as the following:

  • Tooling for migrating schemas and data
  • Adapter/plugin support
  • Connection pooling
  • Eager loading of data
  • Managed transactions

Now that we understand what Sequelize is and its basic capabilities, we will go over why we should use an ORM such as Sequelize over alternative methods such as data access objects (DAOs) or querying the database directly. Some of the advantageous capabilities include being able to handle and organize queries within transactions or migrating schematic changes to a database.