Node.js MySQL-Create Table Using Sequelize – GeeksforGeeks

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, visit How to use Sequelize in Node.js.
  • After getting connected successfully, we would have required three files:
    1. SequelizeDemo>app.js which is our root file.
    2. SequelizeDemo>utils>database.js which is responsible for MySql connection.
    3. SequelizeDemo>models>user.js which is responsible for defining the model.

Configure database.js: SequelizeDemo>utils>database.js 

  • Establish connection is the way mentioned in article How to use Sequelize in Node.js and now, we can use that exported sequelize object from SequelizeDemo>utils>database.js to define our models.

Note: Make sure the database that you are using is created in your database.

database.js file will look like this.

Configure user.js:  

  • Use SequelizeDemo>models>user.js file to define mappings between a model and a table, use the define method.

Javascript




const Sequelize = require('sequelize')

  

const sequelize = require('../utils/database')

  

const User = sequelize.define('user', {

  

    

    

    

    user_id:{

  

        

        type:Sequelize.INTEGER,

  

        

        autoIncrement:true,

  

        

        allowNull:false,

  

        

        primaryKey:true

    },

  

    

    name: { type: Sequelize.STRING, allowNull:false },

  

    

    email: { type: Sequelize.STRING, allowNull:false },

  

    

    

    myDate: { type: Sequelize.DATE, 

            defaultValue: Sequelize.NOW },

  

     

     createdAt: Sequelize.DATE,

     updatedAt: Sequelize.DATE,

})

  

module.exports = User



  • To know more about Sequelize Data Types visit Datatypes.
  • In SequelizeDemo>models>user.js file, we have defined the model.

Configure app.js:  

  • Filename => SequelizeDemo>app.js 
  • To create the model, we can use one of the following way- 
    • sync() Method: This method will create model if the model does not exist, however, if already exist it would not overwrite it.
    • sync({force:true}) Method: This method will create model if the model does not exist, however, if already exist it would overwrite it. 

Javascript




const sequelize = require('./utils/database'

    

const User = require('./models/user) 

    

    

sequelize.sync()  

    

sequelize.sync({force:true})



  • Database Synchronization: When starting a new project you won’t have a database structure and using Sequelize you won’t need to. Just specify your model structures and let the library do the rest.

Steps to run the program:  

  • The project structure will look like this: 
     

Project Structure

  • Install required modules for this project: 
npm install mysql2
npm install sequelize
  • Execute app.js (root file) using below command: 
node app.js

Output

  • In MySql Database, now we can describe User model we have created using Sequelize. Use the below commands 
use database geeksforgeeks
desc users;

db

 

My Personal Notes

arrow_drop_up