Sling Academy
Home/Node.js/How to Define and Use Nested Models in Sequelize.js

How to Define and Use Nested Models in Sequelize.js

Last updated: December 29, 2023

Introduction

Sequelize.js is a robust ORM (Object-Relational Mapping) library for Node.js, which facilitates the interaction with SQL databases. It allows developers to use JavaScript objects and model relationships between these objects in various types of associations. This tutorial will guide you through defining and using nested models in Sequelize.js, providing an effective way to manage complex data structures with clear parent-child relationships.

Understanding Associations

In Sequelize, associations are the foundation of nested models. Three primary types of associations can be established between models:

  • hasOne and belongsTo for one-to-one relationships.
  • hasMany for one-to-many relationships.
  • belongsToMany for many-to-many relationships.

Before diving into code examples, ensure Sequelize and its dependencies are installed and properly set up in your project.

Defining Parent and Child Models

const { Model, DataTypes } = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
  dialect: 'postgres'
});

class Parent extends Model {}
Parent.init({
  // define attributes
}, { sequelize, modelName: 'parent' });

class Child extends Model {}
Child.init({
  // define attributes
}, { sequelize, modelName: 'child' });

Establishing Associations

Parent.hasMany(Child);
Child.belongsTo(Parent);

Nesting Models with Eager Loading

Eager loading in Sequelize allows you to load associated models preemptively. This is done by using the include option in query methods.

Parent.findAll({
  include: Child
}).then(parents => {
  // Access nested children here
});

Advanced Associations

Let’s consider a more complex scenario involving multiple nested models:

class Grandparent extends Model {}
Grandparent.init({ /* attributes */ }, { sequelize, modelName: 'grandparent' });

class Parent extends Model {}
Parent.init({ /* attributes */ }, { sequelize, modelName: 'parent' });

class Child extends Model {}
Child.init({ /* attributes */ }, { sequelize, modelName: 'child' });

Grandparent.hasMany(Parent);
Parent.belongsTo(Grandparent);
Parent.hasMany(Child);
Child.belongsTo(Parent);

To perform a nested eager load, your query would look like this:

Grandparent.findAll({
  include: {
    model: Parent,
    include: [Child]
  }
}).then(grandparents => {
  // Nested parents and children are accessible here
});

Conclusion

In conclusion, Sequelize.js offers a powerful way to define and interact with nested models, opening up a fluent API to express complex model hierarchies. With this guide, you’ve learned the basics of setting up nested models and how to utilize eager loading to query associated data efficiently. Keep in mind that complex relationships may introduce performance considerations, so always optimize your queries and associations accordingly.

Next Article: How to Disable Logging SQL in Sequelize.js

Previous Article: Fixing Sequelize.js TypeError: Undefined is Not a Function

Series: Sequelize.js Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates