How to use Sequelize in Node.js

Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. 

Installation of Sequelize

we have to install to Sequelize module install this module by using the following command.

npm install sequelize

Requiring module

You need to include Sequelize module in your project by using these lines.

const Sequelize = require(‘sequelize’);

Configuring database.js file:

// Include Sequelize module
const Sequelize = require('sequelize')
  
// Creating new Object of Sequelize
const sequelize = new Sequelize(
    'DATABASE_NAME',
    'DATABASE_USER_NAME',
    'DATABASE_PASSWORD', {
  
        // Explicitly specifying 
        // mysql database
        dialect: 'mysql',
  
        // By default host is 'localhost'           
        host: 'localhost'
    }
);
  
// Exporting the sequelize object. 
// We can use it in another file
// for creating models
module.exports = sequelize

Leave a comment

Your email address will not be published. Required fields are marked *