Last active
June 2, 2018 05:09
-
-
Save greyscaled/bcf3566abc83ea64699ca886db2059f2 to your computer and use it in GitHub Desktop.
This file contains 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
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