DynamoDB 解组 dynamodb 记录数组?(SDK v3 JavaScript)

JE_*_*23A 1 amazon-web-services amazon-dynamodb aws-sdk

我找不到解组 DynamoDB 记录数组的方法?我从 DynamoDB 中的查询返回多个项目,但无法使用unmarshal来自@aws-sdk/util-dynamodb记录数组的函数。当我迭代记录数组并解组每一条记录时,这可行,但这不是最好的解决方案,因为我要返回大量数据。如何让 DynamoDB 返回多条记录,而无需迭代记录数组来解组每条记录?

代码:

  const client = new DynamoDBClient({ region: 'us-east-1' });  
  const currentDateUTC = moment().utc().format('YYYY-MM-DD');

  const command = new QueryCommand({
    TableName: 'Device-Data',
    IndexName: 'timestamp-index',
    KeyConditionExpression: '#timestamp = :timestamp',
    ExpressionAttributeNames: { '#timestamp': 'timestamp' },
    ExpressionAttributeValues: { ':timestamp': { S: currentDateUTC } },
  });
  
  const data = await client.send(command)
Run Code Online (Sandbox Code Playgroud)

回复:

[
  {
    deviceId: {
      S: "test1",
    },
    userId: {
      S: "318ecd46-bead-440f-83eb-60345aaa1c40",
    },
    timestamp: {
      S: "2022-06-12",
    },
    frequency: {
      S: "Monthly",
    },
  },
  {
    deviceId: {
      S: "test2",
    },
    userId: {
      S: "318ecd46-bead-440f-83eb-60345aaa1c40",
    },
    timestamp: {
      S: "2022-06-12",
    },
    frequency: {
      S: "Monthly",
    },
  },
]
Run Code Online (Sandbox Code Playgroud)

期望的响应:

[
  {
    deviceId: "test1",
    userId: "318ecd46-bead-440f-83eb-60345aaa1c40",
    timestamp: "2022-06-12",
    frequency: "Monthly",
  },
  {
    deviceId: "test2",
    userId: "318ecd46-bead-440f-83eb-60345aaa1c40",
    timestamp: "2022-06-12",
    frequency: "Monthly",
  },
]
Run Code Online (Sandbox Code Playgroud)

kul*_*csi 7

你可以在 v3 中做这样的事情:

const { unmarshall } = require("@aws-sdk/util-dynamodb");

const items = data.Items.map( (item) => {
    return unmarshall(item);
});
Run Code Online (Sandbox Code Playgroud)