0
votes

I'm building an application using nodejs, sequelize and Postgresql. I used the sequelize cli to manage migrations but also need to use getters and setters in order to add an array of ids which i want to store (set) as a string and return them after as an array again (get). In the documentation I can only find a pretty basic example. Basically i don't know how to correctly apply get and set in the model and in the migration document. Thanks.

1

1 Answers

1
votes

you can do like this .

let Product = database.define('Product', {
    name: {
        type: Sequelize.STRING(50)
    }
}, {
    paranoid: true,

    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: 'deleted_at',

    getterMethods: {
        created_at() {
            return moment.utc(this.getDataValue('created_at')).format('YYYY-MM-DD HH:mm:ss');
        },
        updated_at() {
            return moment.utc(this.getDataValue('updated_at')).format('YYYY-MM-DD HH:mm:ss');
        }
    },

    setterMethods: {
        // setter methods
    }
});

Product.associate = (models) => {
    // your associations
}

hope this help !