Last active
April 15, 2020 18:13
-
-
Save starfeeling/6407b6b873e9129b379045da7c0a1fff to your computer and use it in GitHub Desktop.
async sequelize transaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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