transactWrite
Introduction
transactWrite
method performs transact write request using aws-sdk TransactWriteItemsCommand.
execute
transactWrite
takes 2 arguments:
- (required) an object which must include:
- (optionnal) options
check
an array of Condition expression with at least item identifier.
const res = await User.transactWrite({
check: [
{
id: "user-64",
isActive: false,
},
],
});
delete
an array of object which includes
condition
Condition expression object with at least item identifier.- (optionnally)
ReturnValuesOnConditionCheckFailure
.
const res = await User.transactWrite({
delete: [
{
condition: {
id: "user-64",
isActive: false,
},
},
],
});
put
an array of object which includes
item
item to be stored.- (optionnally)
condition
Condition expression object. - (optionnally) setterInfo
- (optionnally)
ReturnValuesOnConditionCheckFailure
.
const res = await User.transactWrite({
put: [
{
item: {
id: "user-64",
firstname: "John",
lastname: "Doe",
age: 27,
isActive: true,
},
condition: {
age: {
$gt: 25,
},
},
setterInfo: {
generateHash: true,
},
},
],
});
update
an array of object which includes
condition
Condition expression object with at least item identifier.set
any Update Expression object.- (optionnally)
ReturnValuesOnConditionCheckFailure
.
const res = await User.transactWrite({
update: [
{
condition: {
id: "user-64",
isActive: false,
},
set: {
firstname: "John",
},
},
],
});
All together
const res = await User.transactWrite({
check: [
{
id: "user-07",
isActive: false,
},
{
id: "user-91",
isActive: true,
},
],
put: [
{
item: {
id: "user-61",
firstname: "John",
lastname: "Doe",
age: 25,
isActive: true,
},
condition: {
age: {
$gt: 25,
},
},
setterInfo: {
generateHash: true,
},
},
],
delete: [
{
condition: {
id: "user-32",
isActive: false,
},
},
],
update: [
{
condition: {
id: "user-12",
isActive: false,
},
set: {
firstname: "Mike",
},
},
],
});
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
transactWrite will return TransactWriteItemsCommandInput
object.
import type { TransactWriteItemsCommandInput } from "@aws-sdk/client-dynamodb";
const cmd: TransactWriteItemsCommandInput = await User.transactWrite(
{
check: [
{
id: "user-07",
isActive: true,
},
],
update: [
{
condition: {
id: "user-12",
isActive: false,
},
set: {
friendsId: {
$push: "user-07",
},
},
},
],
},
{
exec: false,
}
);