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

Advantages of using Sequelize over other alternatives

There are many alternative ways of querying the database from your application. There are ORMs, DAOs, raw database drivers, and so on. Each methodology has its pros and cons and caters to different programming styles and conventions. Typically, those who favor convention over configuration tend to gravitate toward ORMs, while those who favor configuration tend to use DAO frameworks or raw database drivers.

An ORM can handle data validation, similar to DAOs, with additional features such as reading and writing from a database using a driver. With ORMs, you would not need to manage query statements manually, which could save you time over the DAO or raw connection methods.

Note

An ORM is not mutually exclusive to DAOs. You can think of DAOs as being explicit as opposed to being implicit and presumptuous. A DAO only provides an interface for your data. It does not involve how/where you read or write the data (the database driver), nor will it concern itself with the data’s integrity unless the application manually invokes some form of data validation outside of the DAO’s scope.

When using an ORM such as Sequelize, you will have the following features without any additional code:

  • Transaction handling
  • Connection pooling
  • Model/data validation
  • Data integrity (outside of DBMS’ scope of foreign keys (FKs), unique constraints, and so on)
  • Eager loading
  • Schematic migration and cascading
  • Optimistic locking

Using a DAO or a raw database driver will forfeit these features, and you will have to build these solutions yourself. Using an ORM such as Sequelize will help you build your project with more efficiency and efficacy.

So far, we have covered the what and why for Sequelize; now, we will be going over the how for installing the necessary prerequisites for our application.