Created
August 14, 2016 20:25
-
-
Save ataube/580c1a2f14343c24eb2923949e230378 to your computer and use it in GitHub Desktop.
Loopback async/await transaction example
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
const Transaction = require('loopback-datasource-juggler'); | |
const update = async (ids, delta) => { | |
const result = []; | |
const tx = await models.MyModel.beginTransaction({ isolationLevel: Transaction.READ_COMMITTED }); | |
try { | |
for (const id of ids) { | |
const entity = await updateById(id, delta, { transaction: tx }); | |
result.push(entity); | |
} | |
await tx.commit(); | |
} catch (e) { | |
await tx.rollback(); | |
throw e; | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! This really helped me