44

How do we get the ENUM values of a model after defining it in Sequelize.js?

For example, we define our model as:

sequelize.define('model', {
  states: {
    type:   Sequelize.ENUM,
    values: ['active', 'pending', 'deleted']
  }
})

How do we get the pre-defined ['active', 'pending' ,'deleted'] values from this model?

3 Answers 3

84

The ENUM values in a schema can be found in the rawAttributes property of the model.

var Model = sequelize.define('model', {
  states: {
    type:   Sequelize.ENUM,
    values: ['active', 'pending', 'deleted']
  }
});

console.log(Model.rawAttributes.states.values);
// logs ['active', 'pending', 'deleted'] in console
Sign up to request clarification or add additional context in comments.

Comments

0

Create JavaScript Enum Object like that

module.exports.BookingStatus = Object.freeze({
  Done: 'Done',
  Pending: 'Pending',
  Rejected: 'Rejected'
});

Next, is to create sequalize Schema having enum

const Booking = sequalize.define(
  'booking',
  {
    customerId : DataTypes.STRING,
    bookingStatus : {
      type : DataTypes.ENUM,
      values : Object.values(this.BookingStatus),
      defaultValue :  this.BookingStatus.Pending
    },
  },
  {
    timestamps: true,
  }
);

Comments

-2
sequelize.define('model', {
  states: {
    type:   Sequelize.ENUM('active', 'pending', 'deleted')
  }
})

source: https://sebhastian.com/sequelize-enum/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.