How can I have a datatype of array in mysql Sequel

2020-05-29 03:12发布

问题:

I am building an application that uses Node/Express and MySQL with Sequelize as the ORM. I want to have a datatype of Array, but the sequelize docs says this is limited to postgres only.

Basically, if I have a users table that has 3 columns for example (name, phone, favColors), I want favColors to get populated with an array of string values retrieved from the user. How can I do this?

回答1:

You can use getter/setter functions for this:

favColors: {
    type: Sequelize.STRING,
    allowNull: false,
    get() {
        return this.getDataValue('favColors').split(';')
    },
    set(val) {
       this.setDataValue('favColors',val.join(';'));
    },
}

more info: http://docs.sequelizejs.com/en/latest/docs/models-definition/#getters-setters