Suggestion: add/document a 'using' function for transactions #786
Replies: 3 comments
-
I'm intrigued but I'm not sure I like the API as described. I'm not sure if this would work better as a standalone helper function or a method on the Transaction or Database class. |
Beta Was this translation helpful? Give feedback.
-
Yeah, it could probably benefit from being an internal function. It could simplify the example above to something like this: async function transactionExample(db) {
const collection1 = db.collection('collection1');
const collection2 = db.collection('collection2');
await db.transaction([collection1, collection2], async trx => {
await transaction.run(() => collection1.save({});
await transaction.run(() => collection2.save({});
});
} |
Beta Was this translation helpful? Give feedback.
-
A potentially more elegant solution would also be to use an async generator where you I haven't tested this but this would be potentially how it would look: async function transactionExample(db) {
const collection1 = db.collection('collection1');
const collection2 = db.collection('collection2');
await db.transaction([collection1, collection2], async trx => {
yield () => collection1.save({});
yield () => collection2.save({});
});
} |
Beta Was this translation helpful? Give feedback.
-
Found myself using this approach and thought it'd be useful for others. I'm not sure where it'd fit in the arangojs API's but it's still pretty handy.
To help developers using the streaming transaction API's, we can write a pretty simple "using" function like they have in C#:
This would ensure that we don't forget to actually commit/abort the transactions. An example use case for this is:
Beta Was this translation helpful? Give feedback.
All reactions