Skip to main content

Transaction

Dynamoose supports running DynamoDB Transactions in your database. Transactions are all or nothing, meaning the entire transaction will succeed, or the entire transaction will fail. In the event the transaction fails, the state of the database will be the exact same as if the transaction didn't take place at all (ex. no items will have been modified). You are also able to run these Transactions across multiple tables.

dynamoose.transaction(transactions[, settings][, callback])‚Äč

You can use dynamoose.transaction to run a transaction on your table. This method uses either the transactGetItems or transactWriteItems DynamoDB API call to run the transaction.

The transactions parameter must be an array of transaction objects that will be passed into the DynamoDB API. The standard way to get these is by using the Model.transaction methods. You can also pass Promises into this array that will resolve to an object, and Dynamoose will wait for those promises to be resolved before proceeding.

If you pass RAW objects into the transactions array (without using Model.transaction) you must be sure that the given model has been registered with Dynamoose at some point so we can convert the response to Item instances of that Model.

This method returns a promise that will resolve when the operation is complete, this promise will reject upon failure. You can also pass in a function into the callback parameter to have it be used in a callback format as opposed to a promise format.

You can also pass in an object for the optional settings parameter that is an object. The table below represents the options for the settings object.

NameDescriptionTypeDefault
returnWhat the function should return. Can be items, or request. In the event this is set to request the request Dynamoose will make to DynamoDB will be returned, and no request to DynamoDB will be made.Stringitems
typeIf Dynamoose should use get (transactGetItems) or write (transactWriteItems) to make the transaction call.String (get | write)By default, if all transactions objects are Get, get will be run, otherwise write will be used.
await dynamoose.transaction([
User.transaction.update({"id": "user1"}, {"$ADD": {"balance": -100}}),
Charge.transaction.create({"userid": "user1", "product": "product1", "amount": 100, "status": "successful"}),
Product.transaction.update({"id": "product1"}, {"$ADD": {"inventory": -1}}),
Credit.transaction.delete({"id": "credit1"}),
{
"Delete": {
"Key": {
"id": {
"S": "helloworld"
}
},
"TableName": "MyOtherTable"
}
}
]);