Open In App

How to use Sequelize in Node.js ?

Last Updated : 20 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. Its features are solid transaction support, relations, eager and lazy loading, read replication and many more.

Features of Sequelize:

  • Sequelize is a third-party package to be precise its an Object-Relational Mapping Library(ORM)..
  • Standardization ORMs usually have a single schema definition in the code. This makes it very clear what the schema is, and very simple to change it.
  • No need to learn SQL – queries are written in plain JavaScript.

Setting up a Node.js app:

  • Start Node.js app using the following command:
    npm init -y

Installation of Sequelize:

  1. Sequelize needs MySql module installed in your project. if you have not installed MySql module then make sure before installing Sequelize you need to install MySql2 module. You need to install this module by using the following command.
    npm install mysql2
  2. After installing the MySql2 module, we have to install Sequelize module to 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



Similar Reads

Node.js MySQL-Create Table Using Sequelize
Introduction to Sequelize: Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. Its features are solid transaction support, relations, eager and lazy loading, read replication and many more. Connect to MySql Database using Sequelize: To establish connection between MySQL and Node.js using Sequelize
3 min read
Comparison of Popular JavaScript ORMs: Prisma, TypeORM, and Sequelize
If you're unfamiliar with ORM, it's a powerful tool that allows us to translate database logic into simple JavaScript code. Throughout this article, we will discuss the pros and cons of each ORM and delve into specific use cases where one may be more suitable than the others. Prisma: Modern and LightweightPrisma is an ORM that has gained significan
4 min read
When to use interfaces and when to use classes in TypeScript ?
TypeScript supports object-oriented programming features like classes and interfaces etc. classes are the skeletons for the object. it encapsulates the data which is used in objects. Interfaces are just like types for classes in TypeScript. It is used for type checking. It only contains the declaration of members of the object. It helps in deriving
4 min read
How to use SSL/TLS with Node.js ?
TLS/SSL is used for establishing secure connections over the internet. Today, most websites use HTTPS to communicate with clients. HTTPS is basically HTTP running over TLS/SSL. Web clients like browsers alert users about websites that do not use HTTPS since such websites are vulnerable to cyber-attacks. As a result, people refrain from visiting suc
5 min read
When to use next() and return next() in Node.js ?
In this is article we will see when to use next() and return next() in NodeJS. Features: next() : It will run or execute the code after all the middleware function is finished.return next() : By using return next it will jump out the callback immediately and the code below return next() will be unreachable. Syntax: next() return next() app.use((req
2 min read
Use EJS as Template Engine in Node.js
EJS: EJS or Embedded Javascript Templating is a templating engine used by Node.js. Template engine helps to create an HTML template with minimal code. Also, it can inject data into an HTML template on the client side and produce the final HTML. EJS is a simple templating language that is used to generate HTML markup with plain JavaScript. It also h
4 min read
How to check if email address is already in use or not using express-validator in Node.js ?
The registration or Sign Up in any website always requires a username. Most of the time we use 'email' to register on a website. The registration email is always unique and must refer to only one user otherwise conflict between the users can happen. To solve this conflict every website must have the functionality to not accept the email that alread
5 min read
How to use font-awesome icons from Node.js-modules?
Font-awesome is a web's icon library that gives you scalable vector icons that can be customized in terms of color, size, and in many more aspects. Many companies integrate this library icon's on their websites. It has 600+ icons and every icon is mobile and desktop responsive irrespective of the resolution screen size. It also gives us to use anim
1 min read
How to use EcmaScript modules in Node.js ?
CommonJS modules consists usage of exports and require() statements, while EcmaScript modules consists usage of import and export statements. Learn more here: Difference between node.js require and ES6 import and export Node.js treats JS code as CommonJS modules by default, However the EcmaScript modules can be used instead of using --experimental-
1 min read
How to use jQuery with Node.js ?
jQuery is a JavaScript library, which provides us with the same functionality as vanilla JavaScript but in fewer lines of code. The need for jQuery is reduced as doing things became much simpler in vanilla JavaScript with updates. Although its popularity is decreasing, still around 76% of the projects use jQuery. Our Aim is to use jQuery with Node.
3 min read