What is Models in Sequelize ORM

  • In Sequelize, a model is a representation of a table in your database. Through a model, Sequelize provides a way to interact with the corresponding table via a high-level API for creating, querying, updating, and deleting records. Essentially, a model acts as a bridge between the object-oriented world of JavaScript and the relational world of databases.
Key Concepts of a Sequelize Model:
  • Definition: Models are defined by specifying their name and attributes. Each attribute of a model represents a column in the database table. The definition includes the data type of each attribute, and optionally, constraints and validations.
  • Synchronization: Sequelize models can automatically synchronize with the database. This means that based on your model definitions, Sequelize can create or alter the database tables to match the models, making database schema management easier.
  • Instances: Instances of a model represent rows in the corresponding table. You can create, modify, and save instances using Sequelize methods, which translates into INSERT, UPDATE, or DELETE operations in the database.
  • CRUD Operations: Models provide methods for creating, reading, updating, and deleting records. These include `create()`, `findAll()`, `findByPk()`, `update()`, and `destroy()`, among others.
  • Associations: Models can be associated with one another to reflect the relationships between tables in the database (e.g., one-to-one, one-to-many, many-to-many). Sequelize provides methods to work with these associations, such as adding and removing associated items.
Example of Defining a Model:
  • Here's a basic example of defining a `User` model in Sequelize:


    const { Sequelize, DataTypes } = require('sequelize');
    const sequelize = new Sequelize('sqlite::memory:'); // Example for SQLite

    const User = sequelize.define('User', {
        // Model attributes are defined here
        firstName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        lastName: {
            type: DataTypes.STRING
            // allowNull defaults to true
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        }
    }, {
        // Other model options go here
    });

    module.exports = User;

  • In this example, a `User` model is defined with `firstName`, `lastName`, and `email` attributes. This model corresponds to a table in the database with the same name as the model by default, though this can be customized.
Working with Models:
  • Once defined, you can use the model to perform database operations. For example, to create a new user, you can do the following:


    User.create({
        firstName: 'Jane',
        lastName: 'Doe',
        email: 'jane.doe@example.com'
    }).then(jane => {
        console.log(jane.toJSON());
    });

  • This code creates a new instance of the `User` model and saves it to the database, effectively inserting a new row into the `users` table.
  • Models are a central concept in Sequelize, providing a powerful and flexible way to work with database data in a structured and secure manner.

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...