# Sequelize Model Instance

1. **<mark>Introduction</mark>**  
    In Sequelize, a popular Node.js ORM for relational databases like MySQL, PostgreSQL, SQLite, and others, **Model Instances** represent individual records (rows) within a table in your database.  
    In Sequelize, model instances represent individual records in a table. They encapsulate both the data for the record and methods to interact with that record within the database, providing a powerful way to work with data in a more object-oriented manner.
    
2. **<mark>Model Definition</mark>**  
    A model in Sequelize corresponds to a table in the database. You define a model by extending Sequelize's `Model` class and specifying the attributes (columns) of the table.
    
    ```javascript
    const { Sequelize, DataTypes, Model } = require('sequelize');
    const sequelize = new Sequelize('database_name', 'username', 'password', {
       host: 'localhost',
       dialect: 'postgres',
       port: 5432,  // The default port for PostgreSQL
     });
    
    const User = sequelize.define(
      'User',
      {
        // Model attributes are defined here
        firstName: {
          type: DataTypes.STRING,
          allowNull: false,
        },
        lastName: {
          type: DataTypes.STRING,
          // allowNull defaults to true
        }
      },
      {
        // Other model options
        sequelize, // pass the sequelize instance
        modelName: 'User' // Name of the model
      },
    );
    ```
    
3. **<mark>Creating an Instance</mark>**
    
    A model instance is created by calling the `create` method or by instantiating a new instance using `new Model()` and then saving it.
    
    ```javascript
    // Using create method
    const user = await User.create({ firstName: 'John', lastName: 'Doe' });
    console.log(user instanceof User); // true
    console.log(user.firstName); // "John"
    ```
    
4. **<mark>Instance Methods</mark>**
    
    Model instances come with built-in methods for interacting with the database, such as `save`, `destroy`, and `update`.
    
    You can also define custom instance methods within the model definition.
    
    ```javascript
    // Built-in methods
    user.lastName = 'Smith';
    await user.save(); // Updates the record in the database
    await user.destroy(); // Deletes the record from the database
    ```
    
5. **<mark>Reloading an Instance</mark>**
    
    If you need to refresh an instance with the latest data from the database, you can use the `reload` method.
    
    ```javascript
    await user.reload();
    ```
    
6. **<mark>Validations</mark>**
    
    Sequelize allows you to define validations on model attributes. These validations are automatically run before an instance is saved.
    
    ```javascript
    User.init({
      firstName: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          notEmpty: true
        }
      },
    });
    ```
    
7. **<mark>Instance Scopes</mark>**
    
    You can define scopes to automatically include certain conditions or attributes in your queries when working with model instances.
    
    ```javascript
    const users = await User.scope('active').findAll();
    ```
