transactDelete
Introduction
transactDelete
method performs transact delete request using aws-sdk TransactWriteItemsCommand.
execute
transactDelete
takes 2 arguments:
- (required) an array of object where each object includes:
- item
primaryIndex
- optionnally KeyConditionExpression
- optionnally Condition expression which will perform
FilterExpression
- item
- options (optionnal)
const res = await User.transactDelete(
[
{
id: "user-1",
},
{
id: "user-2",
age: {
$gt: 20,
},
},
],
{ ReturnConsumedCapacity: "TOTAL" }
);
options
options
is an object which accepts TransactWriteItemsInput
options and DynamoQL specific options.
exec
exec
boolean directive to execute or not the actual request.
import type { TransactWriteItemsInput } from "@aws-sdk/client-dynamodb";
const cmd: TransactWriteItemsInput = await User.transactDelete(
[
{
id: "user-1",
},
{
id: "user-2",
age: {
$gt: 20,
},
},
],
{ exec: false }
);
check
ConditionCheck on the same table.
const res = await User.transactDelete(
[
{
id: "user-1",
},
{
id: "user-2",
age: {
$gt: 20,
},
},
],
{
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.transactDelete(
[
{
id: "user-1",
},
{
id: "user-2",
age: {
$gt: 20,
},
},
],
{ 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));