Last updated on
Apr 27, 2021
Description
This function asynchronously writes up to 25 action requests in a group.
The actions are completed atomically so that either all of them succeed, or all of them fail. They are defined by the following objects:
- Put
- Update
- Delete
- ConditionCheck
For more information, see TransactWriteItems.
Category
History
ColdFusion (2021 release): Added this function.
Syntax
serviceHandle.transactWriteItems(requestParameters)
serviceHandle.transactWriteItems(requestParameters)
serviceHandle.transactWriteItems(requestParameters)
Parameters
See request parameters of TransactWriteItems.
Example
<cfscript>
cred = {
"credentialAlias" : "myalias",
"vendorName" : "AWS",
"region" : "us-east-2",
"secretAccessKey" : "xxxxx",
"accessKeyId" : "xxxx"
}
config = {
"serviceName" = "DYNAMODB"
}
dynamo = getCloudService(cred, config)
// create table Customer
createTable_customers = {
"TableName": "CustomerUpd",
"KeySchema": [
{
"AttributeName": "CustomerId",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "CustomerId",
"AttributeType": "S"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
}
}
// create table product
createTable_product = {
"TableName": "Product",
"KeySchema": [
{
"AttributeName": "ProductId",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "ProductId",
"AttributeType": "N"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
}
}
// create table Orders
createTable_orders = {
"TableName": "Orders",
"KeySchema": [
{
"AttributeName": "OrderId",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "OrderId",
"AttributeType": "N"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
}
}
// create all three tables
dynamo.createTable(createTable_customers)
dynamo.createTable(createTable_product)
dynamo.createTable(createTable_orders)
// define structs for items to be inserted into the tables
strct_putItem1 = {
"TableName": "CustomerUpd",
"Item":{
"CustomerId": {
"S": "09e8e9c8-ec48"
}
}
}
strct_putItem2 = {
"TableName": "Product",
"Item":{
"ProductId": {
"N": 1234
},
"producttatus": {
"S": "IN_STOCK"
}
}
}
// put items in the tables
dynamo.putItem(strct_putItem1, {"hasType": true})
dynamo.putItem(strct_putItem2, {"hasType": true})
//STORE DATA IN TABLE AFTER TRANSACTION
CUSTOMER_TABLE_NAME = "CustomerUpd";
CUSTOMER_PARTITION_KEY = "CustomerId";
customerId = "09e8e9c8-ec48";
conditionCheck = {
"TableName": CUSTOMER_TABLE_NAME,
"Key": {
"#CUSTOMER_PARTITION_KEY#": {
"S": customerId
}
},
"ConditionExpression": "attribute_exists(#CUSTOMER_PARTITION_KEY#)",
"ReturnValuesOnConditionCheckFailure": "ALL_OLD"
}
PRODUCT_TABLE_NAME = "Product";
PRODUCT_PARTITION_KEY = "ProductId";
PRODUCT_PARTITION_KEY_VALUE = 1234;
updateproducttatus = {
"TableName": PRODUCT_TABLE_NAME,
"Key": {
"#PRODUCT_PARTITION_KEY#": {
"N": PRODUCT_PARTITION_KEY_VALUE
}
},
"UpdateExpression": "SET producttatus = :new_status",
"ExpressionAttributeValues": {
":new_status": {
"S": "SOLD"
},
":expected_status": {
"S": "IN_STOCK"
}
},
"ConditionExpression": "producttatus = :expected_status",
"ReturnValuesOnConditionCheckFailure": "ALL_OLD"
}
ORDER_PARTITION_KEY = "OrderId";
ORDER_TABLE_NAME = "Orders";
ORDER_PARTITION_KEY_VALUE = 740;
createOrder = {
"TableName": ORDER_TABLE_NAME,
"Item": {
"#ORDER_PARTITION_KEY#": {
"N": ORDER_PARTITION_KEY_VALUE
},
"#PRODUCT_PARTITION_KEY#": {
"N": PRODUCT_PARTITION_KEY_VALUE
},
"#CUSTOMER_PARTITION_KEY#": {
"S": customerId
},
"OrderStatus": {
"S": "CONFIRMED"
},
"OrderTotal": {
"N": 100
}
},
"ReturnValuesOnConditionCheckFailure": "ALL_OLD",
"ConditionExpression": "attribute_not_exists(#ORDER_PARTITION_KEY#)"
}
transactWriteItems = {
"TransactItems": [{
"ConditionCheck": conditionCheck
}, {
"Update": updateproducttatus
}, {
"Put": createOrder
}],
"ReturnConsumedCapacity": "TOTAL"
}
//READ DATA AFTER TRANSACTION COMPLETES
readproductold = {
"TableName": PRODUCT_TABLE_NAME,
"Key": {
"#PRODUCT_PARTITION_KEY#": {
"N": PRODUCT_PARTITION_KEY_VALUE
}
}
}
readCreatedOrder = {
"TableName": ORDER_TABLE_NAME,
"Key": {
"#ORDER_PARTITION_KEY#": {
"N": ORDER_PARTITION_KEY_VALUE
}
}
}
transactGetItems = {
"TransactItems": [
{
"Get": readproductold
},
{
"Get": readCreatedOrder
}
],
"ReturnConsumedCapacity": "TOTAL"
}
// Transact write items
try{
resultWrite = dynamo.transactWriteItems(transactWriteItems, {"hasType":true})
writeOutput("TransactionWriteItems Successful")
writeDump(resultWrite)
}
catch (any e){
writeDump(e)
}
// Transact get items
try{
resultGet=dynamo.transactGetItems(transactGetItems,{"hasType":true})
writeDump(resultGet)
}
catch (any e){
writeDump(e)
}
</cfscript>
<cfscript>
cred = {
"credentialAlias" : "myalias",
"vendorName" : "AWS",
"region" : "us-east-2",
"secretAccessKey" : "xxxxx",
"accessKeyId" : "xxxx"
}
config = {
"serviceName" = "DYNAMODB"
}
dynamo = getCloudService(cred, config)
// create table Customer
createTable_customers = {
"TableName": "CustomerUpd",
"KeySchema": [
{
"AttributeName": "CustomerId",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "CustomerId",
"AttributeType": "S"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
}
}
// create table product
createTable_product = {
"TableName": "Product",
"KeySchema": [
{
"AttributeName": "ProductId",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "ProductId",
"AttributeType": "N"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
}
}
// create table Orders
createTable_orders = {
"TableName": "Orders",
"KeySchema": [
{
"AttributeName": "OrderId",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "OrderId",
"AttributeType": "N"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
}
}
// create all three tables
dynamo.createTable(createTable_customers)
dynamo.createTable(createTable_product)
dynamo.createTable(createTable_orders)
// define structs for items to be inserted into the tables
strct_putItem1 = {
"TableName": "CustomerUpd",
"Item":{
"CustomerId": {
"S": "09e8e9c8-ec48"
}
}
}
strct_putItem2 = {
"TableName": "Product",
"Item":{
"ProductId": {
"N": 1234
},
"producttatus": {
"S": "IN_STOCK"
}
}
}
// put items in the tables
dynamo.putItem(strct_putItem1, {"hasType": true})
dynamo.putItem(strct_putItem2, {"hasType": true})
//STORE DATA IN TABLE AFTER TRANSACTION
CUSTOMER_TABLE_NAME = "CustomerUpd";
CUSTOMER_PARTITION_KEY = "CustomerId";
customerId = "09e8e9c8-ec48";
conditionCheck = {
"TableName": CUSTOMER_TABLE_NAME,
"Key": {
"#CUSTOMER_PARTITION_KEY#": {
"S": customerId
}
},
"ConditionExpression": "attribute_exists(#CUSTOMER_PARTITION_KEY#)",
"ReturnValuesOnConditionCheckFailure": "ALL_OLD"
}
PRODUCT_TABLE_NAME = "Product";
PRODUCT_PARTITION_KEY = "ProductId";
PRODUCT_PARTITION_KEY_VALUE = 1234;
updateproducttatus = {
"TableName": PRODUCT_TABLE_NAME,
"Key": {
"#PRODUCT_PARTITION_KEY#": {
"N": PRODUCT_PARTITION_KEY_VALUE
}
},
"UpdateExpression": "SET producttatus = :new_status",
"ExpressionAttributeValues": {
":new_status": {
"S": "SOLD"
},
":expected_status": {
"S": "IN_STOCK"
}
},
"ConditionExpression": "producttatus = :expected_status",
"ReturnValuesOnConditionCheckFailure": "ALL_OLD"
}
ORDER_PARTITION_KEY = "OrderId";
ORDER_TABLE_NAME = "Orders";
ORDER_PARTITION_KEY_VALUE = 740;
createOrder = {
"TableName": ORDER_TABLE_NAME,
"Item": {
"#ORDER_PARTITION_KEY#": {
"N": ORDER_PARTITION_KEY_VALUE
},
"#PRODUCT_PARTITION_KEY#": {
"N": PRODUCT_PARTITION_KEY_VALUE
},
"#CUSTOMER_PARTITION_KEY#": {
"S": customerId
},
"OrderStatus": {
"S": "CONFIRMED"
},
"OrderTotal": {
"N": 100
}
},
"ReturnValuesOnConditionCheckFailure": "ALL_OLD",
"ConditionExpression": "attribute_not_exists(#ORDER_PARTITION_KEY#)"
}
transactWriteItems = {
"TransactItems": [{
"ConditionCheck": conditionCheck
}, {
"Update": updateproducttatus
}, {
"Put": createOrder
}],
"ReturnConsumedCapacity": "TOTAL"
}
//READ DATA AFTER TRANSACTION COMPLETES
readproductold = {
"TableName": PRODUCT_TABLE_NAME,
"Key": {
"#PRODUCT_PARTITION_KEY#": {
"N": PRODUCT_PARTITION_KEY_VALUE
}
}
}
readCreatedOrder = {
"TableName": ORDER_TABLE_NAME,
"Key": {
"#ORDER_PARTITION_KEY#": {
"N": ORDER_PARTITION_KEY_VALUE
}
}
}
transactGetItems = {
"TransactItems": [
{
"Get": readproductold
},
{
"Get": readCreatedOrder
}
],
"ReturnConsumedCapacity": "TOTAL"
}
// Transact write items
try{
resultWrite = dynamo.transactWriteItems(transactWriteItems, {"hasType":true})
writeOutput("TransactionWriteItems Successful")
writeDump(resultWrite)
}
catch (any e){
writeDump(e)
}
// Transact get items
try{
resultGet=dynamo.transactGetItems(transactGetItems,{"hasType":true})
writeDump(resultGet)
}
catch (any e){
writeDump(e)
}
</cfscript>
<cfscript> cred = { "credentialAlias" : "myalias", "vendorName" : "AWS", "region" : "us-east-2", "secretAccessKey" : "xxxxx", "accessKeyId" : "xxxx" } config = { "serviceName" = "DYNAMODB" } dynamo = getCloudService(cred, config) // create table Customer createTable_customers = { "TableName": "CustomerUpd", "KeySchema": [ { "AttributeName": "CustomerId", "KeyType": "HASH" } ], "AttributeDefinitions": [ { "AttributeName": "CustomerId", "AttributeType": "S" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 } } // create table product createTable_product = { "TableName": "Product", "KeySchema": [ { "AttributeName": "ProductId", "KeyType": "HASH" } ], "AttributeDefinitions": [ { "AttributeName": "ProductId", "AttributeType": "N" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 } } // create table Orders createTable_orders = { "TableName": "Orders", "KeySchema": [ { "AttributeName": "OrderId", "KeyType": "HASH" } ], "AttributeDefinitions": [ { "AttributeName": "OrderId", "AttributeType": "N" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 } } // create all three tables dynamo.createTable(createTable_customers) dynamo.createTable(createTable_product) dynamo.createTable(createTable_orders) // define structs for items to be inserted into the tables strct_putItem1 = { "TableName": "CustomerUpd", "Item":{ "CustomerId": { "S": "09e8e9c8-ec48" } } } strct_putItem2 = { "TableName": "Product", "Item":{ "ProductId": { "N": 1234 }, "producttatus": { "S": "IN_STOCK" } } } // put items in the tables dynamo.putItem(strct_putItem1, {"hasType": true}) dynamo.putItem(strct_putItem2, {"hasType": true}) //STORE DATA IN TABLE AFTER TRANSACTION CUSTOMER_TABLE_NAME = "CustomerUpd"; CUSTOMER_PARTITION_KEY = "CustomerId"; customerId = "09e8e9c8-ec48"; conditionCheck = { "TableName": CUSTOMER_TABLE_NAME, "Key": { "#CUSTOMER_PARTITION_KEY#": { "S": customerId } }, "ConditionExpression": "attribute_exists(#CUSTOMER_PARTITION_KEY#)", "ReturnValuesOnConditionCheckFailure": "ALL_OLD" } PRODUCT_TABLE_NAME = "Product"; PRODUCT_PARTITION_KEY = "ProductId"; PRODUCT_PARTITION_KEY_VALUE = 1234; updateproducttatus = { "TableName": PRODUCT_TABLE_NAME, "Key": { "#PRODUCT_PARTITION_KEY#": { "N": PRODUCT_PARTITION_KEY_VALUE } }, "UpdateExpression": "SET producttatus = :new_status", "ExpressionAttributeValues": { ":new_status": { "S": "SOLD" }, ":expected_status": { "S": "IN_STOCK" } }, "ConditionExpression": "producttatus = :expected_status", "ReturnValuesOnConditionCheckFailure": "ALL_OLD" } ORDER_PARTITION_KEY = "OrderId"; ORDER_TABLE_NAME = "Orders"; ORDER_PARTITION_KEY_VALUE = 740; createOrder = { "TableName": ORDER_TABLE_NAME, "Item": { "#ORDER_PARTITION_KEY#": { "N": ORDER_PARTITION_KEY_VALUE }, "#PRODUCT_PARTITION_KEY#": { "N": PRODUCT_PARTITION_KEY_VALUE }, "#CUSTOMER_PARTITION_KEY#": { "S": customerId }, "OrderStatus": { "S": "CONFIRMED" }, "OrderTotal": { "N": 100 } }, "ReturnValuesOnConditionCheckFailure": "ALL_OLD", "ConditionExpression": "attribute_not_exists(#ORDER_PARTITION_KEY#)" } transactWriteItems = { "TransactItems": [{ "ConditionCheck": conditionCheck }, { "Update": updateproducttatus }, { "Put": createOrder }], "ReturnConsumedCapacity": "TOTAL" } //READ DATA AFTER TRANSACTION COMPLETES readproductold = { "TableName": PRODUCT_TABLE_NAME, "Key": { "#PRODUCT_PARTITION_KEY#": { "N": PRODUCT_PARTITION_KEY_VALUE } } } readCreatedOrder = { "TableName": ORDER_TABLE_NAME, "Key": { "#ORDER_PARTITION_KEY#": { "N": ORDER_PARTITION_KEY_VALUE } } } transactGetItems = { "TransactItems": [ { "Get": readproductold }, { "Get": readCreatedOrder } ], "ReturnConsumedCapacity": "TOTAL" } // Transact write items try{ resultWrite = dynamo.transactWriteItems(transactWriteItems, {"hasType":true}) writeOutput("TransactionWriteItems Successful") writeDump(resultWrite) } catch (any e){ writeDump(e) } // Transact get items try{ resultGet=dynamo.transactGetItems(transactGetItems,{"hasType":true}) writeDump(resultGet) } catch (any e){ writeDump(e) } </cfscript>