在Sequelize.js中定义模型之后,我们如何获得它的ENUM值?例如,我们将模型定义为:
sequelize.define('model', { states: { type: Sequelize.ENUM, values: ['active', 'pending', 'deleted'] } })
我们如何从这个模型中获得预定义的['active', 'pending' ,'deleted']值?
['active', 'pending' ,'deleted']
n6lpvg4x1#
架构中的ENUM值可以在模型的rawAttributes属性中找到。
rawAttributes
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
50few1ms2#
sequelize.define('model', { states: { type: Sequelize.ENUM('active', 'pending', 'deleted') } })
来源:https://sebhastian.com/sequelize-enum/
g52tjvyc3#
像这样创建JavaScript枚举对象
module.exports.BookingStatus = Object.freeze({ Done : 'Done', Pending : 'Pending', Rejected : 'Rejected' });
接下来,创建具有枚举的sequalize Schema
const Booking = sequalize.define( 'booking', { customerId : DataTypes.STRING, bookingStatus : { type : DataTypes.ENUM, values : Object.values(this.BookingStatus), defaultValue : this.BookingStatus.Pending }, }, { timestamps: true, } );
3条答案
按热度按时间n6lpvg4x1#
架构中的ENUM值可以在模型的
rawAttributes
属性中找到。50few1ms2#
来源:https://sebhastian.com/sequelize-enum/
g52tjvyc3#
像这样创建JavaScript枚举对象
接下来,创建具有枚举的sequalize Schema