Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to adding default value to Sequelize model with NodeJS?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading