How to use Sequelize in Node.js ?
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:
- 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
- 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:
Configuring database.js file:
const Sequelize = require( 'sequelize' )
const sequelize = new Sequelize(
'DATABASE_NAME' ,
'DATABASE_USER_NAME' ,
'DATABASE_PASSWORD' , {
dialect: 'mysql' ,
host: 'localhost'
}
);
module.exports = sequelize
|
Last Updated :
20 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...