Jignasha Royala
Jignasha Royala

Reputation: 1030

How to append nodejs sequelize model list of json field

How to append a list of JSON filed in nodejs Sequelize

database: Postgres

I have one model field like that :

student_list:
    {
        type: Sequelize.ARRAY(Sequelize.JSON),
        allowNull: true
    },

I am trying to append Sequelize Array of JSON like this way but not get sucessfully append list:

var data =  ({'share_by':"aaa",'list_name':"list_name"})
student.update( 
                {'student_list': Sequelize.fn('array_append', Sequelize.col('student_list'), data)},
                {'where': {studet_id: student_id}}
                );

how to do this?

Upvotes: 2

Views: 2725

Answers (1)

William Prigol Lopes
William Prigol Lopes

Reputation: 1889

You can do a find() and get the element value, update the element and do a update with data updated. Like this:

// Get the actual student data
const student = await student.findOne(studentId);

// Spread and add the new value
const student_list = {
   ...student_list,
   data
};

// Update the field
await student.update(
    {
        student_list
    },
    {
        where: {
            student_id
        }
    }
);

Upvotes: 3

Related Questions