From 530d5ec2870f93c1cb96f97cbda06cf5bf97536d Mon Sep 17 00:00:00 2001 From: Elliot Scribner Date: Fri, 7 Jun 2024 14:45:33 -0700 Subject: [PATCH] DA-452: Adding _commit example, suggest await syntax for commit/rollback --- docusaurus/docs/advanced/transactions.md | 25 ++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/docusaurus/docs/advanced/transactions.md b/docusaurus/docs/advanced/transactions.md index 59f21232..4dabd673 100644 --- a/docusaurus/docs/advanced/transactions.md +++ b/docusaurus/docs/advanced/transactions.md @@ -114,17 +114,38 @@ Keep a sharp eye on it! ### Rollback -To trigger a `rollback` manually you should execute `ctx._rollback` function. +To trigger a `rollback` manually you should execute the `ctx._rollback` function. ```typescript await otttoman.$transactions(async (ctx: TransactionAttemptContext) => { const odette = Swan.create({ name: 'Odette', age: 30 }, { transactionContext: ctx }); - ctx._rollback(); + await ctx._rollback(); }) ``` This way you are canceling the transaction, so no changes inside the `$transaction` function will be committed. +:::info Important +**Be sure to `await` the `ctx._rollback()`**, instead of using promise chaining. Not doing so can lead to unexpected results and race conditions. +::: + +### Commit + +To manually commit the transaction, you should execute the `ctx._commit` function. + +```typescript +await otttoman.$transactions(async (ctx: TransactionAttemptContext) => { + const odette = Swan.create({ name: 'Gloria', age: 26 }, { transactionContext: ctx }); + await ctx._commit(); +}) +``` + +This will commit the changes inside the `$transaction` function. + +:::info Important +**Be sure to `await` the `ctx._commit()`**, instead of using promise chaining. Not doing so can lead to unexpected results and race conditions. +::: + ### Handle Error While creating a transaction you always should wrap it inside a `try catch` block and handle the exceptions.