Skip to content

Instantly share code, notes, and snippets.

@greyscaled
Last active June 2, 2018 05:09
Show Gist options
  • Save greyscaled/bcf3566abc83ea64699ca886db2059f2 to your computer and use it in GitHub Desktop.
Save greyscaled/bcf3566abc83ea64699ca886db2059f2 to your computer and use it in GitHub Desktop.
let transaction
let teamToUpdate
try {
transaction = await sequelize.transaction() // Managed Transaction
// update the team
teamToUpdate = await Team.findOne({ where: {...}, transaction })
/* POTENTIAL ERROR - .update of null */
await teamToUpdate.update({...}, {transaction})
// Associate the team to Tags
let tagModels = []
for (let i = 0; i < teamProperties.tags.length; i++) {
/* CUSTOM ERROR no tag */
let tag = await Tag.findById(teamProperties.tags[i], {transaction})
if (!tag) throw new Error('tag not found')
tagModels.push(tag)
}
await teamToUpdate.setTags(tagModels, {transaction})
// everything worked as planned - commit the changes
await transaction.commit()
} catch (err) {
// we'll write this after
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment