How to map a model to table using Sequelize

Configure Database.js
const Sequelize = require('sequelize')
const sequelize = new Sequelize(
   'YOUR_DB_NAME', // TutorialsPoint
   'YOUR_DB_USER_NAME', // root
   'YOUR_DB_PASSWORD', //root{
      dialect: 'mysql',
      host: 'localhost'
   }
);
module.exports = sequelize

We can use this file to define the mappings between a model and a table.

const Sequelize = require('sequelize')
const sequelize = require('../utils/database')
const User = sequelize.define('user', {
   // Name of Column #1 and its properties defined: id
   user_id:{

      // Integer Datatype
      type:Sequelize.INTEGER,

      // Increment the value automatically
      autoIncrement:true,

      // user_id can not be null.
      allowNull:false,

      // To uniquely identify user
      primaryKey:true
   },

   // Name of Column #2: name
   name: { type: Sequelize.STRING, allowNull:false },

   // Name of Column #3: email
   email: { type: Sequelize.STRING, allowNull:false },

   // Column: Timestamps
   createdAt: Sequelize.DATE,
   updatedAt: Sequelize.DATE,
})
module.exports = User

Leave a comment

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