Home > Mobile >  How to adding default value to Sequelize model with NodeJS?
How to adding default value to Sequelize model with NodeJS?

Time:09-21

i have created a controller that to adding new value into the database, but now i do not know how to set up the default value into Sequelize. Let me demonstrate my code below

Model User:

module.exports = (sequelize, Sequelize) => {
  const User = sequelize.define("user", {
    user_id: {
      type: Sequelize.STRING,
      autoIncrement: true,
      primaryKey: true,
    },
    username: {
      type: Sequelize.STRING,
    },
    password: {
      type: Sequelize.STRING,
    },
    full_name: {
      type: Sequelize.STRING,
    },
    email: {
      type: Sequelize.STRING,
    },
    role_id: {
      type: Sequelize.STRING,
    },
    is_active: {
      type: Sequelize.BOOLEAN,
    },
    created_date: {
      type: Sequelize.DATE,
    },
    created_by: {
      type: Sequelize.STRING,
    },
    updated_date: {
      type: Sequelize.DATE,
    },
    updated_by: {
      type: Sequelize.STRING,
    },
  });

  return User;
};

Model Role:

module.exports = (sequelize, Sequelize) => {
  const Role = sequelize.define("role", {
    role_id: {
      type: Sequelize.STRING,
      autoIncrement: true,
      primaryKey: true,
    },
    role_name: {
      type: Sequelize.STRING,
    },
    is_active: {
      type: Sequelize.BOOLEAN,
    },
    created_date: {
      type: Sequelize.DATE,
    },
    created_by: {
      type: Sequelize.STRING,
    },
    updated_date: {
      type: Sequelize.DATE,
    },
    updated_by: {
      type: Sequelize.STRING,
    },
  });

  return Role;
};

Controller User:

const db = require("../models");
const User = db.users;

exports.create = (req, res) => {
    // Validate request
    if (!req.body.username) {
      res.status(400).send({
        message: "Content can not be empty!"
      });
      return;
    }
  
    // Create a Tutorial
    const user = {
        username: req.body.username,
        password: req.body.password,
        full_name: req.body.full_name,
        email: req.body.email,
        role_id: req.body.role_id,
        is_active: req.body.is_active,
        created_date: req.body.created_date,
        created_by: req.body.created_by,
        updated_date: req.body.updated_date,
        updated_by: req.body.updated_by,
    };
  
    // Save Tutorial in the database
    User.create(user)
      .then(data => {
        res.send(data);
      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while creating new User."
        });
      });
  };

Now i need help to add the default value that every value MUST BE Role is USER and is_active = 1. If you have any ideas please help me or give my some suggestions so i can complete this problem easier. And, i hope you can upvote this post. Thank you

CodePudding user response:

You can define the default value at the database level in your migration file. Or, you can also define at sequelize level in your model definition itself.

module.exports = (sequelize, Sequelize) => {
  const User = sequelize.define("user", {
    user_id: {
      type: Sequelize.STRING,
      autoIncrement: true,
      primaryKey: true,
    },
role_name: {
      type: Sequelize.STRING,
      defaultValue: 'USER'
    },
    is_active: {
      type: Sequelize.BOOLEAN,
      defaultValue: true
    }
    });

  return User;
};

https://sequelize.org/docs/v6/core-concepts/model-basics/#default-values

  • Related