Transaction
Transaction
is a special class that allows you to add queries and commit them
in one single batch, rolling back if any error occurs (on the DB or in your code inside the Transaction
).
For more info please refer to The MongoDB manual
#
tl;dr - UsageThis is an example of using a transaction from Executor.
#
Class Propertiesdb
#
A Database
instance.
Transaction#constructor
#
#
Signaturewarning
You won't usually need to instanciate the Transaction object yourself. This is done for you using the
db.transaction()
method:
add
#
#
SignatureThis function takes two arguments. The first one is the query function you would like to run.
important
query
MUST be a bound callable function. Look at the example below.
Usually you will need to bind your function to keep this
intact.
The rest (args
) are all the arguments you would like to pass to your query function when running it.
warning
When running a transaction, the transaction method will run query by query, checking the length
of each
query (meaning the number of arguments it can take).
For Transaction
the LAST argument is ALWAYS the options
passed to the driver. Which means that if the last argument is provided, Transaction
will try to merge it and add its session
.
If there are any missing arugments between the provided list and the last one, Transaction
will fill them with undefined
.
Example
commit
#
#
SignatureCommits the transaction using db.atomic()
.
This method will run every query function passed to it, and return a Promise.all
of the lot.