繁体   English   中英

DynamoDB 流 - 查询和批量删除

[英]DynamoDB Streams - Querying and Batch Delete

我有两张桌子:

  1. JobListings
  2. JobListingImages

英文说明:当我删除一个职位列表时,我希望JobListingImages表中与该职位列表关联的所有图像也被删除。

代码说明:当 JobListings 中发生JobListings streamedItem.eventName === "REMOVE"事件时,我想查询JobListingImages中的JobListingID属性,返回所有记录,然后删除。

我的用例分步骤:

  1. 一个职位列表被删除
  2. JobListing 表 streamedItem.eventName streamedItem.eventName === "REMOVE"触发器 Lambda function
  3. 我得到工作列表的 id 并查询 JobListingImages 表
  4. 我返回所有完全匹配的结果
  5. 然后我批量删除这些结果

文档在这里。

到目前为止,这是我的工作:

exports.handler = async (event) => {
  for (const streamedItem of event.Records) {
    if (streamedItem.eventName === "REMOVE") {

      //Get the ID of the deleted draft job listing
      const id = streamedItem.dynamodb.OldImage.id.S;
      let imagesToDelete = [];

      //Organise the parameters to query JobListingImages table
      var params = {
   TableName: table,
    FilterExpression: "#draftJobListingID = :draftJobListingID",
    ExpressionAttributeNames: {
      "#draftJobListingID": "draftJobListingID",
    },
    ExpressionAttributeValues: {
      ":draftJobListingID": id,
    },

    Select: "SPECIFIC_ATTRIBUTES",
    AttributesToGet: [id],
      };

      //Query the table, and store the results
      documentClient
        .query(params, function (err, data) {
          if (err) console.log("THERE WAS AN ERROR", err);
          else {
            imagesToDelete = data
          }
        })
        .promise();

    //   Batch delete all the stored items
        
        //...Code here (unsure how to complete)

      return { status: "done" };
    }
  }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM