Home > Software design >  BatchWriteItemCommand with AWS.DynamoDB class using AWS SDK V3 in Nodejs
BatchWriteItemCommand with AWS.DynamoDB class using AWS SDK V3 in Nodejs

Time:03-08

I have been trying for hours to perform a DynamoDB DeleteRequest using BatchWriteItemCommand but I keep getting the following error:

Error ValidationException: 1 validation error detected: Value null at 'requestItems.td_notes_sdk.member.1.member.deleteRequest.key' failed to satisfy constraint: Member must not be null

This is what my table looks like:

Partition key: user_id (string)

Sort key: timestamp (number)

DynamoDB Screenshot

This is what my code looks like:

// Import required AWS SDK clients and commands for Node.js
import {
  DynamoDBClient,
  BatchWriteItemCommand,
} from "@aws-sdk/client-dynamodb";

// Set the parameters
export const params = {
  RequestItems: {
    "td_notes_sdk": [
      {
        DeleteRequest: {
          Item: {
              Key: {
                  user_id: { S : "bb" },
                  timestamp: { N : 2 },
                },
          },
        },
      },
    ],
  },
};

export const run = async () => {
  const ddbClient = new DynamoDBClient({ region: "us-east-2" });
  try {
    const data = await ddbClient.send(new BatchWriteItemCommand(params));
    console.log("Success, items inserted", data);
    return data;
  } catch (err) {
    console.log("Error", err);
  }
};
run();

Here are some resources that I've been trying to follow along with: Resource 1: Writing items in Batch Example Resource 2: AWS Javascript SDK v3 Documentation

Update: BatchWrite PutRequest work with the code below, so I know that the structure of my keys/attributes is closer to being correct. Still does not work for DeleteRequest.

export const params = {
  RequestItems: {
    "td_notes_sdk": [
      {
        PutRequest: {
          Item: {
            user_id: { "S": "bb" },
            timestamp: { "N": "5" },
          },
        },
      },
    ],
  },
};

CodePudding user response:

You don't supply an Item when deleting an item. You supply a Key.

Here is a working example:

const params_delete = {
  RequestItems: {
    "td_notes_sdk": [
      {
        DeleteRequest: {
          Key: {
            user_id: { S: "bb" },
            timestamp: { N: "2" },
          },
        },
      },
    ],
  },
};

const delete_batch = async () => {
  const ddbClient = new DynamoDBClient({ region: "us-east-2" });
  try {
    const data = await ddbClient.send(new BatchWriteItemCommand(params_delete));
    console.log("Success, item deleted");
    return data;
  } catch (err) {
    console.log("Error", err);
  }
};

delete_batch();
  • Related