Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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,
  }
);
sequelize.define('model', {
  states: {
    type:   Sequelize.ENUM('active', 'pending', 'deleted')
  }
})

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top