Skip to content

Instantly share code, notes, and snippets.

@starfeeling
Last active April 15, 2020 18:13
Show Gist options
  • Save starfeeling/6407b6b873e9129b379045da7c0a1fff to your computer and use it in GitHub Desktop.
Save starfeeling/6407b6b873e9129b379045da7c0a1fff to your computer and use it in GitHub Desktop.
async sequelize transaction
const findUser = await models.Users.findOne({
where: {
ds_id: user.id
}
})
const transaction = await models.sequelize.transaction()
try {
// Create
await models.Users.create(data, transaction)
// Update
await models.Users.update(data, {where}, transaction)
// Save
findUser.avatar = avatar
await findUser.save(transaction)
// Destroy
await models.Users.destroy({where: {id}, transaction})
await transaction.commit()
} catch (e) {
await transaction.rollback()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment