squlize set new record code example
Example 1: create or update in sequelize
async function updateOrCreate (model, where, newItem) {
const foundItem = await model.findOne({where});
if (!foundItem) {
const item = await model.create(newItem)
return {item, created: true};
}
const item = await model.update(newItem, {where});
return {item, created: false};
}
Example 2: update column with find sequelize
Project.find({ where: { title: 'aProject' } })
.on('success', function (project) {
if (project) {
project.update({
title: 'a very different title now'
})
.success(function () {})
}
})