BatchWriteItemCommand AWS.DynamoDB class 在 Nodejs 中使用 AWS SDK V3

BatchWriteItemCommand with AWS.DynamoDB class using AWS SDK V3 in Nodejs

我已经尝试了几个小时来使用 BatchWriteItemCommand 执行 DynamoDB DeleteRequest,但我不断收到以下错误:

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

这是我的 table 的样子:

分区键:user_id(字符串)

排序键:时间戳(数字)

DynamoDB Screenshot

我的代码是这样的:

// 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();

这里有一些我一直在尝试关注的资源: 资源 1:Writing items in Batch Example 资源 2:AWS Javascript SDK v3 Documentation

更新:BatchWrite PutRequest 使用下面的代码,所以我知道我的 keys/attributes 的结构更接近正确。仍然不适用于 DeleteRequest。

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

您在删除项目时没有提供项目。您提供密钥。

这是一个工作示例:

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();