Skip to content

Commit a3efdd7

Browse files
committed
Add documentation to CdsTransaction
1 parent 28ae679 commit a3efdd7

File tree

1 file changed

+32
-0
lines changed
  • javascript/frameworks/cap/lib/advanced_security/javascript/frameworks/cap

1 file changed

+32
-0
lines changed

javascript/frameworks/cap/lib/advanced_security/javascript/frameworks/cap/CDS.qll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,38 @@ class CdsUser extends API::Node {
586586
}
587587
}
588588

589+
/**
590+
* A transaction object to be carried out by a service that it is initialized
591+
* with through a call to `tx`. Note that there are two types of styles when
592+
* it comes to using the transaction object, that is, within a callback or in a
593+
* `try`-`catch` block:
594+
*
595+
* ``` javascript
596+
* // 1. The transaction object is bound to a callback and is used in its body.
597+
* await srv.tx({ user: someUser }, async (tx) => {
598+
* tx.run(SELECT.from`Entity`.where`attr=${attr}`);
599+
* });
600+
*
601+
* // 2. The transaction object is used in a try-catch block, in a manual manner.
602+
* let tx = this.tx();
603+
* try {
604+
* tx.run(SELECT.from`Entity`.where`attr=${attr}`);
605+
* await tx.commit();
606+
* } catch (e) {
607+
* await tx.rollback(e);
608+
* }
609+
* ```
610+
*
611+
* The former style allows for automatic transaction management by the framework
612+
* (notice there are no `commit` and `rollback` calls), while the latter style
613+
* makes the low-level transaction operations explicit.
614+
*
615+
* To accommodate for both styles, this class captures both the transaction call
616+
* itself, and the parameter of the callback.
617+
*
618+
* Note that the call to `tx` can optionally take a context object as its first
619+
* parameter that lets overriding of certain options such as `user`.
620+
*/
589621
class CdsTransaction extends SourceNode {
590622
ServiceInstance srv;
591623
CallNode txCall;

0 commit comments

Comments
 (0)