Skip to main content

transactUpdate

Introduction

transactUpdate method performs transact update request using aws-sdk TransactWriteItemsCommand.

execute

transactUpdate takes 2 arguments:

  1. (required) list of object which must include:
  2. (optionnal) options
const res = await User.transactUpdate(
[
{
condition: {
id: "user-1",
},
set: {
age: {
$incr: 1,
},
},
},
{
condition: {
id: "user-2",
sex: "female",
},
set: {
"friends[0]": "user-1",
},
},
],
{ ReturnConsumedCapacity: "TOTAL" }
);

options

options is an object which accepts TransactWriteItemsCommandInput options and DynamoQL specific options.

exec

exec boolean directive to execute or not the actual request.
When false transactUpdate will return TransactWriteItemsCommandInput object.

import type { TransactWriteItemsCommandInput } from "@aws-sdk/client-dynamodb";

const cmd: TransactWriteItemsCommandInput = await User.transactUpdate(
[
{
condition: {
id: "user-1",
},
set: {
firstname: "John",
},
},
{
condition: {
id: "user-2",
age: 22,
},
set: {
firstname: "Sara",
},
},
],
{ exec: false }
);

check

ConditionCheck on the same table.

const res = await User.transactUpdate(
[
{
condition: {
id: "user-1",
age: 27,
firstname: "John",
},
set: {
sex: "male",
},
},
{
condition: {
id: "user-2",
firstname: {
$neq: "Sara",
},
},
set: {
age: 22,
firstname: "Sara",
sex: "female",
friends: ["user-1"],
isActive: true,
},
},
],
{
check: [
{
id: "user-64",
isActive: false,
},
],
}
);

If you need ConditionCheck on another table, use { exec: false } then merge your conditions manually.

import { TransactWriteItemsCommand } from "@aws-sdk/client-dynamodb";
import type { TransactWriteItemsInput } from "@aws-sdk/client-dynamodb";

const cmd: TransactWriteItemsInput = await User.transactUpdate(
[
{
condition: {
id: "user-1",
},
set: {
isActive: true,
},
},
{
condition: {
id: "user-2",
isActive: false,
},
set: {
isActive: true,
},
},
],
{ exec: false }
);

// pretending UserOrders is another instance of Model

const orderCheck: TransactWriteItemsInput = await UserOrders.transactWrite(
[
{
check: [
{
id: "user-id",
orders: {
$size: 0,
},
},
],
},
],
{ exec: false }
);

cmd.TransactItems.push(orderCheck.TransactItems[0]);

await User.client.send(new TransactWriteItemsCommand(cmd));