Supercharging Node.js Applications with Sequelize

Book description

A hands-on implementation guide to application development in Node.js using the powerful Sequelize ORM

Key Features

  • Use the highly powerful JavaScript library to map your database to objects easily
  • Build an application from scratch with easy-to-follow steps and examples
  • Discover industry best practices, tips, and techniques on Sequelize to manage your database with ease

Book Description

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.

What you will learn

  • Configure and optimize Sequelize for your application
  • Validate your database and hydrate it with data
  • Add life cycle events (or hooks) to your application for business logic
  • Organize and ensure the integrity of your data even on preexisting databases
  • Scaffold a database using built-in Sequelize features and tools
  • Discover industry-based best practices, tips, and techniques to simplify your application development

Who this book is for

This book is for beginner- to intermediate-level Node.js developers looking to upgrade their skills and use Sequelize as their ORM of choice. Basic knowledge of and prior experience with JavaScript and SQL are needed to make the most out of this book.

Table of contents

  1. Supercharging Node.js Applications with Sequelize
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewers
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  7. Part 1 – Installation, Configuration, and the Basics
  8. Chapter 1: Introduction to Sequelize and ORM in Node.js
    1. Technical requirements
    2. Introducing Sequelize
    3. Advantages of using Sequelize over other alternatives
    4. Installing the necessary applications, frameworks, and tools to help get you started
      1. Installing MySQL
      2. Creating a database
      3. Installing Node.js
    5. Configuring Sequelize within an Express application
      1. Connecting Sequelize with Express
    6. Summary
  9. Chapter 2: Defining and Using Sequelize Models
    1. Technical requirements
    2. Defining models for a database
    3. Exploring the various Sequelize data types and when to use them
      1. STRING
      2. CHAR
      3. TEXT/TINYTEXT/MEDIUMTEXT/LONGTEXT
      4. CITEXT
      5. NUMBER
      6. INTEGER/TINYINT/SMALLINT/MEDIUMINT/BIGINT
      7. FLOAT/REAL
      8. DECIMAL/DOUBLE
      9. BOOLEAN
      10. DATE/DATEONLY/TIME
      11. NOW
      12. HSTORE
      13. JSON
      14. JSONB
      15. BLOB
      16. UUID/UUIDV1/UUIDV4
      17. VIRTUAL
      18. ENUM
      19. ARRAY
      20. GEOMETRY
      21. GEOGRAPHY
      22. CIDR/INET/MACADDR
      23. TSVECTOR
    4. Migrating schematic changes and data from Sequelize to the database
      1. Migrating schematic changes
      2. Initializing seed data
    5. Manipulating and querying data using Sequelize
      1. Reading data
      2. Complex querying
      3. Deleting data
      4. Updating and saving data
      5. Creating data
      6. Ordering and grouping
      7. Limits and pagination
    6. Advanced Sequelize options for defining models
      1. sequelize
      2. modelName
      3. defaultScope/scopes
      4. omitNull
      5. timestamps
      6. paranoid
      7. createdAt/updatedAt/deletedAt
      8. underscored
      9. freezeTableName
      10. tableName
      11. name
      12. schema
      13. engine
      14. charset
      15. collation
      16. comment
      17. initialAutoIncrement
      18. hooks
      19. validate
      20. indexes
    7. Summary
  10. Part 2 – Validating, Customizing, and Associating Your Data
  11. Chapter 3: Validating Models
    1. Technical requirements
    2. Using validations as constraints
      1. allowNull
      2. unique
      3. Built-in validations
      4. Applying validations to our project
    3. Creating custom validation methods
      1. Creating custom attribute validators
    4. Executing validations while performing asynchronous operations
    5. Handling validation errors
    6. Summary
  12. Chapter 4: Associating Models
    1. Technical requirements
    2. Association methods
      1. hasOne
      2. belongsTo
      3. hasMany
      4. belongsToMany
      5. Renaming associations
    3. Relationship patterns
      1. One-to-One
      2. One-to-Many
      3. Many-to-Many
      4. Using custom foreign key definitions
    4. Querying associations with eager loading and lazy loading
      1. Lazy loading
      2. Eager loading
    5. Using advanced association options
      1. Using scopes with associations
      2. Creating Super Many-to-Many relationships
      3. Using polymorphic associations
    6. Applying associations to Avalon Airlines
    7. Summary
  13. Chapter 5: Adding Hooks and Lifecycle Events to Your Models
    1. Technical requirements
    2. Order of operations for lifecycle events
    3. Defining, removing, and executing lifecycle events
      1. Defining instance and model lifecycle events
      2. Removing lifecycle events
      3. Executing lifecycle events
    4. Using lifecycle events with associations and transactions
    5. Putting it all together
    6. Summary
    7. References
  14. Chapter 6: Implementing Transactions with Sequelize
    1. Technical requirements
    2. Managed and unmanaged transactions
    3. Running transactions concurrently
      1. Running transactions concurrently with Sequelize
      2. Running transactions with CLS
    4. Isolation levels and advanced configuration
      1. Locking rows with Sequelize
      2. Using life cycle events for transactions
    5. Putting it all together
    6. Summary
  15. Chapter 7: Handling Customized, JSON, and Blob Data Types
    1. Technical requirements
    2. Querying JSON and JSONB data
    3. Using the BLOB column type
    4. Creating custom data types
    5. Putting it all together
    6. Summary
  16. Part 3 – Advanced Queries, Using Adapters, and Logging Queries
  17. Chapter 8: Logging and Monitoring Your Application
    1. Technical requirements
    2. Configuring logging with all of the available interfaces
    3. Integrating third-party logging applications such as Pino or Bunyan
      1. Integrating with Pino
      2. Integrating with Bunyan
    4. Collecting metrics and statistics for Sequelize with OpenTelemetry
    5. Summary
  18. Chapter 9: Using and Creating Adapters
    1. Technical requirements
    2. Installing, configuring, and integrating AdminJS with Sequelize
    3. Integrating Sequelize with GraphQL
    4. Creating our own adapter
    5. Summary
  19. Chapter 10: Deploying a Sequelize Application
    1. Technical requirements
    2. Refactoring and adding flight schedule routes
    3. Integrating Express’ static middleware and securing the admin interface
    4. Creating a page to list and book flights
    5. Deploying the application
    6. Summary
  20. Index
    1. Why subscribe?
  21. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Supercharging Node.js Applications with Sequelize
  • Author(s): Daniel Durante
  • Release date: October 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801811552