Apply for Zend Framework Certification Training

NodeJs





Step 1 Add in user model

'use strict';
var bcrypt = require('bcryptjs');
module.exports = (sequelize, DataTypes) => {
  const Users = sequelize.define('Users', {
    name: {
      type: DataTypes.STRING,
      required: true
    },
    lName: {
      type: DataTypes.STRING
    },
    email: {
      type: DataTypes.STRING,
      required: true,
      unique: true
    },
    password: {
      type: DataTypes.STRING,
      required: true
    },
    mobileNumber: {
      type: DataTypes.STRING
    },
    isActive: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1
    },
    isModVerified: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    companyName: {
      type: DataTypes.STRING,
    }
  }, {
    hooks: {
      beforeCreate: (user, options) => {
        {
          user.password = user.password && user.password != "" 
                        ? bcrypt.hashSync(user.password, 10) : "";
        }
      }
    },
    underscored: true
  });
  Users.associate = function (models) {
    Users.hasMany(models.CandidateDetails, {
      as: 'CandidateDetails',
      foreignKey: 'userId',
      constraints: false
    })
  };
  return Users;
};
Step 2 File name C:\xampp\htdocs\demo-wabatech\wabatech-api\models\candidateDetails.js
'use strict';
module.exports = (sequelize, DataTypes) => {
  const CandidateDetails = sequelize.define('CandidateDetails', {
    candidate_details_id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true
    },
    profilePicture: {
      type: DataTypes.STRING,
      required: true

    },
    languageProficiencyLevel: {
      type: DataTypes.STRING
    },
    companySize: {
      type: DataTypes.STRING
    },
    totalExp: {
      type: DataTypes.INTEGER,
      defaultValue: 0
    }
  }, {
    underscored: true
  });
  CandidateDetails.associate = function (models) {
    // associations can be defined here
    CandidateDetails.belongsTo(models.Users, {
      as: 'User',
      foreignKey: 'userId',
      constraints: false
    })
    CandidateDetails.hasMany(models.CandidateAwardDetails, {
      as: 'awardDetails',
      foreignKey: 'candidate_details_id',
      constraints: false
    })
    CandidateDetails.hasOne(models.companySourceStatus, {
      as: 'companySourceStatus',
      foreignKey: 'candidate_detail_id',
      constraints: false
    })
  };
  return CandidateDetails;
};

< update using sequilize in nodeJs NodeJS Program Lifecycle >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top