Reputation: 8100
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?
Upvotes: 44
Views: 42081
Reputation: 9
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,
}
);
Upvotes: 0
Reputation: 1
sequelize.define('model', {
states: {
type: Sequelize.ENUM('active', 'pending', 'deleted')
}
})
source: https://sebhastian.com/sequelize-enum/
Upvotes: -2
Reputation: 8100
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
Upvotes: 83