Introduction
Transactions make sure that a group of database actions all happen together or not at all. This keeps data correct and safe.
Transactions make sure that a group of database actions all happen together or not at all. This keeps data correct and safe.
TransactWriteItems {
TransactItems: [
{ Put: { TableName: 'TableName', Item: { /* item attributes */ } } },
{ Update: { TableName: 'TableName', Key: { /* key attributes */ }, UpdateExpression: 'string', ExpressionAttributeValues: { /* values */ } } },
{ Delete: { TableName: 'TableName', Key: { /* key attributes */ } } }
]
}TransactWriteItems {
TransactItems: [
{ Put: { TableName: 'Users', Item: { UserId: '123', Name: 'Alice' } } },
{ Update: { TableName: 'Accounts', Key: { AccountId: 'abc' }, UpdateExpression: 'SET Balance = Balance - :amount', ExpressionAttributeValues: { ':amount': 100 } } }
]
}TransactWriteItems {
TransactItems: [
{ Delete: { TableName: 'Cart', Key: { CartId: 'cart123' } } },
{ Put: { TableName: 'Orders', Item: { OrderId: 'order456', Status: 'Placed' } } }
]
}This transaction transfers 50 units from account A1 to A2. Both updates happen together or not at all.
TransactWriteItems {
TransactItems: [
{ Update: { TableName: 'Bank', Key: { AccountId: 'A1' }, UpdateExpression: 'SET Balance = Balance - :amt', ExpressionAttributeValues: { ':amt': 50 } } },
{ Update: { TableName: 'Bank', Key: { AccountId: 'A2' }, UpdateExpression: 'SET Balance = Balance + :amt', ExpressionAttributeValues: { ':amt': 50 } } }
]
}Transactions help avoid partial updates that can cause data errors.
They are useful when multiple related changes must be consistent.
Transactions group multiple actions to happen all at once.
This ensures data stays correct and safe.
If one action fails, none of the changes happen.
{
TransactItems: [
{ Put: { TableName: 'Users', Item: { UserId: '123', Name: 'Alice' } } },
{ Put: { TableName: 'Orders', Item: { OrderId: 'abc', Amount: 50 } } }
]
}Pass the test and this pattern moves unknown