afterChange hook to update the stock count of related Items. If an error occurs when updating an Item and an HTTP error is returned to the user, you would not want the new Order to be persisted or any other items to be changed either. This kind of interaction with the database is handled seamlessly with transactions.
By default, FrogBot will use transactions for all data changing operations, as long as it is supported by the configured database. Database changes are contained within all FrogBot operations and any errors thrown will result in all changes being rolled back without being committed. When transactions are not supported by the database, FrogBot will continue to operate as expected without them.
MongoDB requires a connection to a replicaset in order to make use of transactions.
Transactions in SQLite are disabled by default. You need to pass
transactionOptions: {} to enable them.req.transactionID. If you have a hook that interacts with the database, you can opt in to using the same transaction by passing the req in the arguments. For example:
Async Hooks with Transactions
Since FrogBot hooks can be async and be written to not await the result, it is possible to have an incorrect success response returned on a request that is rolled back. If you have a hook where you do notawait the result, then you should not pass the req.transactionID.
Direct Transaction Access
When writing your own scripts or custom endpoints, you may wish to have direct control over transactions. This is useful for interacting with your database outside of FrogBot’s Local API. The following functions can be used for managing transactions:frogbot.db.beginTransaction- Starts a new session and returns a transaction ID for use in other FrogBot Local API calls.frogbot.db.commitTransaction- Takes the identifier for the transaction, finalizes any changes.frogbot.db.rollbackTransaction- Takes the identifier for the transaction, discards any changes.
req object to pass the transaction ID through to the database adapter. If you are not using the req object, you can make a new object to pass the transaction ID directly to database adapter methods and Local API calls.
Example:
Disabling Transactions
If you wish to disable transactions entirely, you can do so by passingfalse as the transactionOptions in your database adapter configuration. All the official FrogBot database adapters support this option.
In addition to allowing database transactions to be disabled at the adapter level. You can prevent FrogBot from using a transaction in direct calls to the Local API by adding disableTransaction: true to the args. For example: