NodeJS 从已定义的模型中获取Sequelize.js ENUM值

r3i60tvu  于 2023-03-17  发布在  Node.js
关注(0)|答案(3)|浏览(224)

在Sequelize.js中定义模型之后,我们如何获得它的ENUM值?
例如,我们将模型定义为:

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

我们如何从这个模型中获得预定义的['active', 'pending' ,'deleted']值?

n6lpvg4x

n6lpvg4x1#

架构中的ENUM值可以在模型的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
50few1ms

50few1ms2#

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

来源:https://sebhastian.com/sequelize-enum/

g52tjvyc

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,
        }
    );

相关问题