Supercharging Node.js Applications with Sequelize - Daniel Durante - E-Book

Supercharging Node.js Applications with Sequelize E-Book

Daniel Durante

0,0
26,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 234

Veröffentlichungsjahr: 2022

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Supercharging Node.js Applications with Sequelize

Create high-quality Node.js apps effortlessly while interacting with your SQL database

Daniel Durante

BIRMINGHAM—MUMBAI

Supercharging Node.js Applications with Sequelize

Copyright © 2022 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Associate Group Product Manager: Pavan Ramchandani

Publishing Product Manager: Kushal Dave

Senior Editor: Mark Dsouza

Content Development Editor: Divya Vijayan

Technical Editor: Simran Ali

Copy Editor: Safis Editing

Project Coordinator: Sonam Pandey

Proofreader: Safis Editing

Indexer: Pratik Shirodkar

Production Designer: Aparna Bhagat

Marketing Coordinator: Anamika Singh and Marylou De Mello

First published: October 2022

Production reference: 2061222

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80181-155-2

www.packt.com

To my parents, brother, family, and friends. To Audrey, for teaching me that not all stories have to be boring. To those who have built all of the libraries, frameworks, and applications for making this book possible.

– Daniel J. Durante

Foreword

Sequelize is a modern ORM that supports many different SQL dialects. Sequelize was born during the early days of the JavaScript runtime Node.js. It was the first of its kind, and was presented as a tool for the Node.js community to connect against existing relational databases, or to embrace them in new projects despite the rising trend of document stores. Ever since its release, a strong team of driven engineers added battle-tested support for transactions, relations, eager/lazy loading, read replication, and so much more. Twelve years on, the Sequelize project is part of npm’s top 100, and sees wide adoption by huge enterprises and small pet projects alike.

Daniel Durante joined the team in 2013 and contributed immensely to the library’s Postgres support by improving the general stability, but also via features such as HStore capabilities and pooling. Furthermore, and maybe most importantly, he was also the one behind the introduction of hooks and object lifecycles, which is now one of the core mechanics used to interact with data objects.

This book will guide you through all the steps of app development, starting with the very ideation of an app, to the modeling of entities and definition of business requirements, all the way to the implementation and deployment of a project. Throughout this journey you’ll learn how to grow a small pet project into a stable and scalable application that is free of unwanted side-effects, that caters for auditing of user actions, and can be easily extended going forward.

Enjoy your ride and welcome to the world of databases!

Sascha Depold

Author of Sequelize, Creator of bitte.kaufen and Engineering Manager at eBay

Contributors

About the author

Daniel Durante has nearly a decade of experience in creating Node.js applications with Sequelize. He is currently working on setting up applications and infrastructures for blockchain and options trading analysis. He has been working with Genesis Volatility for the last three years, and currently works as a chief technical officer.

I want to thank Sascha Depold for not only being a great programmer but also a great mentor and leader. Always willing to give insightful input and a hand wherever it’s needed. I want to also thank everyone who has contributed to Sequelize. It’s been a great experience, and a blessing, to witness a project go from infancy to enterprise and through all of its trials and tribulations. I also want to thank the Packt team for doing an amazing job and keeping this project going during troubled times and extending deadlines and moving mountains. You guys did one heck of a great job, thank you!

About the reviewers

Sascha Depold is the original author of the first-of-its-kind Node.js ORM Sequelize. He is an experienced software engineer and engineering manager who has worked for companies such as DaWanda (now part of Etsy), Contentful, and eBay. In his spare time, he likes to collaborate with different tech schools, where he teaches software patterns and approaches as well as recent technology trends (such as GraphQL and Docker/Kubernetes). Sascha is also the creator of the platform-independent wishlist app bitte.kaufen, which makes heavy use of Sequelize under the hood.

Benjamin Coe works on the open source libraries yargs, nyc, and c8, and is a core collaborator on Node.js. He’s a manager of a team at Google that generates idiomatic client libraries for eight programming languages.

Table of Contents

Preface

Part 1 – Installation, Configuration, and the Basics

1

Introduction to Sequelize and ORM in Node.js

Technical requirements

Introducing Sequelize

Advantages of using Sequelize over other alternatives

Installing the necessary applications, frameworks, and tools to help get you started

Installing MySQL

Creating a database

Installing Node.js

Configuring Sequelize within an Express application

Connecting Sequelize with Express

Summary

2

Defining and Using Sequelize Models

Technical requirements

Defining models for a database

Exploring the various Sequelize data types and when to use them

STRING

CHAR

TEXT/TINYTEXT/MEDIUMTEXT/LONGTEXT

CITEXT

NUMBER

INTEGER/TINYINT/SMALLINT/MEDIUMINT/BIGINT

FLOAT/REAL

DECIMAL/DOUBLE

BOOLEAN

DATE/DATEONLY/TIME

NOW

HSTORE

JSON

JSONB

BLOB

UUID/UUIDV1/UUIDV4

VIRTUAL

ENUM

ARRAY

GEOMETRY

GEOGRAPHY

CIDR/INET/MACADDR

TSVECTOR

Migrating schematic changes and data from Sequelize to the database

Migrating schematic changes

Initializing seed data

Manipulating and querying data using Sequelize

Reading data

Complex querying

Deleting data

Updating and saving data

Creating data

Ordering and grouping

Limits and pagination

Advanced Sequelize options for defining models

sequelize

modelName

defaultScope/scopes

omitNull

timestamps

paranoid

createdAt/updatedAt/deletedAt

underscored

freezeTableName

tableName

name

schema

engine

charset

collation

comment

initialAutoIncrement

hooks

validate

indexes

Summary

Part 2 – Validating, Customizing, and Associating Your Data

3

Validating Models

Technical requirements

Using validations as constraints

allowNull

unique

Built-in validations

Applying validations to our project

Creating custom validation methods

Creating custom attribute validators

Executing validations while performing asynchronous operations

Handling validation errors

Summary

4

Associating Models

Technical requirements

Association methods

hasOne

belongsTo

hasMany

belongsToMany

Renaming associations

Relationship patterns

One-to-One

One-to-Many

Many-to-Many

Using custom foreign key definitions

Querying associations with eager loading and lazy loading

Lazy loading

Eager loading

Using advanced association options

Using scopes with associations

Creating Super Many-to-Many relationships

Using polymorphic associations

Applying associations to Avalon Airlines

Summary

5

Adding Hooks and Lifecycle Events to Your Models

Technical requirements

Order of operations for lifecycle events

Defining, removing, and executing lifecycle events

Defining instance and model lifecycle events

Removing lifecycle events

Executing lifecycle events

Using lifecycle events with associations and transactions

Putting it all together

Summary

References

6

Implementing Transactions with Sequelize

Technical requirements

Managed and unmanaged transactions

Running transactions concurrently

Running transactions concurrently with Sequelize

Running transactions with CLS

Isolation levels and advanced configuration

Locking rows with Sequelize

Using life cycle events for transactions

Putting it all together

Summary

7

Handling Customized, JSON, and Blob Data Types

Technical requirements

Querying JSON and JSONB data

Using the BLOB column type

Creating custom data types

Putting it all together

Summary

Part 3 – Advanced Queries, Using Adapters, and Logging Queries

8

Logging and Monitoring Your Application

Technical requirements

Configuring logging with all of the available interfaces

Integrating third-party logging applications such as Pino or Bunyan

Integrating with Pino

Integrating with Bunyan

Collecting metrics and statistics for Sequelize with OpenTelemetry

Summary

9

Using and Creating Adapters

Technical requirements

Installing, configuring, and integrating AdminJS with Sequelize

Integrating Sequelize with GraphQL

Creating our own adapter

Summary

10

Deploying a Sequelize Application

Technical requirements

Refactoring and adding flight schedule routes

Integrating Express’ static middleware and securing the admin interface

Creating a page to list and book flights

Deploying the application

Summary

Index

Other Books You May Enjoy

Part 1 – Installation, Configuration, and the Basics

In this part, you will learn how to install and configure Sequelize for your operating system, and how to insert, delete, update, and query data from your database.

This part comprises the following chapters:

Chapter 1, Introduction to Sequelize and ORM in Node.jsChapter 2, Defining and Using Sequelize Models

1

Introduction to Sequelize and ORM in Node.js

Managing database drivers, managing schematics, maintaining a business’ workflow, and validating data can be daunting for any programmer. Along with perpetually changing business requirements, organizing the business logic into database models can be cumbersome. This usually entails the programmer finding all applicable references and updating queries manually. This could be an expensive operation for both the project and the programmer; without proper testing, the modifications could result in errors within the application or erroneous logic, leaving the programmer, the business, and the customer in a state of confusion.

This book will help guide you through the process of installing, building, maintaining, upgrading, extending, querying, and applying database schematics using an object-relational mapping (ORM) framework in a Node.js application using the Node.js runtime environment. The book can be read from start to finish in a sequential manner, or if you are more experienced, you can read the chapters that interest you directly. Each chapter complements the previous chapter since we will be creating an entire application from scratch. However, more experienced programmers can skip between chapters with the understanding that there may be “gaps” within their data model and what is shown within the chapter. The concepts and methodologies taught in each chapter, regardless of your data’s structure, will still be applicable.

The goal of this chapter is to help you become familiar with what Sequelize is and which capabilities are offered to you from using Sequelize. We will go over the necessary prerequisite steps for installing applicable libraries, frameworks, runtime engines, and database management systems (DBMS). By the end of this chapter, you will have acquired the knowledge and skillset of installing, configuring, and running an application, under the Node.js runtime with Sequelize, from scratch.

The first chapter of this book will cover the following topics:

Introducing SequelizeAdvantages of using Sequelize over other alternativesInstalling the necessary applications, frameworks, and tools to help get you startedConfiguring Sequelize within an Express application

Technical requirements

Before we embark on our journey of developing an application with Sequelize, there are a few prerequisites. We will need to install the following:

A DBMS such as MySQLThe Node.js runtime libraryA few Node.js packages: Sequelize, Express, and a MySQL driver

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.

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 handlingConnection poolingModel/data validationData integrity (outside of DBMS’ scope of foreign keys (FKs), unique constraints, and so on)Eager loadingSchematic migration and cascadingOptimistic 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.

Installing the necessary applications, frameworks, and tools to help get you started

Our application will require customers to view information from a centralized source, and we will need to capture information that they have entered into our database. Usually, customers can either view your product/services via an application that they install on their machine or they can use a browser to visit our website. Node.js is a good choice for building web applications, which is what we’ll be building throughout this book, due to its central processing unit (CPU)-bound limitations and ease of context switching between frontend development (what is displayed to the end user) and backend development (what the end user does not see but still invokes) owing to Node.js being JavaScript. We will need to install the following applications/programs in order to get started:

A DBMS (we will be installing MySQL)Node.js runtimeSequelize and Express

Installing MySQL

This next section will go over the installation process for MySQL on three different operating system distributions: Microsoft Windows, macOS, and Linux. MySQL was chosen due to the ease of installation (no need to mess with configurations or access-control lists (ACLs)). Do not let those points discourage you from using a different database. For the most part, Sequelize will be able to gracefully translate from one DBMS to another, and the majority of this book will use common/standard Structured Query Language (SQL) methods.

Windows

The MySQL installer for Microsoft Windows can be found here:

https://dev.mysql.com/downloads/mysql/5.7.html

Note

The default Uniform Resource Locator (URL) for downloading Windows’ MySQL installer is currently at version 8.0.26. This book uses version 5.7, but other versions of MySQL should still work appropriately as long as the Node.js MySQL driver is compatible with that version.

Once we are finished downloading and opening the installer application, you will be greeted with the Choosing a Setup Type screen. We will want to select the Developer Default and Install all products options, as illustrated in the following screenshot:

Figure 1.1 – Windows MySQL Installer: Choosing a Setup Type

If you have Python or Visual Studio installed on your computer, you may be greeted with a Check Requirements step (see Figure 1.2). If you are using Visual Studio as your integrated development environment (IDE), then you may install the necessary products, but it is not a requirement. Throughout your projects, you may come across a utility that is written in Python that interacts with your database (for example, most data science-related libraries/frameworks). By selecting the Connector/Python option shown in the following screenshot, we can avoid potential headaches in the future:

Figure 1.2 – Windows MySQL Installer: Check Requirements

The next section should be the Download step. The main products that we will be required for the contents of this book are listed here:

MySQL ServerMySQL Workbench (for a graphical user interface (GUI) to our database)MySQL Shell

You can see the aforementioned products in the following screenshot:

Figure 1.3 – Windows MySQL Installer: Download

Note

If you are new to MySQL, it may be a good idea to download the MySQL Documentation and Samples and Examples packages.

After we have finished downloading our packages, we will be entering our configuration details for each applicable selected product (for example, MySQL Server and Samples and Examples). For the majority of the configuration settings, we will be using the default values; however, there will be some steps that will require your intervention. You can see an overview of this in the following screenshot:

Figure 1.4 – Windows MySQL Installer: Type and Networking

From the MySQL Server configuration wizard, we will want the following settings (as shown in Figure 1.4):

Config Type: Development ComputerTCP/IP: CheckedPort: 3306Open Windows Firewall port for network access: Optional

The next part of the MySQL Server configuration step is to declare your MySQL root password and user accounts. Make sure to keep this information in a safe place in case you run into administration issues throughout your projects. If you forget the MySQL root password, there are several methods for resetting the password, as explained here: https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html.

For setting up a MySQL user account with a role, you will be greeted with the following Accounts and Roles screen:

Figure 1.5 – Windows MySQL Installer: Accounts and Roles

Within the MySQL User Accounts section, you will need to click on the Add User button (near the right side of the window, as shown in Figure 1.5) and type in a username and password that you will memorize for when we initialize our Node.js application. When you are finished adding the appropriate root password and MySQL user account(s), we can proceed to the next step.

Next, the installation process will offer a Configure MySQL Server as a Windows Service option, as illustrated in the following screenshot. Windows Service is a process control system (PCS) that will also orchestrate background processes (in the Unix/Linux world, these are referred to as daemons):

Figure 1.6 – Windows MySQL Installer: Windows Service

We will want to ensure the following parameters are configured (as shown in Figure 1.6):

Configure MySQL Server as a Windows Service: CheckedStart the MySQL Server at System Startup: CheckedStandard System Account selected under the Run Windows Service as... section

Click on Next > to apply our configurations for the MySQL server. If you selected additional packages to install earlier, you may be prompted with additional screens asking for more configuration settings and parameters.

Note

If you selected the MySQL Router package from the previous section, the installation process will ask you for information on how you would like to set up a cluster environment. It is not recommended to install this package unless you are a database administrator or you are setting up a production environment. Simply uncheck the Bootstrap MySQL Router for use with InnoDB cluster option and click Finish to proceed without installing MySQL under a cluster environment.

If the Samples and Examples package was selected for installation, we will be prompted with a screen that will allow us to enter our MySQL username and password. You may use your root credentials for the username and password input fields and click on the Next > button to continue. An overview of the screen is provided in the following screenshot:

Figure 1.7 – Windows MySQL Installer: Connect To Server

macOS

There are a couple of ways to install MySQL on a macOS machine. The first way is to download and install MySQL from a Disk iMaGe (DMG



Tausende von E-Books und Hörbücher

Ihre Zahl wächst ständig und Sie haben eine Fixpreisgarantie.