I tried upsert data using sequelize mode but it will be created duplicated.
what i want if row is not exists insert and if exists update the row.
here is my sequelize model :
module.exports = (sequelize, DataTypes) => {
return sequelize.define('Contact', {
id: {
type: DataTypes.UUID,
field: 'id',
allowNull: false,
autoIncrement: true,
primaryKey: true
},
first_name: {
type: DataTypes.STRING(100),
field: 'first_name',
allowNull: true
},
external_id: {
type: DataTypes.STRING(255),
field: 'external_id',
allowNull: false,
primaryKey: true
}
}, {
schema: 'ec3owner',
tableName: 'contact',
timestamps: false
});
};
and Sequelize method for upsert :
function upsertRow(bodyParams) {
console.log('bodyParams', bodyParams);
Contact.upsert(bodyParams, {
where: {
external_id: bodyParams.external_id
},
individualHooks: true
})
.then(function(row) {
console.log('row', row);
})
}
Version used :
postgres : 9.6
package.json
"sequelize": "^3.30.4"
"pg": "^6.1.0",